如果ComputerName是localhost或'。',则将powershell脚本作为本地脚本运行。

问题描述:

我有这个脚本应该能够在远程和本地主机上运行。它接受-ComputerName作为参数与'。' (localhost)作为默认值。如果ComputerName是localhost或'。',则将powershell脚本作为本地脚本运行。

目前我正在测试如何验证新的远程会话并为它写了一个小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 } 
} 

所有的命令,试验的WSMan,新的PSSession当他们以为我想打一个远程连接

是调用命令将失败如果$ ComputerName是'。',那么可以让Powershell在本地会话中运行命令。或localhost,还是我必须在if/else子句中自己处理?

脚本意味着同时运行本地和远程计算机上,我不想启用是在本地运行脚本

要求PSRemoting AFAIK没有$localsession -variable。您可以使用if-tests:

Function Test-PsRemoting { 
    [CmdletBinding()] 
    param(
     $ComputerName = ".", 
     $Credentials  
    ) 

    $ErrorActionPreference = "Stop" 

    $remote = $ComputerName -notmatch '\.|localhost' 
    $sc = { 1 } 

    #If remote computer, test connection create sessions 
    if($remote) { 
     Test-Connection -ComputerName $ComputerName -Count 1 | 
      Format-List -Property PSComputerName,Address,IPV4Address,IPV6Address 

     Test-WSMan $ComputerName 

     $session = New-PSSession -ComputerName $ComputerName -Credential $Credentials 
    } 

    if($remote) { 
     #If remote computer 
     Invoke-Command -ComputerName $computername -ScriptBlock $sc 
    } else { 
     #Localhost 
     Invoke-Command -ScriptBlock $sc 
    } 

} 
+0

感谢您的提示,仍然是Powershell的学习者,并没有想到它将其作为ScriptBlock。 – 2014-08-29 19:56:53

+0

您没有在Test-WsMan cmdlet上使用凭据;那是故意的吗?它在这种情况下是否仍然返回有效结果? – 2014-08-30 15:50:31

+0

我修改了他的脚本来回答他的问题。我没有运行它,我不知道'Test-WSMan'是否需要凭证来完成它的工作。这是一个单独的“问题”。 :-) – 2014-08-30 16:46:39

看起来您正试图检查PowerShell Remoting是否在计算机上启用/正在运行。如果您不希望yoru功能在本地计算机上运行,​​那么您可以过滤掉本地计算机,或者将其保留给正在调用Test-PsRemoting的人传递非本地计算机名称。如果他们确实通过了本地计算机,那么他们会得到PowerShell远程处理未启用的结果。

function Test-PsRemoting 
{ 
    [CmdletBinding()] 
    param(
     [Parameter(Mandatory=$true)] 
     [string[]] 
     # The computers to check. 
     $ComputerName, 

     [Management.Automation.PSCredential 
     # The credentials to use to connect to the computers. 
     $Credentials  
    ) 

    # Filter out local computer names 
    $ComputerName = $ComputerName | Where-Object { $_ -ne '.' -and $_ -ne $env:COMPUTERNAME } 

    Test-Connection -ComputerName $ComputerName -Count 1 | 
     Format-List -Property PSComputerName,Address,IPV4Address,IPV6Address 

    $credentialsParam = @{ } 
    if($Credentials) 
    { 
     $credentialsParam.Credentials = $Credentials 
    } 

    Test-WSMan -ComputerName $ComputerName @credentialsParam 

    Invoke-Command -ComputerName $ComputerName { hostname } @credentialsParam 
} 
+1

感谢您的输入。我认为Frodes的答案更接近。即使用户将localhost作为参数传递,我也希望脚本能够运行,但不需要PSRemoting,因此我希望在这种情况下以普通脚本的形式运行这些命令。 – 2014-10-26 12:20:30