![資格情報は Get-WmiObject では機能しますが、別のドメインのコンピューターの Invoke-Command では機能しませんか?](https://rvso.com/image/1641908/%E8%B3%87%E6%A0%BC%E6%83%85%E5%A0%B1%E3%81%AF%20Get-WmiObject%20%E3%81%A7%E3%81%AF%E6%A9%9F%E8%83%BD%E3%81%97%E3%81%BE%E3%81%99%E3%81%8C%E3%80%81%E5%88%A5%E3%81%AE%E3%83%89%E3%83%A1%E3%82%A4%E3%83%B3%E3%81%AE%E3%82%B3%E3%83%B3%E3%83%94%E3%83%A5%E3%83%BC%E3%82%BF%E3%83%BC%E3%81%AE%20Invoke-Command%20%E3%81%A7%E3%81%AF%E6%A9%9F%E8%83%BD%E3%81%97%E3%81%BE%E3%81%9B%E3%82%93%E3%81%8B%3F.png)
Last Checked
私は、 Windows Update の値を確認することと、リモート サーバーのすべてのドライブを列挙してそれらのドライブの残り容量を確認することという2 つのことを実行する 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%
このスクリプトは、私と同じドメインのコンピューターに対して実行すると、期待どおりに動作します。ただし、同じドメインになく、アクセスにローカル管理者アカウントを使用するコンピューターが数台あります。これらのコンピューターの 1 台に対してスクリプトを実行すると、Windows Update 部分は失敗しますが、ディスク領域部分は正常に実行されます。
スクリプトの2つの部分は同じを共有していますPsCredential
が、ディスクスペース部分はGet-WmiObject
パラメータとを使用する関数であるのに対し-ComputerName
、-Credential
Windows Update部分は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