Hello,
I've put together this script drawing heavily from LucD's similar scripts (thanks LucD!) which imports information from a legacy .csv file, and then looks for the VMs on the vCenter in order to provide updated status's. It also checks to see if the VM no longer exists at all:
The script is working with one major exception, if the source .csv file has a field in which there is an extra line it's pushing that onto the next line. In other words if there is the migration strategy field contains:
Scheduled
Everything works, but if the migration strategy field has an extra line, EG:
Scheduled
Rescheduled due to change freeze
This will end up corrupting the output. Is there a way to fix this?
#Setup column labels in output file
$outfilname = "VM_Decom_status_Primary"
$outfilepath = "C:\Output\"
"VMNAME,currentstatus,Migration_Strategy,PreviousPowerstate,CurrentPowerstate,vCenter,datacenter,Core_Project,Barcode,Migration_Prime,Senior_Manager,Director,VP,PreviousProjectStatus,PreviousProjectNotes,CurrentProjectStatus,CurrentProjectNotes" > "$outfilepath$outfilname.csv"
#Import the source file
Import-Csv "C:\input\VMS_Main_VC.csv" -PipelineVariable row |
ForEach-Object -Process {
$check = Get-VM -Name $row.VM -ErrorAction SilentlyContinue
If ($check) {
$currentstatus = "FOUND"
} Else {
$currentstatus = "VM NOT FOUND"
}
If ($check -eq "FOUND") {
$VMNAME = $row.VM
$Migration_Strategy = $row.'Migration Strategy'
$PreviousPowerstate = $row.Powerstate
$CurrentPowerstate = $check.PowerState
$vCenter = $check.Uid.Split('@')[1].Split(':')[0].Split('.')[0]
$datacenter = (Get-Datacenter -VM $check).Name
$Core_Project = $row.'Core Project'
$Barcode = $row.Barcode
$Migration_Prime = $row.'Migration prime'
$Senior_Manager = $row.'Senior Manager'
$Director = $row.Director
$VP = $row.VP
$PreviousProjectStatus = $row.'Project Status'
$PreviousProjectNotes = $row.'Project Notes'
$CurrentProjectStatus = "Requires Update"
$CurrentProjectNotes = "Requires Update"
}
Else {
$VMNAME = $row.VM
$Migration_Strategy = $row.'Migration Strategy'
$PreviousPowerstate = $row.Powerstate
$CurrentPowerstate = $null
$vCenter = $row.vCenter
$datacenter = $row.Datacenter
$Core_Project = $row.'Core Project'
$Barcode = $row.Barcode
$Migration_Prime = $row.'Migration prime'
$Senior_Manager = $row.'Senior Manager'
$Director = $row.Director
$VP = $row.VP
$PreviousProjectStatus = $row.'Project Status'
$PreviousProjectNotes = $row.'Project Notes'
$CurrentProjectStatus = "Requires Update"
$CurrentProjectNotes = "Requires Update"
}
write-host $VMNAME $currentstatus $Migration_Strategy $PreviousPowerstate $CurrentPowerstate $vCenter $datacenter $Core_Project $Barcode $Migration_Prime $Senior_Manager $Director $VP $ProjectStatus $ProjectNotes
$output = $VMNAME,$currentstatus,$Migration_Strategy,$PreviousPowerstate,$CurrentPowerstate,$vCenter,$datacenter,$Core_Project,$Barcode,$Migration_Prime,$Senior_Manager,$Director,$VP,$PreviousProjectStatus,$PreviousProjectNotes,$CurrentProjectStatus,$CurrentProjectNotes -join ","
$output >> "$outfilepath$outfilname.csv"
}
-------------------------------------------