PowerShell Remote Sessions and Scope Question: Commands Displayed Locally

Here's an example script that tries to create a remote session on the server, then use WMI to list the IIS server application pools and specify 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 PowerShell, the CD in the directory that contains this file, the point source of the file and calls 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 application pools on my local machine, not TestServer.

Alternatively, if I run the following lines (identical to those of the function) manually at the PowerShell prompt, I get a list of application 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:\>

, , , PowerShell. - ?

+3
1

, Enter/Exit-PSSession . Enter-PSSession:

SYNOPSIS
    Starts an interactive session with a remote computer.

script New-PSSession Invoke-Command :

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

: script FilePath Invoke-Command:

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

script 01 .

+5

Source: https://habr.com/ru/post/1731292/


All Articles