3

Here's a sample script that attempts to create a remote session on a server, then use WMI to get a list of the server's IIS application pools, and list their names:

    function Test-Remoting
    {
        [CmdletBinding()]
        param
        (    
        )
        begin
        {
            Enter-PSSession TestServer
            $appPools = Get-WmiObject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPool" -Authentication 6
            $appPools | ForEach-Object {
                $appPool = $_;
                $appPool.Name
            }
            Exit-PSSession
        }    
    }

This function is contained in a file called "Test-Remoting.ps1." I open up PowerShell, CD into the directory that contains this file, dot-source the file in, and call the function:

PS C:\Users\moskie> . .\Test-Remoting.ps1
PS C:\Users\moskie> Test-Remoting

But the result of this script is a list of the application pools on my local machine, and not TestServer.

Alternatively, if I run the following lines (identical to the ones in the function) manually at the PowerShell prompt, I do get the list of app pools on the remote server:

PS C:\Users\moskie> Enter-PSSession TestServer
[TestServer]: PS C:\> $appPools = Get-WmiObject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPool" -Authentication 6
[TestServer]: PS C:\> $appPools | ForEach-Object { $appPool = $_; $appPools.Name }
<a list of the names of the application pools on TestServer>
[TestServer]: PS C:\>

I think there's a concept I'm oblivious to, regarding PowerShell remoting and scope. Can anyone help explain this behavior?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Moskie
  • 1,277
  • 2
  • 16
  • 23

1 Answers1

5

I believe Enter/Exit-PSSession is meant more interactive use. From the Enter-PSSession help:

SYNOPSIS
    Starts an interactive session with a remote computer.

In a script, use New-PSSession and Invoke-Command like so:

$session = New-PSSession server01
Invoke-Command -Session $session {hostname}
Remove-PSSession -Session $session

Update: To execute a complete script remotely use the FilePath parameter on Invoke-Command:

icm server01 -FilePath C:\users\keith\myscript.ps1 -arg 1,2

This will copy the script to the remote computer server01 and execute it there with the supplied parameters.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • I'll give that a shot, but I'd like to avoid a work-around such as that, because the script I'm looking to run is pretty involved. It would be a pain to have to run every command through Invoke-Command. Using Enter/Exit-PSSession would be the most convenient, because I would just have to surround my script with those commands. – Moskie Feb 04 '10 at 00:39
  • If it is a script, then you can do this `Invoke-Command -FilePath myscript.ps1` and PowerShell will move the file to the remote machine and execute it there. – Keith Hill Feb 04 '10 at 03:18
  • Thanks, I did end up going this route, so I'll mark yours as the answer. But this notion of an "interactive" session is a little nebulous... it would seem logical to me that when I have a script execute a command to enter a new session, that the commands that follow it would be run in that new session. Guess not... thanks again! – Moskie Feb 05 '10 at 17:31
  • Yeah it does seem to break the model of "whatever you type at the prompt can be pasted into a script and work". – Keith Hill Feb 05 '10 at 23:11