Running IIS command on remote server via Powershell

iispowershell

I am trying to check if an IIS application pool exists on a remote server using a PowerShell script. The command I am running is:

test-path "IIS:\AppPools\DefaultAppPool"

If I run this script directly on the IIS server in question I get a response back of "True" so this tells me that I have IIS management correctly configure in PowerShell. However when I run the following script from a remote server I get a response of "False"

invoke-command -ComputerName IISSERVER -ScriptBlock { test-path "IIS:\AppPools\DefaultAppPool" }

I know that PowerShell remoting is correctly configured because I can run the following command and get a list of files

invoke-command -ComputerName IISSERVER -ScriptBlock { get-childitems "c:\" }

So why am I getting the wrong response about the existence of the application pool?

Best Answer

This is because the remoting PowerShell session does not include the "IIS:" PowerShell drive. (Try running "Get-PSDrive" locally, and then through Invoke-Command, to see the difference).

The "IIS:" drive is almost certainly being added by the WebAdministration PS snap-in when you are running Powershell locally on the IIS server, either because you are launching PowerShell from a special IIS-specific shortcut, or because a local PowerShell profile script is running and loading it.

You should get the results you're looking for by explicitly adding the WebAdministration snap-in to the remote session (which creates the "IIS:" drive) by changing your Invoke-Command to look like this:

invoke-command -ComputerName IISSERVER -ScriptBlock { Add-PSSnapin WebAdministration; test-path "IIS:\AppPools\DefaultAppPool" }
Related Question