![¿La credencial funciona para Get-WmiObject pero no para Invoke-Command para una computadora en un dominio diferente?](https://rvso.com/image/1641908/%C2%BFLa%20credencial%20funciona%20para%20Get-WmiObject%20pero%20no%20para%20Invoke-Command%20para%20una%20computadora%20en%20un%20dominio%20diferente%3F.png)
Tengo un script de PowerShell destinado a hacer dos cosas: verificar el Last Checked
valor de Windows Update y enumerar todas las unidades y verificar el espacio restante en esas unidades, para un servidor remoto.
El resultado esperado de la función es:
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%
El script funciona exactamente como se esperaba cuando se ejecuta en una computadora en el mismo dominio que yo. Sin embargo, tenemos un puñado de computadoras que no están en el mismo dominio y usamos una cuenta de administrador local para acceder. Al ejecutar el script en una de esas computadoras, la parte de Windows Update falla pero la parte de Espacio en disco se ejecuta correctamente.
Las dos partes del script comparten lo mismo PsCredential
, pero la parte de Espacio en disco es una Get-WmiObject
función que utiliza los parámetros -ComputerName
y -Credential
mientras que la parte de Windows Update está dentro de una Invoke-Command
función con -ComputerName
y-Credential
No estoy seguro de por qué lo mismo PsCredential
funcionaría para uno y fallaría para el otro, ¿tal vez una ruta de autenticación diferente?
El error que recibo de la parte de Windows Update es:
[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
El guión es:
$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
Respuesta1
Por lo que puedo decir, Invoke-Command
es necesario que la computadora sea un host confiable si no está en el mismo dominio, por lo que la solución es agregar la computadora como host confiable.
Para no interferir innecesariamente con los hosts confiables, implementé la solución de una manera que solo afectará temporalmente a los hosts confiables:
Antes Invoke-Command
(agregar un host de confianza):
$curHosts = (Get-ChildItem WSMan:\localhost\Client\TrustedHosts).Value
Set-Item WSMan:\localhost\Client\TrustedHosts -Value $server -Concatenate -Force
Después Invoke-Command
(restablecer hosts confiables):
Set-Item WSMan:\localhost\Client\TrustedHosts $curHosts -Force