Powershell invoke-command不返回get-itemproperty的值

时间:2016-05-13 09:51:16

标签: powershell iis powershell-remoting

我正在编写脚本以从IIS服务器检索池回收参数。

我的问题是本地命令工作正常(我在几分钟内得到正确的结果):

(Get-ItemProperty "IIS:\AppPools\MyPool" -Name "Recycling.Periodicrestart.Time.Value").totalminutes

但是当使用invoke-command远程启动时,没有任何内容返回,$RecycleTimeInterval值为$null

$PSSession = New-PSSession -ComputerName MyServer
Invoke-Command -Session $PSSession -ScriptBlock {Import-Module WebAdministration}
$PoolArray = Invoke-Command -Session $PSSession -ScriptBlock {Get-ChildItem -Path 'IIS:\AppPools' -Verbose}
ForEach($pool in $PoolArray) {
    $PoolName = $pool.Name
    $RecycleTimeInterval = Invoke-Command -Session $PSSession -ScriptBlock {(Get-ItemProperty IIS:\AppPools\$args[0] -Name 'Recycling.Periodicrestart.Time.Value').totalminutes} -ArgumentList $PoolName
    }
}

请注意,获取池列表的Get-ChildItem命令可以正常工作。

1 个答案:

答案 0 :(得分:0)

我设法通过以下方式获得了价值:

$PSSession = New-PSSession -ComputerName MyServer
Invoke-Command -Session $PSSession -ScriptBlock {Import-Module WebAdministration}
$PoolArray = Invoke-Command -Session $PSSession -ScriptBlock {Get-ChildItem -Path 'IIS:\AppPools' -Verbose}
ForEach($pool in $PoolArray) {
    $PoolName = $pool.Name
    $RecycleTimeInterval = Invoke-Command -Session $PSSession -ScriptBlock {
        param($SBPoolName)
        (Get-Item IIS:\AppPools\$SBPoolName).Recycling.Periodicrestart.Time.TotalMinutes}
        -ArgumentList $PoolName
    }
}