0
錯誤:WinRM客戶端無法在特定時間內完成操作。檢查計算機名稱是否有效且可通過網絡訪問,並且啓用了Windows遠程管理服務的防火牆例外。錯誤號:-2144108250錯誤連接在線交換使用Office365 a/c創建屬性獲取郵箱cmdlet
PS1代碼:
param
(
[Parameter(Mandatory=$true)][System.Management.Automation.PSCredential]$credentials
)
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $credentials -Authentication Basic –AllowRedirection
Import PSSession $Session
Get-Mailbox
C#代碼:
PSCredential credential;
private Runspace runspace;
private string UserName = ConfigurationManager.AppSettings["office365username"];
private string Password = ConfigurationManager.AppSettings["office365password"];
internal PSHelper()
{
//Create the object of PSCredentials class
credential = new PSCredential(this.UserName, CreateSecurePassword(this.Password));
InitialSessionState SessionState = InitialSessionState.CreateDefault();
SessionState.ImportPSModule(new[] { "MSOnline" });
InitialSessionState Session = InitialSessionState.CreateDefault();
SessionState.ImportPSModule(new[] { "PSSession" });
//Create new runspace
runspace = RunspaceFactory.CreateRunspace(SessionState);
runspace.Open();
}
public GetRecentUsersCountResponse GetRecentUserCount()
{
GetRecentUsersCountResponse Response = new GetRecentUsersCountResponse();
try
{
int CountValue = 0;
DateTime BeginDateForWeek;
DateTime EndDateForWeek;
string script = ReadPowerShellScript("GetRecentUserCount.ps1");
Command cmd = new Command(script, true);
cmd.Parameters.Add(new CommandParameter("credentials", credential));
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.Add(cmd);
pipeline.Input.Close();
Collection<PSObject> collectionPSObject = pipeline.Invoke();
if (collectionPSObject.Count > 0)
{
foreach (PSObject itemUser in collectionPSObject)
{
//check if the user if licensed,IsActive,WhenCreated
if (itemUser.Properties["IsLicensed"] != null && itemUser.Properties["IsActive"] != null && itemUser.Properties["WhenCreated"] != null)
{
if (Convert.ToBoolean(itemUser.Properties["IsLicensed"].Value) && Convert.ToBoolean(itemUser.Properties["IsActive"].Value) && itemUser.Properties["WhenCreated"] != null)
{
BeginDateForWeek = DateTime.Now;
EndDateForWeek = Convert.ToDateTime(itemUser.Properties["WhenCreated"].Value);
TimeSpan DifferenceofWeekDate = BeginDateForWeek - EndDateForWeek;
int DiffernceofDays = Convert.ToInt32(DifferenceofWeekDate.Days);
//Count only if recently created users from last 7 days
if (DiffernceofDays <= 30)
{
CountValue++;
}
}
}
}
}
pipeline.Stop();
if (pipeline.Error.Count > 0)
{
StringBuilder builder = new StringBuilder();
foreach (object item in pipeline.Error.ReadToEnd())
{
builder.Append(item.ToString());
builder.Append(" - ");
}
Response.ErrorMessage = builder.ToString();
}
if (CountValue <= 7)
{
Response.RecentUserCountWeek = CountValue;
}
if (CountValue <= 30)
{
Response.RecentUserCountMonth = CountValue;
}
}
catch (Exception ex)
{
Response.ErrorMessage = ex.Message;
}
finally
{
runspace.Dispose();
}
//return Response;
return Response;
}
我想連接到交流,從過去的7天和30天返回最近的用戶。
您能幫我連接一下,交換任何錯誤,並返回最近7天和30天的用戶 –