로 호출하는 powershell 스크립트가 Get-WmiObject
있습니다 -Credential
. 그러나 로컬 컴퓨터에서 실행하면 오류가 발생합니다.
Get-WmiObject : User credentials cannot be used for local connections
이 오류를 피하기 위해 if localhost 논리를 추가하는 올바른 방법은 무엇입니까? 아니면 더 좋은 방법이 있습니까?
답변
항상 WMI를 통해 로컬 IP를 쿼리하여 $ localIP에 저장 한 다음 파이프 라인 또는 배열에서 현재 다음 주소와 일치시킬 수 있습니다.
if ($localIP -eq $otherIP) { get-wmiobject without -credential }
else { existing query }
답변
첫 번째 명령에서 erroraction stop을 사용하여 try catch 블록으로 랩핑하면 오류를 포착하고 자격 증명없이 catch 블록을 실행합니다.
Try
{
Get-WmiObject -Credential domain\user -ComputerName localhost -class Win32_BIOS -erroraction Stop
}
Catch
{
Get-WmiObject -ComputerName localhost -class Win32_BIOS
}