首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >PowerShell表示未找到'%‘和``New Object`。我是不是错过了一个导入?WinRM或交换角色权限?

PowerShell表示未找到'%‘和``New Object`。我是不是错过了一个导入?WinRM或交换角色权限?
EN

Stack Overflow用户
提问于 2012-02-12 00:28:15
回答 2查看 1.3K关注 0票数 3

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

然后,当我注释掉有问题的for..each语句时,它显示无法找到新对象。

我是不是错过了一个导入?根据评论,我可能遗漏了WinRM中的某些配置或Exchange2010角色权限。

代码语言:javascript
代码运行次数:0
运行
复制
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)
{

}
        }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-16 07:39:56

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

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

票数 0
EN

Stack Overflow用户

发布于 2012-02-13 00:34:36

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

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

然后在本地运行空间中处理返回的内容。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9241830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档