Hello. I am using Hyper-V Manager V 10.0.18362.1 and I try to automate Antivirus update on each VM. I manage to do it
on Windows 10 virtuals machines with powershell by using Enter-PSSession and Invoke-Command. But I don't manage on Windows 7 and 8.1 virtuals machines.
I meet these errors :
Enter-PSSession : Une erreur s’est produite et Windows PowerShell n’est pas en mesure de la gérer. Une session à distance a peut-être pris fin.
Au caractère C:\Users\SESA105951\Documents\UpdateVMWin7.ps1:28 : 1
+ Enter-PSSession -VMName $VMName -Credential $cred
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation : (:) [Enter-PSSession], PSRemotingDataStructureException
+ FullyQualifiedErrorId : CreateRemoteRunspaceForVMFailed,Microsoft.PowerShell.Commands.EnterPSSessionCommand
Une erreur s’est produite et Windows PowerShell n’est pas en mesure de la gérer. Une session à distance a peut-être pris fin.
+ CategoryInfo : OpenError: (RDITS_Win8_32_fr:String) [], PSRemotingDataStructureException
+ FullyQualifiedErrorId : PSSessionStateBroken
Exit-PSSession : La référence d'objet n'est pas définie à une instance d'un objet.
Au caractère C:\Users\SESA105951\Documents\UpdateVMWin7.ps1:44 : 1
+ Exit-PSSession
+ ~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Exit-PSSession], NullReferenceException
+ FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.ExitPSSessionCommand
Could you tell me why I have these errors only on Windows 7 and Windows 8.1 Virtuals Machines ? My code is this one
:
$cred= New-Object System.Management.Automation.PSCredential($login, $securePassword )
#$cred= New-Object System.Management.Automation.PSCredential($login, $myCred.Password )
$VMName = 'RDITS_Win8_32_fr'
function Update-VM($VMName, $VMcred)
{
Invoke-Command -VMName $VMName -ScriptBlock {Import-Module PSWindowsUpdate -Force} -Credential $VMcred
Invoke-Command -VMName $VMName -ScriptBlock {Get-WUServiceManager} -Credential $VMcred
#Invoke-Command -VMName $VMName -ScriptBlock {Add-WUServiceManager -ServiceID 7971f918-a847-4430-9279-4a52d1efe18d -Confirm:$false} -Credential $VMcred
Invoke-Command -VMName $VMName -ScriptBlock {Add-WUServiceManager -ServiceID 9482f4b4-e343-43b6-b170-9a65bc822c77 -Confirm:$false} -Credential $VMcred
Invoke-Command -VMName $VMName -ScriptBlock {Get-WUInstall -MicrosoftUpdate -AcceptAll -AutoReboot} -Credential $VMcred
}
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securePassword);
$myPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)
Start-VM -VMName $VMName
Start-Sleep -Seconds 20
Enter-PSSession -VMName $VMName -Credential $cred
if($VMName.Contains("_32"))
{
Invoke-Command -VMName $VMName -ScriptBlock {start "C:\Program Files\McAfee\VirusScan Enterprise\mcupdate.exe"} -Credential $cred
}
else
{
Invoke-Command -VMName $VMName -ScriptBlock {start "C:\Program Files (x86)\McAfee\VirusScan Enterprise\mcupdate.exe"} -Credential $cred
}
if($VMName.Contains("_win10"))
{
Update-VM $VMName $cred
}
Exit-PSSession
Stop-VM -VMName $VMName