본문 바로가기
파워쉘(Powershell)/파워쉘 리모팅(Remoting-원격컴퓨터 제어)

파워쉘 (Invoke-Command) : 원격 컴퓨터에서 실행

by 예배파 2024. 4. 18.

Invoke-Command를 사용하면 원격 컴퓨터에서 스크립트를 실행시킬 수 있습니다

 

 

1. 로컬 컴퓨터에 있는 파워쉘 스크립트를 원격 컴퓨터에서  실행

#test.ps1 파일내용

$env:COMPUTERNAME  #현재 컴퓨터의 이름
#로컬에 있는 test.ps1파일을 원격 컴퓨터 com1에서 실행
Invoke-command -FilePat 'C:\Powershell\test.ps1' -ComputerName 'com1'


실행결과
com1

 

 

 

2. 원격 컴퓨터에서 실행 될 명령어들을 ScriptBlock에 넣고 실행 

Invoke-Command -ComputerName 'com1' -ScriptBlock{
    $env:COMPUTERNAME
}

실행결과
com1

 

 

 

3. 하나 이상의 원격컴퓨터에 ScriptBlock에 있는 명령어 실행

Invoke-Command -ComputerName 'com1','com2' -ScriptBlock{
    $env:COMPUTERNAME
}


#배열사용
$computers = 'com1','com2'
Invoke-Command -ComputerName $computers -ScriptBlock{
    $env:COMPUTERNAME
}



#Get-Content를 사용해서 원격 컴퓨터들 불러서 사용
Invoke-Command -ComputerName (Get-Content .\computers.txt) -ScriptBlock{
    $env:COMPUTERNAME
}


#Get-Content를 사용해서 원격 컴퓨터들을 배열에 저장후 사용
$coms = Get-Content .\computers.txt
Invoke-Command -ComputerName $coms -ScriptBlock{
    $env:COMPUTERNAME
}

 

 

 

4. 원격 컴퓨터에 있는 exe, batch파일 실행

Invoke-Command -ComputerName humpwcef99nbe81 -ScriptBlock{
    & "C:\Powershell\test.bat"
}

 

 

매개변수 사용

Invoke-Command -ComputerName 'com1' -ScriptBlock{
    & "C:\Powershell\test.bat" "aa" "bbb"  #매개변수 사용
}

 

 

 

5. ScriptBlock에 변수 전달

ScriptBlock안의 코드는 원격 컴퓨터에서 실행되기 때문에, ScriptBlock밖의 변수를 사용할 수 없어서

-ArgumentList 를 사용해서 전달

$a = "First"
$b = "Second"

Invoke-Command -ComputerName humpwcef99nbe81 -ScriptBlock{
    & c:\scripts\t.bat $args[0] $args[1]
}-ArgumentList $a, $b

 

 

 

6. ScriptBlock내에 param을 사용하여 매개변수 이름 바꾸기

$a = "First"
$b = "Second"

Invoke-Command -ComputerName humpwcef99nbe81 -ScriptBlock{
    param($firstArg, $sec_arg)
    & c:\scripts\t.bat $firstArg $sec_arg
}-ArgumentList $a, $b

 

 

 

7. return

Invoke-Command의 return값은 에러 메세지를 포함한 모든명령어 실행시 발생한 output이 됩니다

output이 여러개인 경우에는 return값을 배열로 받게됩니다 

 

int값 return

$r = Invoke-Command -ComputerName 'Computer-1' -ScriptBlock{
    1
}

 

 

 

string값 return

$r = Invoke-Command -ComputerName 'Computer-1' -ScriptBlock{
    "test"
}

 

 

 

HashTable값 return

$r = Invoke-Command -ComputerName 'Computer-1' -ScriptBlock{
    $PSVersionTable
}

 

 

 

배열값 return : ScriptBlock내의 명령어 출력이 두 개 이상일 때 array로 return

$r = Invoke-Command -ComputerName humpwcef99nbe81 -ScriptBlock{
    $PSVersionTable
    $PSEdition
}

 

$PSVersionTable 실행결과 : HashTable ==> return 값배열의 첫번째에 저장( $r[0] )

$PSEdition 실행결과 : String   ===> return 값배열의 두번째에 저장( $r[1] )

 

 

 

return배열의 마지막 값 사용 

원격 컴퓨터에 명령어 실행 결과를 true, false로 return 받고 싶은 경우가 있습니다

그런데 ScriptBlock명령어중 실행시 output이 있는 경우가 있어서, retrurn값 배열 내용이 몇 개가 될지

예상하기 어려운 경우가 있습니다

이런 경우에 마지막에 true, false를 return값에 추가 시키고, return값 배열의 마지막 항목만 읽어서

사용하면 됩니다

$r = Invoke-Command -ComputerName humpwcef99nbe81 -ScriptBlock{
    $PSVersionTable
    $PSEdition
    $ver = [System.environment]::OSVersion.Version.Major
    if($ver -eq 10){ $true }
    else { $false }

}

if($r[-1]) { Write-Host "Windows 10" }
else { Write-Host "Not WIndows 10" }

결과
Windows 10