인수로 변수를 인수 목록으로 전달합니다. 속성을 잃어 버리십시오

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

  •  11-12-2019
  •  | 
  •  

문제

$Computers = Get-QADComputer -sizelimit 5
.

다섯 대의 컴퓨터 목록을 반환합니다.와 루프

foreach($computer in $computers) {
echo "and then I can do this $computer.name"
.

$ 컴퓨터에서 computername 만 가져옵니다. 그러나 이처럼 시작하는 작업에 전달하려고 할 때 :

    Start-Job -FilePath $ScriptFile -Name $Computer.Name -ArgumentList $Computer
.

$ ScriptFile에서 $ Computer.Name을 수행 할 수 없습니다.나는 $ computer.name처럼 그것을 전달하고 $ args [0]과 같이 호출해야합니다.그러나 나는 다른 모든 속성을 잃습니다 (나는 $ scriptfile에서 무리를 사용하고 있습니다.)

여기서 뭐하고 있지 않습니까?$ 컴퓨터를 뭐라고 부릅니까?그리고 $ computer.name이라고 부르는 것은 무엇입니까?

Sune :)

도움이 되었습니까?

해결책

$ args [0] .name으로 이름 속성을 가져올 수 있어야합니다.so : $ computer.name과 같은 이름 매개 변수에 액세스하려면 $ scriptfile에서 매개 변수를 정의해야합니다.

param(
   $Computer
)

$Computer.name
.

'당신은 이것을 할 수 없습니다 :

echo "and then I can do this $computer.name"
.

PowerShell은 값 $ 컴퓨터 만 확장합니다.하위 표현식에 넣으십시오 :

echo "and then I can do this $($computer.name)"
.

다른 팁

정확히 그렇게, 이것은 내가 비슷한 것을 쓸 것인가 :

#Get Services
$Services = Get-Service
#Limit results to the first 5 entries
$Services = $Services[0..4]
#Loop through services
foreach ($Service in $Services)
{
    #For each Service run a basic echo to host to prove that the Service was passed in successfully
    Start-Job -ScriptBlock { param ($Service) Write-Host "Service Name is $($Service.Name)" } -ArgumentList $Service
}
.

다음과 같이 작업을 검색 할 수 있습니다.

#Retrieve Jobs
Get-Job | Receive-Job
.

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