PowerShell远程处理-调用命令不返回输出
我正在管理模式下从PowerShell远程运行以下命令。这将从远程计算机获取应用程序池(在IIS中)的状态PowerShell远程处理-调用命令不返回输出,powershell,remoting,invoke-command,Powershell,Remoting,Invoke Command,我正在管理模式下从PowerShell远程运行以下命令。这将从远程计算机获取应用程序池(在IIS中)的状态 $result = Invoke-Command -ComputerName $servername -ScriptBlock { param($appPoolName) Get-WebAppPoolState -Name $appPoolName } -ArgumentList $appPoolName | select value 如果我传递的是正确的值,我将在$r
$result = Invoke-Command -ComputerName $servername -ScriptBlock {
param($appPoolName)
Get-WebAppPoolState -Name $appPoolName
} -ArgumentList $appPoolName | select value
如果我传递的是正确的值,我将在$result
中获得正确的数据,但如果我传递的是无效的服务器名称,我将得到以下错误
“连接到远程服务器失败,错误消息如下:WinRM无法处理该请求。”
问题是我在$result
中没有得到任何东西,甚至这也不会捕获block。无法确定命令是否成功运行
请提供有关如何在$result中获取命令输出的任何指针
注意:我是远程计算机上的管理员,WinRM已启用。为了捕获错误,需要终止它。您可以使用
-ErrorAction Stop
强制执行此操作
try {
$result = Invoke-Command -ComputerName $servername -ScriptBlock {
param($appPoolName)
Get-WebAppPoolState -Name $appPoolName
} -ArgumentList $appPoolName -ErrorAction Stop | select value
}
catch {
$result = [pscustomobject]@{"value"="Not Found"}
}
使用try/Catch通过添加
-ErrorAction Stop
捕捉错误,并使用$error[0]
Try{
$result = Invoke-Command -ComputerName $servername -ErrorAction Stop -ScriptBlock {
param($appPoolName)
Get-WebAppPoolState -Name $appPoolName
} -ArgumentList $appPoolName | Select-Object value
}
Catch{
$result = $error[0]
}