2014-08-28 43 views
2

我有這個腳本應該能夠在遠程和本地主機上運行。它接受-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子句中自己處理?

腳本意味着同時運行本地和遠程計算機上,我不想啓用是在本地運行腳本

回答

2

要求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

0

看起來您正試圖檢查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