자격 증명이 Get-WmiObject에서는 작동하지만 다른 도메인에 있는 컴퓨터에서는 Invoke-Command에서는 작동하지 않습니까?

자격 증명이 Get-WmiObject에서는 작동하지만 다른 도메인에 있는 컴퓨터에서는 Invoke-Command에서는 작동하지 않습니까?

Last CheckedWindows 업데이트 값 확인, 모든 드라이브 열거 및 원격 서버에 대한 해당 드라이브의 남은 공간 확인이라는 두 가지 작업을 수행하기 위한 PowerShell 스크립트가 있습니다 .

함수의 예상 출력은 다음과 같습니다.

servername
----------
Last Checked for Windows Update:
01/18/2021 08:12:46

Disk    Size    Free    %Free
C:      501.46  238.06  47.47%
E:      300.00  140.15  46.72%

스크립트는 나와 동일한 도메인에 있는 컴퓨터에 대해 실행할 때 예상한 대로 정확하게 작동합니다. 그러나 동일한 도메인에 있지 않고 액세스를 위해 로컬 관리자 계정을 사용하는 소수의 컴퓨터가 있습니다. 해당 컴퓨터 중 하나에 대해 스크립트를 실행하면 Windows 업데이트 부분은 실패하지만 디스크 공간 부분은 성공적으로 실행됩니다.

스크립트의 두 부분은 동일 PsCredential하지만 디스크 공간 부분은 Get-WmiObject매개변수를 사용하는 함수 -ComputerName이고 -CredentialWindows 업데이트 부분 은 Invoke-Command-ComputerName-Credential

PsCredential왜 같은 것이 하나에서는 작동하고 다른 하나에서는 실패하는지 잘 모르겠습니다 . 아마도 다른 인증 경로일까요?

Windows Update 부분에서 발생하는 오류는 다음과 같습니다.

[servername] Connecting to remote server servername failed with the following error message : WinRM cannot process the
request. The following error with errorcode 0x80090311 occurred while using Kerberos authentication: We can't sign you
in with this credential because your domain isn't available. Make sure your device is connected to your organization's
network and try again. If you previously signed in on this device with another credential, you can sign in with that
credential.
 Possible causes are:
  -The user name or password specified are invalid.
  -Kerberos is used when no authentication method and no user name are specified.
  -Kerberos accepts domain user names, but not local user names.
  -The Service Principal Name (SPN) for the remote computer name and port does not exist.
  -The client and remote computers are in different domains and there is no trust between the two domains.
 After checking for the above issues, try the following:
  -Check the Event Viewer for events related to authentication.
  -Change the authentication method; add the destination computer to the WinRM TrustedHosts configuration setting or
use HTTPS transport.
 Note that computers in the TrustedHosts list might not be authenticated.
   -For more information about WinRM configuration, run the following command: winrm help config. For more
information, see the about_Remote_Troubleshooting Help topic.
    + CategoryInfo          : OpenError: (servername:String) [], PSRemotingTransportException
    + FullyQualifiedErrorId : AuthenticationFailed,PSSessionStateBroken

스크립트는 다음과 같습니다

$server = "servername"
$adminaccount = $server + "\localadminaccount"
$PASSWORD = ConvertTo-SecureString "localadminpassword" -AsPlainText -Force
$UNPASSWORD = New-Object System.Management.Automation.PsCredential $adminaccount, $PASSWORD

$out = ""
$out += $server + "`n----------`n"
$out += Invoke-Command -ComputerName $server -Credential $UNPASSWORD -ScriptBlock {
    Function Get-LocalTime($UTCTime) {
        $strCurrentTimeZone = (Get-WmiObject win32_timezone).StandardName;
        $TZ = [System.TimeZoneInfo]::FindSystemTimeZoneById($strCurrentTimeZone);
        Return [System.TimeZoneInfo]::ConvertTimeFromUtc($UTCTime, $TZ);
    } 

    $updateInfo = "Last Checked for Windows Update: `n";
    $updateInfo += Get-LocalTime $(New-Object -ComObject Microsoft.Update.AutoUpdate).Results.LastSearchSuccessDate;
    $updateInfo += "`n`n"
    
    Return $updateInfo
}

$out += "Disk`tSize`tFree`t%Free`n"
$disks = Get-WmiObject Win32_LogicalDisk -computername $server -filter DriveType=3 -Credential $UNPASSWORD
foreach ($objdisk in $disks)
{
    $size = "{0:N2}" -f ($objDisk.Size/1GB)
    $free = "{0:N2}" -f ($objDisk.FreeSpace/1GB)
    $freePercent="{0:P2}" -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size)

    $out += $objDisk.DeviceID + "`t" 
    $out += $size + "`t" 
    $out += $free + "`t" 
    $out += $freePercent + "`n"
}
$out

답변1

내가 알 수 있는 한, Invoke-Command컴퓨터가 동일한 도메인에 있지 않은 경우 신뢰할 수 있는 호스트여야 하므로 해결 방법은 컴퓨터를 신뢰할 수 있는 호스트로 추가하는 것입니다.

신뢰할 수 있는 호스트를 불필요하게 망치지 않기 위해 신뢰할 수 있는 호스트에 일시적으로만 영향을 미치는 방식으로 솔루션을 구현했습니다.

이전 Invoke-Command(신뢰할 수 있는 호스트 추가):

$curHosts = (Get-ChildItem WSMan:\localhost\Client\TrustedHosts).Value
Set-Item WSMan:\localhost\Client\TrustedHosts -Value $server -Concatenate -Force

이후 Invoke-Command(신뢰할 수 있는 호스트 재설정):

Set-Item WSMan:\localhost\Client\TrustedHosts $curHosts -Force

관련 정보