如何使用PowerShell从远程计算机获取所有(IIS)网站的状态/状态?

4

我尝试从远程读取IIS网站的状态。 我尝试了以下代码:

$Session = New-PSSession -ComputerName $serverName
$block = { 
import-module 'webAdministration'
Get-ChildItem -path IIS:\Sites
}
Invoke-Command -Session $Session -ScriptBlock $block  | Out-File -append $WebreportPath

但是上述命令只给出了具有https绑定的网站,当涉及到https时,它会抛出以下错误。
The data is invalid. (Exception from HRESULT: 0x8007000D)
+ CategoryInfo          : NotSpecified: (:) [Get-ChildItem], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.PowerShell.Commands.GetChildItemCommand

我正在尝试远程连接安装了IIS 7的Windows Server 2008 R2操作系统。 请指导我,谢谢!

2个回答

8
希望对某些人有所帮助。以下是我的研究结果和答案。
在远程情况下,PowerShell只发送对象,并使用可用的模板进行呈现。因此需要相应地提供一个模板。我使用了“Format-Table”。要小心,如果您在外部使用“Format-Table”,会导致相同的错误。请记得在脚本块内部使用它。
容易出错的内容:
Invoke-Command  -ComputerName $serverName { Import-Module WebAdministration; Get-ChildItem -path IIS:\Sites} | Format-Table | Out-File -append $WebreportPath

正确的代码:

Invoke-Command  -ComputerName $serverName { Import-Module WebAdministration; Get-ChildItem -path IIS:\Sites | Format-Table} | Out-File -append $WebreportPath

我的问题指向以下内容: http://forums.iis.net/t/1155059.aspx?IIS+provider+and+PowerShell+remoting

祝好运!


这将解决数据传输问题,但您只是编写输出,并且失去了对象结构以进行进一步处理。 - oɔɯǝɹ

2

使用远程会话检索对象时似乎存在一些问题,但这不是阻碍问题(我也遇到了这个问题)。

为了防止错误被写入,您可以像这样使用-ErrorAction

$Session = New-PSSession -ComputerName $serverName

$block = { 
    Import-Module 'webAdministration'
    Get-ChildItem -path IIS:\Sites
}

Invoke-Command -Session $Session -ErrorAction SilentlyContinue -ScriptBlock $block | 
    Out-File -append $WebreportPath

这样做可以避免看到远程错误,但仍会收到所需数据。
请注意,这会隐藏所有错误,因此您可能会隐藏想要查看的错误。为此,您需要修复潜在问题,以便可以删除-ErrorAction补丁。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接