PowerShell 远程会话和范围问题:命令似乎在本地运行
下面是一个示例脚本,它尝试在服务器上创建远程会话,然后使用 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
}
}
该函数包含在名为“Test-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 远程处理和范围的信息。任何人都可以帮助解释这种行为吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信 Enter/Exit-PSSession 意味着更多的交互使用。来自 Enter-PSSession 帮助:
在脚本中,使用 New-PSSession 和 Invoke-Command,如下所示:
更新: 要远程执行完整的脚本,请使用 Invoke-Command 上的 FilePath 参数:
这将复制将脚本发送到远程计算机 server01 并使用提供的参数在那里执行它。
I believe Enter/Exit-PSSession is meant more interactive use. From the Enter-PSSession help:
In a script, use New-PSSession and Invoke-Command like so:
Update: To execute a complete script remotely use the FilePath parameter on Invoke-Command:
This will copy the script to the remote computer server01 and execute it there with the supplied parameters.