【发布时间】:2014-10-22 15:34:58
【问题描述】:
我有这个脚本应该能够在远程和本地主机上运行。它接受 -ComputerName 作为带有 '.' 的参数。 (localhost) 作为默认值。
目前我正在测试如何验证新的远程会话并为它编写了一个小 cmdlet。问题是,如果我使用 '.' 运行这个脚本或 localhost 作为 ComputerName 脚本尝试连接到我计算机上的新远程会话。这不起作用,因为我没有启用 PSRemoting。
这是我的测试脚本:
Function Test-PsRemoting {
[CmdletBinding()]
param(
$ComputerName = ".",
$Credentials
)
$ErrorActionPreference = "Stop"
Test-Connection -ComputerName $ComputerName -Count 1 |
Format-List -Property PSComputerName,Address,IPV4Address,IPV6Address
Test-WSMan $ComputerName
$session = New-PSSession -ComputerName $ComputerName -Credential $Credentials
Invoke-Command -ComputerName $computername { 1 }
}
所有命令,Test-WSMan、New-PSSession 和 Invoke-Command 都将失败,因为它们假设我要建立远程连接
如果 $ComputerName 是 '.' 是否可以让 Powershell 在本地会话中运行命令或 localhost 还是我必须在 if/else 子句中自己处理?
该脚本旨在在本地和远程计算机上运行,我不希望启用 PSRemoting 成为本地运行脚本的要求
【问题讨论】:
-
invoke-command localhost需要提升的命令提示符。
标签: powershell powershell-remoting