좋아요, 그렇기 때문에 함수를 호출하고 powershell에서 매개 변수를 전달하는 것에 대해 읽었지만 어쩌면 뭔가를 이해하지 못하고 있습니다. 함수를 호출하고 몇 개의 매개 변수를 전달하면 모든 것이 잘 작동합니다. 함수 내에 정의 된 변수이며 그렇지 않을 때 null 인 것으로 보입니다.Powershell 함수가 null 예외를 throw합니다
기능 :
function get-session {
$session = New-PSSession -ComputerName $ip -Credential $cred -Auth CredSSP
$subcomps = 'COMP1'
foreach ($Computer in $subcomps)
{
$Computer
Invoke-Command -Session $session -ScriptBlock { Get-WmiObject -ComputerName $Computer -Query "SELECT * FROM Win32_Group" }
}
Remove-PSSession -ComputerName $ip
}
스크립트 :
$ip = '123.45.67.89'
$ComputerName = "HOPCOMP"
$user = '\Admin'
$cred = $ComputerName + $user
$ip
$cred
(get-session -ComputerName $ip -Credential $cred)
나는이 실행하면 : 물론
Cannot validate argument on parameter 'ComputerName'. The argument is null or empty.
Supply an argument that is not null or empty and then try the command again.
+ CategoryInfo : InvalidData: (:) [Get-WmiObject], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObjectCommand
, 나는 기능에에 $ 컴퓨터를 변경하면은 Get-WMIObject에서 라인을 COMP1 모든 것이 잘 작동합니다. 그러나 foreach 루프에서 $ Computer 쓰기는 COMP1을 성공적으로 기록합니다.
무엇이 여기에 있습니까?
우수, 감사합니다! – snoop