2014-08-28 3 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 } 
} 

명령은 Test-가 WSMan은 New-PSSession을하고 내가

인가 원격 연결을 원하는 가정으로-명령 호출 실패의 모든 $ ComputerName이 '.'이면 Powershell이 ​​로컬 세션에서 명령을 실행할 수 있습니다. 또는 localhost 또는 if/else 절에서 직접 처리해야합니까?

이 스크립트는 로컬 및 원격 시스템에서 모두 실행하기위한 것입니다 내가 로컬

답변

2

을 스크립트를 실행하기위한 요구 사항으로 사용할 수 PSRemoting을 원하지 않는 AFAIK -variable 더 $localsession이 없습니다. 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로 생각하지 않았습니다. –

+0

Test-WsMan cmdlet에서 자격 증명을 사용하지 않았습니다. 그게 의도적 인거야? 이 경우 여전히 유효한 결과를 반환합니까? –

+0

나는 그의 질문에 대답하기 위해 그의 대본을 수정했다. 나는 그것을 실행하지 않았고'Test-WSMan'이 그 일을하기 위해 자격 증명을 요구하는지 모른다. 그것은 별개의 "문제"입니다. :-) –

0

컴퓨터에서 PowerShell Remoting이 활성화되어 있는지/작동 중인지 확인하려는 것 같습니다. 로컬 컴퓨터에 대해 yoru 기능을 실행하지 않으려면 로컬 컴퓨터를 필터링하거나 비 로컬 컴퓨터 이름으로 Test-PsRemoting을 전달하는 사람에게 맡길 수 있습니다. 로컬 컴퓨터를 통과하면 PowerShell Remoting을 사용할 수 없다는 결과가 발생합니다.

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이 필요하지 않으므로이 경우 일반 스크립트로 명령을 실행하고 싶습니다. –

관련 문제