Hi Guys
Got a strange issue a affecting a successfully tested script that has suddenly broke. The script was till a day ago, and the only change is the one in the heading.
Script:
$VCenter="VCenter"
$TargetCluster="TargetCluster"
$TargetDS="TestDS"
$StoragePolicy="vSANStorage Policy"
$LogFileBasePath = (Get-Location).tostring() + "\Log\"
$MailTo = "xxx@yyy.com"
$Batchfile = (Get-Location).tostring() + "\Mig.csv"
#Lets first Check that we have a log directory
IF (Test-Path $LogFileBasePath ) {
write-host "Log Directory found found moving on"
}
ELSE {
write-host "Creating the directory for Diagnostic Logs"
New-Item -Path "$LogFileBasePath" -ItemType Directory -ErrorAction SilentlyContinue | Out-Null
}
#function for logging to file
function Write-Log ($message)
{
$Date = (Get-Date -f "yyyyMMdd")
$LogDate = Get-Date -Format "yyyy:MM:dd hh:mm:ss"
$outContent = "$LogDate" + ": " + "$message"
$LogFile = "$LogFileBasePath\" + "$Date" + ".log"
Write-Host "$LogDate" ":" "$message"
Add-Content "$LogFile" $outContent
}
Write-Log "Importing batch file: $Batchfile"
try {
$vms = Import-Csv $Batchfile
}
catch {
Write-Log "Failed to import batch file: $Batchfile cannot continue.Please check that you have specified a valid bath file to process"
Break
#[Environment]::Exit(1)
}
Write-Log "Attempting to connect to vCenter server: $VCenter"
try {
Connect-VIServer $VCenter -credential (get-credential) -ErrorAction:Stop
Write-Log "Connected to vCenter server: $VCenter"
}
catch {
Write-Log "Could not connect to the vCenter server : $VCenter"
Break
#[Environment]::Exit(1)
}
# Get the Storage Policy in the vCenter
Write-Log "Locating storage policy: $StoragePolicy in vCenter:$vCenter"
try {
$SPBMPolicy = Get-SpbmStoragePolicy -Name $StoragePolicy
Write-Log "Located Storage Policy: $SPBMPolicy"
}
catch {
Write-Log "Could not locate the Storage Policy defined"
Break
#[Environment]::Exit(1)
}
Write-Log "Locating an ACI integrated VDS that Cluster: $TargetCluster is connected to"
try {
$dvSwitch = Get-Cluster $TargetCluster | Get-VMhost | ? { $_.Connectionstate -eq "Connected"} | Sort-Object Name | Select-Object -first 1 | Get-VDSwitch | ? {$_.Name -like "*ACI*"}
Write-Log "Located ACI integrated VDS named : $dvSwitch that Cluster: $TargetCluster is connected to"
}
catch {
Write-Log "Failed to find a suitable VDS that Cluster: $TargetCluster is connected to"
Break
#[Environment]::Exit(1)
}
foreach ($vm in $vms) {
$vmname = $vm.vmname
#check that VM exists
$vmexists = get-vm $vmname -ErrorAction SilentlyContinue
IF ($vmexists){
Write-Log "Located VM: $vmname"
#get the vm network adapters and check if PG's exist in vDS
$nics = @()
$nics = get-VM -Name $vmname | Get-NetworkAdapter
$niccount = $nics.count
$PGs = @()
foreach ($nic in $nics) {
$PGs += $dvSwitch | Get-VDPortgroup | Where-Object { $_.ExtensionData.Key -eq $nic.ExtensionData.Backing.Port.PortgroupKey }
IF ($PGs.Count -le 0) {
$nics
$PGs
Write-Log "There was a problem locating one or more of the target port-groups for VM: $vmname on the ACI vDS that $TargetCluster is connected to"
}
}
#list all nics in order
Write-Log "Listing $niccount network adapters"
$i = 0
foreach ($nic in $nics) {
Write-Log "VM: $vmname - NIC# $i Name: $($nic.name) - NIC# $i Network Name: $($nic.NetworkName) - PG ID: $($PGs[$i].ExtensionData.Key) - PG Name: $($PGs[$i].Name)"
$i += 1
}
#attempt migration
try {
$migrate = move-vm -VM (get-vm $vmname) -VMotionPriority High -Destination (Get-Cluster $TargetCluster | Get-VMhost | ? { $_.Connectionstate -eq "Connected"} | Select -first 1) -Datastore (Get-Datastore -Server $VCenter -Name $TargetDS) -DiskStorageFormat Thin -StoragePolicy $SPBMPolicy -NetworkAdapter $nics -PortGroup $PGs -ErrorAction Stop
Write-Log "VM: $vmname has been migrated to cluster $TargetCluster and placed on DS: $TargetDS"
# Send the report email
Send-MailMessage -SmtpServer "mail.fnb.co.za" -To $MailTo -Subject "Successfully Migrated - $vmname" -Body "$vmname has been migrated." -From "migrations@cts-rbrdp01.fnb.co.za"
}
catch [exception]{
Write-Log "VM: $vmname has been NOT migrated"
Write-Log "Error Caught: $_"
# Send the report email
Send-MailMessage -SmtpServer "mail.fnb.co.za" -To $MailTo -Subject "Error during migration - $vmname" -Body "$vmname has NOT migrated.Please examine the log file for detailed error trace" -From "migrations@cts-rbrdp01.fnb.co.za"
}
}
ELSE{
Write-Log "Cannot Locate: $vmname . Please check that it is valid and correct in batch file if required"
# Send the report email
Send-MailMessage -SmtpServer "mail.fnb.co.za" -To $MailTo -Subject "Error during migration - $vmname" -Body "$vmname has NOT migrated as it could not be found. Please check that it is valid and correct in the batch file if required" -From "migrations@cts-rbrdp01.fnb.co.za"
}
}
Write-Log "All VMs in $Batchfile has been processed, please review the logfile"
#Disconnect from vCenter instances
Disconnect-VIServer $VCenter -confirm:$false
error attached
Cannot find the source but tried 3 tier cluster to 3 tier cluster and got the same error, VSAN to VSAN same error.
Another important point is that the task still runs in vsphere but due to the error the script doesn't serialise migration.