2015-02-06 4 views
2

아래 코드에서 Get-ChildItem 뒤에 $ scanpath 변수를 사용하려고하면 어떤 이유로 인해 작동하지 않습니다. 그러나 $ scanpath 대신 실제 경로를 넣으면 작동합니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? $ computer 변수와 $ savepath 변수 모두 잘 동작합니다.PowerShell - Invioke-Command에 변수 전달

$computer = 'Server' 
$scanpath = 'P$\Directory\Directory\Z' 
$savepath = 'C:\Z-Media.csv' 
Invoke-Command -ComputerName $computer -scriptblock {Get-ChildItem $scanpath -recurse -include *.mp3,*.wma,*.wmv,*.mov,*.mpg,*.ogg,*.jpg -force | select FullName, Length | Sort-Object { [long]$_.Length } -descending} | Export-Csv $savepath -NoTypeInformation 
+0

"does not work"란 무엇을 의미합니까? PowerShell에서 오류 메시지가 나타나면 해당 메시지의 내용을 공유 할 수 있습니까? – MatthewG

+0

빈 파일과 오류가 발생했습니다. – shank

답변

3

$scanpath은 스크립트 블록과 동일한 범위에 있지 않습니다. -

Invoke-Command -ComputerName $computer -scriptblock {Get-ChildItem $Using:scanpath -recurse} 

자세한 내용은 about_Scopes를 참조 Using 범위 수정

PowerShell을 3+ : 당신은이 문제를 해결하는 두 가지 방법이있다.

사용은 원격 명령 의 로컬 변수를 식별하는 특수 범위 수정 자입니다. 기본적으로 원격 명령의 변수는 원격 세션에서 으로 정의됩니다.

모든 버전 - 매개 변수

Invoke-Command -ComputerName $computer -scriptblock {param($thisPath) Get-ChildItem $thisPath -recurse} -ArgumentList $scanpath 

당신은 함수와 같은 스크립트 블록 매개 변수를 제공 할 수 있습니다. Invoke-Command-ArgumentList 매개 변수를 사용하여 값을 스크립트 블록의 매개 변수로 전달합니다.

+0

$ 사용 : scanpath가 필요합니다. 고맙습니다! – shank