0

I'm scripting a task to remotely restart some server application pools periodically. I'm using Invoke-Command as follows:

Invoke-Command -ComputerName $server {Restart-WebItem "IIS:\AppPools\DefaultAppPool"}

and this works just fine; however, if I parameterize the app pool as follows

$appPool = "IIS:\AppPools\DefaultAppPool"
Invoke-Command -ComputerName $server {Restart-WebItem $appPool}

it fails with

Unexpected object type.
Parameter name: pspath

I assume this is just a syntax issue, but I can't work out what.

1 Answer 1

1

on the remote host $appPool will not exist. If you're using PS V3 you can prefix your variable with using: keyword ->

$appPool = "IIS:\AppPools\DefaultAppPool"
Invoke-Command -ComputerName $server {Restart-WebItem $using:appPool}

prior to version 3, you had to use the -argumentList parameter

Sign up to request clarification or add additional context in comments.

4 Comments

Excellent the $using syntax worked. Strangely I was under the impression that the local variable was resolved prior to the command being remotely executed. I was getting the same error message using -ArgumentList.
@KevinRoche nope, this is why -argumentlist is made for : "Supplies the values of local variables in the command. The variables in the command are replaced by these values before the command is run on the remote computer."
Just tried a few tests with -ArgumentList but I'm missing something. The -PSPath parameter for this function is a string array so I tried {Restart-WebItem} -ArgumentList (,$appPool) and also {Restart-WebItem} -ArgumentList $appPool - same error as original. What's the correct syntax?
staight from the doc: To use local variables in a command, use the following command format: {param($<name1>[, $<name2>]...) <command-with-local-variables>} -ArgumentList <value> -or- <local-variable>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.