Thanks LucD! I ended up going a slightly different route. I added a check to see if there's a snapshot, if there is one the script simply tells the user to delete it before continuing. I couldn't think of a way to have the script write the output to the file and also type the output on the screen so I'm just running the commands twice. How hard would it be to change the script so that I can pull multiple VMs from the spreadsheet rather that enter a VM name each time? Here is my script:
$Migrationvm1 = Read-Host "Enter the name of the VM you intend to migrate"
$credential = get-credential
$COMVC = "aplvmw015"
$CARVC = "aplvmw016"
Connect-VIServer $COMVC -credential $credential
Connect-VIServer $CARVC -credential $credential
$snaps = Get-VM -Name $Migrationvm1 | Get-Snapshot
$Migvm1 = Get-VM -Name $Migrationvm1
if ($snaps) {
write-output "Snapshot Detected. Delete Snaphosts before continuing."
$snaps | Format-List -Property @{N = 'VM'; E = { $_.VM.Name } },
Name, Description, Created
}
else {
write-output "NO snapshot"
$Migvm1 | Format-List -Property @{N = 'VM'; E = { $_.Name } },
@{N='VMhost';E={$_.VMhost}},
@{N='Notes';E={$_.Notes}},
@{N='NumCpu';E={$_.NumCpu}},
@{N='MemoryGB';E={$_.MemoryGB}},
@{N='Folder';E={$_.Folder}},
@{N="IP Address";E={@($_.guest.IPAddress[0])}},
@{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}} |
Out-String |
Set-Content -Path c:\output\$Migrationvm1.txt
$Migvm1 | Format-List -Property @{N = 'VM'; E = { $_.Name } },
@{N='VMhost';E={$_.VMhost}},
@{N='Notes';E={$_.Notes}},
@{N='NumCpu';E={$_.NumCpu}},
@{N='MemoryGB';E={$_.MemoryGB}},
@{N='Folder';E={$_.Folder}},
@{N="IP Address";E={@($_.guest.IPAddress[0])}},
@{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}}
}
Also, how do you upload you code so that it's properly formatted? Mine keeps coming up at as text. Sorry, I'm really rusty with PowerCLI!