PowerShell 说“%”;和“New-Object”未找到。我错过了导入吗? WinRM 或 Exchange 角色权限?

发布于 2025-01-04 10:33:37 字数 2809 浏览 2 评论 0原文

假设我已经将 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)
{

}
        }

Suppose I already configured IIS to allow the remote runspace of "full", how do I resolve the exception that I'm getting where Powershell says "%" is not found.

Then when I comment out the offending for..each statement, it says New-Object can't be found.

Am I missing an import? Based on the comments, it's possible that I'm missing some configuration in WinRM, or an Exchange 2010 role permission.

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)
{

}
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眉黛浅 2025-01-11 10:33:37

远程会话是一个受限的运行空间。它不允许您使用脚本所需的 cmdlet。我相信您需要做的是使用不受约束的本地运行空间,然后使用调用命令在远程运行空间中运行 Exchange 管理 cmdlet:

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

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

The remote session is a constrained runspace. It doesn't allow you to use cmdlets you're wanting for your script. I believe what you'll need to do is use an unconstrained local runspace, and then from there run the Exchange management cmdlets in the remote runspace using invoke-command:

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

Then work with the return from that in the local runspace.

泪意 2025-01-11 10:33:37

中导入 Exchange 2010 命令

尝试创建本地运行空间,并从该 PS1 http://blogs.technet.com/b/exchange/archive/2009/11/02/3408653.aspx

Try creating a local runspace, and importing the Exchange 2010 commands from within that PS1

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文