How to get all (IIS) websites status/state from remote machine using PowerShell?

3

I was trying to read the status of IIS website from remotely. I tried below code.

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

But above command given me only Websites with https binding, when it comes to https it throws below error.

The data is invalid. (Exception from HRESULT: 0x8007000D)
+ CategoryInfo          : NotSpecified: (:) [Get-ChildItem], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.PowerShell.Commands.GetChildItemCommand

I am trying to renmote a Windows Server 2008 R2 with IIS 7. Please guide me. Thanks !

powershell
iis
asked on Stack Overflow Mar 20, 2015 by Smug

2 Answers

6

Hope this will help for someone. Below is my findings and the answer.

Powershell sends only objects remotely, and render them using available templates. So need to give a template accordingly. I used "Format-Table". Be-careful, if you use "Format-Table" outside it results same error. Remember to use it inside the scriptblock.

Error-prone:

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

Correct code:

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

My refers : http://forums.iis.net/t/1155059.aspx?IIS+provider+and+PowerShell+remoting

Good Luck !

answered on Stack Overflow Mar 25, 2015 by Smug
2

There seems to be some problem with retrieving the objects using a remote session, but that is no blocking issue (I have run into this as well).

To prevent the error from being written, you can use -ErrorAction like this:

$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

This way you will not see the remoting error, but you will still receive the data you need.

Note that this hides all errors, so you are possibly hiding an error that you may want to see. For that you will need a fix for the underlying ptoblem, so you can remove the -ErrorAction patch.

answered on Stack Overflow Jul 31, 2016 by oɔɯǝɹ

User contributions licensed under CC BY-SA 3.0