下面是一个示例脚本,尝试在服务器上创建一个远程会话,然后使用WMI来获取服务器的IIS应用程序池的列表,并列出他们的名字:

    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
        }    
    }

此功能包含在文件称为“试验Remoting.ps1”。我打开PowerShell中,CD到包含此文件的目录,点源的文件,并调用该函数:

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

但这个脚本的结果是我的本地的机器上的应用程序池的列表,并的的TESTSERVER。

可选地,如果我运行下面的线(等同于该函数的那些)手动在PowerShell提示符,我获得在远程服务器上的应用程序池的列表:

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远程和范围。任何人的帮助可以解释这种现象?

有帮助吗?

解决方案

我相信进入/退出-PSSession将意味着更多的交互使用。从输入-PSSession将帮助:

SYNOPSIS
    Starts an interactive session with a remote computer.

在脚本中,使用新-的PSSession和调用指令,像这样:

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

<强>更新要执行一个完整的脚本远程使用上调用指令FilePath参数:

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

此将脚本复制到远程计算机SERVER01并与所提供的参数来执行它。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top