C# 当创建属性get-mailbox cmdlet时,使用Office365 a/c连接到exchange online时出错

C# 当创建属性get-mailbox cmdlet时,使用Office365 a/c连接到exchange online时出错,c#,powershell-remoting,C#,Powershell Remoting,错误:WinRM客户端无法在指定的时间内完成操作。检查计算机名是否有效,是否可以通过网络访问,并且启用了Windows远程管理服务的防火墙例外。错误号码:-2144108250 ps1代码: param ( [Parameter(Mandatory=$true)][System.Management.Automation.PSCredential]$credentials ) $Session = New-PSSession -ConfigurationName Microsoft.Excha

错误:WinRM客户端无法在指定的时间内完成操作。检查计算机名是否有效,是否可以通过网络访问,并且启用了Windows远程管理服务的防火墙例外。错误号码:-2144108250

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;
私人运行空间;
私有字符串UserName=ConfigurationManager.AppSettings[“office365username”];
私有字符串密码=ConfigurationManager.AppSettings[“office365password”];
内部PSHelper()
{
//创建PSCredentials类的对象
凭证=新的PSCredential(this.UserName,CreateSecurePassword(this.Password));
InitialSessionState SessionState=InitialSessionState.CreateDefault();
SessionState.ImportPSModule(新[]{“MSOnline”});
InitialSessionState会话=InitialSessionState.CreateDefault();
会话状态.ImportPSModule(新[]{“PSSession”});
//创建新的运行空间
runspace=RunspaceFactory.CreateRunspace(SessionState);
Open();
}
公共GetRecentUsersCountResponse GetRecentUserCount()
{
GetRecentUsersCountResponse响应=新建GetRecentUsersCountResponse();
尝试
{
int CountValue=0;
日期时间开始于周;
DateTime EndDateForWeek;
string script=ReadPowerShellScript(“GetRecentUserCount.ps1”);
Command cmd=新命令(脚本,true);
cmd.Parameters.Add(新命令参数(“凭证”,凭证));
Pipeline Pipeline=runspace.CreatePipeline();
pipeline.Commands.Add(cmd);
pipeline.Input.Close();
Collection collectionPSObject=pipeline.Invoke();
如果(collectionPSObject.Count>0)
{
foreach(集合PSObject中的PSObject itemUser)
{
//检查用户是否已获得许可,在创建时是否处于活动状态
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=日期时间。现在;
EndDateForWeek=Convert.ToDateTime(itemUser.Properties[“WhenCreated”].Value);
TimeSpan DifferenceofWeekDate=BeginDateForWeek-EndDateForWeek;
int differenceofdays=转换为32(DifferenceofWeekDate.Days);
//仅当最近7天内创建的用户
如果(不同日期0)
{
StringBuilder=新的StringBuilder();
foreach(管道中的对象项。错误。ReadToEnd())
{
Append(item.ToString());
生成器。附加(“-”);
}
Response.ErrorMessage=builder.ToString();
}

if(CountValue我开发了一个powershell脚本,可以运行该脚本。如果需要,您还可以将WhenCreated属性更改为另一个日期属性。我通常使用这些命令分别运行远程输入命令

$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
    }
}

如果有任何错误,请帮助我连接到exchange并返回过去7天和30天的最近用户,好吗
$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
    }
}