Remote powershell iis 7 [Answered]RSS

2 replies

Last post Apr 12, 2010 02:17 PM by jrjespersen

  • Remote powershell iis 7

    Mar 29, 2010 05:43 PM|stevepink|LINK

    $serv = servername

    New-PSSession -ComputerName $sevr

    $AppPool = "DefaultAppPool"

    invoke-command -Session $serv -Scriptblock {import-module webadministration; stop-WebAppPool $AppPool}

     I get the below error when I run the above.  I have narrowed it down to the $AppPool not being passed into the ScriptBlock.  What am I missing?

    Cannot validate argument on parameter 'Name'. The argument is null. Supply a non-null argument and try the command again.




  • Re: Remote powershell iis 7

    Apr 02, 2010 02:48 AM|thomad|LINK

    The variable is not declared in the scriptblock that you are running, i.e. the variable $AppPool is empty. You can try this without even involving IIS:

    $myvar = "test";invoke-command -Session $session -Scriptblock {"Value of myvar: $myvar"}

    You will see the following output:
    Value of myvar:

    You have to do it like this:

    invoke-command -Session $serv -Scriptblock {$AppPool = "DefaultAppPool";import-module webadministration; stop-WebAppPool $AppPool}

    Hope this helps

    Thomas Deml
    Group Program Manager
    Internet Information Services
    Microsoft Corp.
  • Re: Remote powershell iis 7

    Apr 12, 2010 02:17 PM|jrjespersen|LINK

     Alternatively, you can construct your scriptblock to take one or more parameters and pass the parameters into the invoke-command cmdlet.

    Changing the example to look like this:

    $scriptblock = {param($var2)"Value of myvar: $var2"}
    $myvar = "test";invoke-command -Session $rs -Scriptblock $scriptblock -argumentlist $myvar

    Outputs this:

     Value of myvar: test