Running IIS command on remote server via Powershell

6

3

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?

Paul Hunt

Posted 2013-06-19T16:17:54.587

Reputation: 113

1note that in the example for invoke-command, get-childitems "c:" should read get-childitem "c:". That will teach me to copy and paste. – rob – 2014-01-15T10:22:37.177

Answers

5

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" }

jbsmith

Posted 2013-06-19T16:17:54.587

Reputation: 216

Please note that the IIS PSDrive caches data. You should call { Remove-Module WebAdministration; Import-Module WebAdministration } to see actual data if it's updated in another session. – Der_Meister – 2016-05-01T12:51:39.100