Hi Experts,
I am attaching the script below that uses the “New-CimSession” cmdlet to connect to a remote system and shut down Hyper-V virtual machines on that remote machine.
Every few iterations of the script, I run into this error message (screenshot also attached) and script fails.
Note: both systems have accounts in Active Directory.
Workarounds to this error have been:
1. Building remote session using IP address instead of system name as in Active Directory. But this also fails after a few iterations.
2. Deleting and reattaching systems from active directory.
3. Pings still work in this state.
4. No duplicate entries in DNS
Could you suggest a different solution to this problem. Appreciate your help.
#===============================================================
# Declare vmHosts (SUTs) partners AND OTHER VARIABLES
#===============================================================
Hosts = "Host1" , "Host2"
#$Hosts = "172.31.17.51" , "172.31.18.21"
$filePath = "D:\VMs"
#=====================================================================================
# Collect and enter domain login credentials for connecting to domain network
#=====================================================================================
$username = "XXXXXXXX.local\Administrator"
$password = "XXXXX"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $secstr
# ===============================================================================
# Tear down existing remote-session connections.
# ===============================================================================
Remove-CimSession -Name *
#====================================================================================
# Create new sessions to each SUT, Session information stored in an array for later use.
#======================================================================================
$CimSessionArray = @()
for ($i=0; $i -lt $Hosts.Length; $i++){
$CimSession = New-CimSession -ComputerName $Hosts[$i] -Credential $cred
$CimSession
$CimSessionArray += $CimSession
}
<#
$CimSession = New-CimSession 172.31.18.21 -Credential $cred
$CimSession
$CimSession = New-CimSession 172.31.17.51 -Credential $cred
$CimSession
#>
# Preparing SUTs
# ================================================================================================================
# Tear down. Bringing SUTs (VMHosts) to clean state.
#
# 1. Stop all VMs on Hosts.
#
# ==================================================================================================================
foreach ($vmHost in $Hosts){
$VMList = get-vm -ComputerName $vmHost
foreach ($VM in $VMList) {
Write-Host "Stopping VM $($VM.Name) on $($vmHost)"
Stop-VM $VM
}
}