PowerShell说找不到'%'和`New-Object`。我错过了一个导入? WinRM或Exchange角色权限?

时间:2012-02-11 16:28:15

标签: c# exception powershell runspace

假设我已经将IIS配置为允许远程运行空间为“full”,如何解决我在Powershell所说的“%”找不到的异常。

然后当我注释掉有问题的for..each语句时,它说无法找到New-Object。

我错过了一个导入?根据评论,我可能缺少WinRM中的某些配置或Exchange 2010角色权限。

public static void testExchangeMBScript()
{
  PSCredential credential = new PSCredential(@"domain\me", createPassword("pw"));

WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "exchangehost.company.com", 80, "/Powershell", "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);

connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;

Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);

try
{
    runspace.Open();
    Pipeline pipeline = runspace.CreatePipeline();

    string ps1 = "Get-MailboxDatabase -Status";
    string PSFull = @" $Databases = Get-MailboxDatabase -Status
    foreach($Database in $Databases) {
        $DBSize = $Database.DatabaseSize
        $MBCount = @(Get-MailboxStatistics -Database $Database.Name).Count

        $AllMBStats = Get-MailboxStatistics -Database $Database.Name    
     $MBItemAssocCount = $AllMBStats   |   %{$_.AssociatedItemCount.value} |  Measure-Object -Average   -Sum
     $MBDeletedCount =   $AllMBStats   |   %{$_.DeletedItemCount.value} |  Measure-Object -Average   -Sum
     $MBItemCount =      $AllMBStats   |   %{$_.ItemCount.value} |  Measure-Object -Average  -Sum
     $MBDeletedItemSize= $AllMBStats   |   %{$_.TotalDeletedItemSize.value.ToMb() } |  Measure-Object -Average  -Sum
     $MBItemSize   =     $AllMBStats   |   %{$_.TotalItemSize.value.ToMb()} |  Measure-Object -Average    -Sum      

        New-Object PSObject -Property @{
Server = $Database.Server.Name
DatabaseName = $Database.Name
UserCount = $MBCount
""DatabaseSize (GB)"" = $DBSize.ToGB()
""AverageMailboxSize (MB)"" =  $MBItemSize.Average
""WhiteSpace (MB)"" = $Database.AvailableNewMailboxSpace.ToMb()
ItemCount = $MBItemCount.Sum
""LogicalSize (MB)"" =   $MBItemSize.Sum
        }
    }
";
    pipeline.Commands.AddScript(PSFull);



    // This method cannot be called multiple times on a given pipeline. The state of the 
    // pipeline must be NotStarted when Invoke is called. When this method is called, it
    // changes the state of the pipeline to Running. 
    // see http://msdn.microsoft.com/en-us/library/windows/desktop/ms569128(v=vs.85).aspx
    if (pipeline.PipelineStateInfo.State == PipelineState.NotStarted)
    {
        Collection<PSObject> results = pipeline.Invoke();
    }
}
catch (RemoteException re)
{

    // if: Assignment statements are not allowed in restricted language mode or a Data section.
    // then: configure IIS application settings PSLanguageMode = FullLanguage
}
catch (Exception e)
{

}
        }

2 个答案:

答案 0 :(得分:1)

远程会话是受约束的运行空间。它不允许您使用您想要的脚本cmdlet。我相信您需要做的是使用无约束的本地运行空间,然后使用invoke-command在远程运行空间中运行Exchange管理cmdlet:

$ AllMBStats = invoke-command {Get-MailboxStatistics -Database $ databasename} -argumentlist $ database.name -connectionuri“http://exchangehost.company.com/powershell”

然后使用本地运行空间中的返回值。

答案 1 :(得分:0)

尝试创建本地运行空间,并从该PS1中导入Exchange 2010命令

http://blogs.technet.com/b/exchange/archive/2009/11/02/3408653.aspx