6

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?

  • 1
    note that in the example for invoke-command, get-childitems "c:\" should read get-childitem "c:\". That will teach me to copy and paste. – rob Jan 15 '14 at 10:22
5
+50

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" }
| improve this answer | |
  • 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 May 1 '16 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.