在创建属性get-mailbox cmdlet时使用Office365 a/c联机连接到exchange时出错
本文关键字:联机 连接 出错 exchange Office365 属性 创建 get-mailbox cmdlet | 更新日期: 2023-09-27 18:00:40
错误:WinRM客户端无法在指定的时间内完成该操作。检查计算机名称是否有效,是否可以通过网络访问,并且是否启用了Windows远程管理服务的防火墙例外。错误编号:-214108250
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天的最近用户。
我已经开发了一个powershell脚本,我运行它来实现这一点。如果需要,还可以将"创建时间"属性更改为其他日期属性。我通常用这些命令单独运行远程输入的命令
$cred = Get-Credential
(enter full email address and password)
$Session = New-PSSession -ConfigurationName Microsoft.Exchange
-ConnectionUri https://ps.outlook.com/powershell/
-Credential $cred -Authentication Basic -AllowRedirection
Import-PSSession $Session
但如果你愿意,你可以把它们放在你的剧本里。我通常手动运行这个脚本。你只需要更改天数(确保它保持为负数),输入正确的文件路径,并添加你想要获得的任何其他属性。
$fpath = "c:'users'(currentuser)'desktop'UserCreateDates.csv"
$numdays = -30
$logfilePath = (Test-Path $fpath)
if (($logFilePath) -ne "True")
{
New-Item $fpath -ItemType File
Add-Content $fpath "Name,EmailAddress,Created"
}
else
{
Clear-Content $fpath
Add-Content $fpath "Name,EmailAddress,Created"
}
$date1 = [DateTime]::Today.AddDays($numdays)
$Mailboxes = Get-Mailbox -ResultSize Unlimited -filter {RecipientType -eq "UserMailbox"}
ForEach ($Mailbox In $Mailboxes) {
$DisplayName = """" + $Mailbox.DisplayName + """"
$UPN = """" + $Mailbox.UserPrincipalName + """"
$mailboxCreated = $Mailbox.WhenCreated
if ($mailboxCreated -gt $date1)
{
$DisplayName + "," + $UPN + ",""" +
$mailboxCreated + """" | Out-File -FilePath $fpath -Append
}
}