PowerShell Remote sessions and scope question: Commands appear to run locally -


Here is a sample script that tries to create a remote session on a server, then to get the list of the server's IIS Use WMI for the application pool, and list their names:

  Function Test-Remoting {[CMDLabing ()] Ultimate (start) {Enter- PSSession Test Server $ appPools = Get -WMIObject -namespace "root \ MicrosoftIISv2" -Choose "IISAPPplicationPool" - Authentication 6 $ Appulle | For every object {$ appPool = $ _; $ AppPool.Name} Exit-PSSession}}  

This function is included in a file called "Test-Remoting .ps1". I open the PowerShell, put the CD in this file in the directory, dot-paste the file, and call the function:

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

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

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

PS: \ Users \ Moskia & gt; Enter- PSSession Test Server [Test Server]: PS C: \ & gt; Dollar $ AppPools | For every object {$ appPool = $ _; $ AppPools.Name} & lt; A list of application pool names on TestServer & gt; [Test server]: psc: \> gt;

I think there is a concept that I know about PowerShell Remoting and Scope. Can anyone explain this behavior?

I believe entry / exit - PSSession means more interactive use - PSSession With help:

  SYNCPSIS starts an interactive session with a remote computer  

In a script, use the new-PSSession and ask-order such as :

$ session = new- PSSession Server 01 come-command-session $ session {Hostname} Remove- PSSession- session $ session

Updates: Perform a full script Use the FilePath parameter on remote-to-be-command:

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

This copy of the script will be copied to the remote computer server and it will execute thereby the supplied parameters.


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -