원격 서버에서 기존 스크립트를 실행하고 로컬 파일에서 출력을 캡처하려고합니다.

StackOverflow https://stackoverflow.com//questions/24055009

문제

거의 서버 (거의 40-50 대 서버)에서이를 실행하고 싶습니다

$ username="사용자"

$Password = "Password"

$servers = get-content "c:\temp\servers.txt"

$sb = {c:\temp\PsExec.exe -h \\$server -u $Username -p $password cmd.exe /c "echo . | Powershell.exe -executionpolicy bypass -file c:\script.ps1" > "$env:userprofile\Desktop\output.txt"} 

foreach($server in $servers)
{
    start-job -ScriptBlock $sb
}
.

이 코드는 시작 작업을 제거하지만 다른 쪽 다음에 실행됩니다.

우리 환경에서 제한되기 때문에 pssession 또는 invoke-command를 사용할 수 없습니다.

이 코드는 절대로 종료되지 않습니다.이 위치에서 멈 춥니 다.

 + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com
.

도움이 되었습니까?

해결책

로 시작하려면 변수를 작업에 전달하지 못합니다.필요한 것은 스크립트 블록 내에서 $ args 변수를 사용 한 다음 -ArgumentList와 원하는 변수를 전달하는 것입니다.

$Password = "Password"

$servers = get-content "c:\temp\servers.txt"

$sb = {
  c:\temp\PsExec.exe -h \\$args[0] -u $args[1] -p $args[2] cmd.exe /c "echo . | Powershell.exe -executionpolicy bypass -file c:\script.ps1" > "$args[3]\Desktop\output.txt"
} 

foreach($server in $servers)
{
    start-job -ScriptBlock $sb -ArgumentList $server,$Username,$password,$env:userprofile
}
.

나는 아마 환경 변수를 통과 할 필요가 없었지만 변수에 대한 범위 문제가있는 것처럼 보였다.

또는 ScriptBlock의 Param 블록을 사용하여 변수의 이름을 지정하는 것으로 기본적으로 명명 된 변수에 전달 된 인수를 최대로 맵핑 할 수 있습니다.

$Password = "Password"

$servers = get-content "c:\temp\servers.txt"

$sb = {
  Param ($Server,$UserName,$Password,$UserProfile)

  c:\temp\PsExec.exe -h \\$Server -u $UserName -p $Password cmd.exe /c "echo . | Powershell.exe -executionpolicy bypass -file c:\script.ps1" > "$UserProfile\Desktop\output.txt"
} 

foreach($server in $servers)
{
    start-job -ScriptBlock $sb -ArgumentList $server,$Username,$password,$env:userprofile
}
.

이 도움이되기를 바랍니다. 환호, 크리스.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top