Powershell

Powershell 외부서버의 스크립트 실행하기

bedbmsguru 2018. 5. 25. 15:39

1. AD  등록된 서버

AD 관리자 계정으로 스크립트 실행


Invoke-Command -ComputerName host1, host2 -ScriptBlock {Invoke-Expression 'C:\script.bat'}



2.AD에 등록되지 않은 서버


(1) 로컬 서버의 "신뢰할 수 있는 호스트 목록" 에 원격 서버 추가하기.
    Set-Item WSMan:\localhost\Client\TrustedHosts -Value "원격서버IP 혹은 호스트네임"


    여러개의 원격 서버를 지정해야할 경우

     Set-Item WSMan:\localhost\Client\TrustedHosts -Value "host1, host2"



(2)원격서버에서 스크립트 실행

$password='p@ssword'|convertto-securestring -asplaintext -force;

$cred=new-object -typename system.management.automation.pscredential('Domain\Username',$password);

$s = New-PSSession -computer "127.0.0.1" -credential $cred;

Invoke-Command -Session $s -ScriptBlock { cmd /c "c:\myfile.bat" };

Remove-PSSession $s;