powershell会话返回空

时间:2016-02-24 09:41:56

标签: c# asp.net powershell office365

我们尝试在powershell命令下运行并在以下环境中获得不同的结果,请告知我们可能忽略的问题:

$Username = 'xxxx@xxx.com'
$Password = ConvertTo-SecureString -AsPlainText 'xxxx' -Force
$Livecred = New-Object System.Management.Automation.PSCredential $Username, $Password
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Livecred -Authentication Basic -AllowRedirection
INVOKE-COMMAND –Session $Session –scriptblock {Get-DistributionGroup}

计算机环境1 :Windows 7 64位Powershell 4.0

结果环境1 :好的。能够返回所有分发组

计算机环境2 :使用System.Management.Automation.Powershell c#ASP.net在IIS中运行Windows 7 Powershell 4.0 64位 示例代码:

 PowerShell psExec = PowerShell.Create();
    Collection<PSObject> results;
    Collection<ErrorRecord> errors;
    psExec.AddScript(scriptText);
    results = psExec.Invoke();
    if (results != null && results.Count != 0){
     foreach (PSObject obj in results)
     {
      //this is where it retrieve the info from distribution group
      }
    }

结果环境2 :好的。能够返回所有分发组

计算机环境3 :Windows Server 2012 64位Powershell 4.0

结果环境3 :好的。能够返回所有分发组

计算机环境4 :使用System.Management.Automation.Powershell c#ASP.net在IIS中运行的Windows Server 2012 Powershell 4.0 64位 示例代码:

 PowerShell psExec = PowerShell.Create();
    Collection<PSObject> results;
    Collection<ErrorRecord> errors;
    psExec.AddScript(scriptText);
    results = psExec.Invoke();
    if (results != null && results.Count != 0){
     foreach (PSObject obj in results)
     {
      //this is where it retrieve the info from distribution group
      }
    }

结果环境4 :无法返回所有通讯组,它会出现此错误“

Connecting to remote server outlook.office365.com failed with the following error message : Access is denied. For more information, see the about_Remote_Troubleshooting Help topic.\r\nCannot validate argument on parameter \u0027Session\u0027. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.\r\n"

0 个答案:

没有答案