Ok, i wrote a script pieced together from different things I found online.. can someone double check it for me?
# export list of Staging VMs
get-folder -Name "Staging" | get-vm | select Name | Sort Name | Export-CSV "c:\staging.csv" -NoTypeInformation
# Get Staging VMs
$stg = get-folder -Name "Staging"
# Shutdown VMs
Foreach ($VM in ($stg | Get-VM)){
# Shutdown the guest cleanly
$VM | Shutdown-VMGuest -Confirm:$false
}
# Remove Hard Disk 2 and 3
$stg = $null
$stg = get-folder -name "Staging"
Foreach ($VM in ($stg | Get-VM)){
# Remove Transient and Data disks
$hdName2 = “Hard disk 2"
$hdName3 = “Hard disk 3"
$vm = Get-VM $stg| Get-View
$tgtdev = $vm.Config.Hardware.Device | where {$_.DeviceInfo.Label -eq $hdName2}
$dev = New-Object VMware.Vim.VirtualDeviceConfigSpec
$dev.operation = “remove”
$dev.device = $tgtdev
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.DeviceChange += $dev
$vm.ReconfigVM($spec)
$tgtdev = $vm.Config.Hardware.Device | where {$_.DeviceInfo.Label -eq $hdName3}
$dev = New-Object VMware.Vim.VirtualDeviceConfigSpec
$dev.operation = “remove”
$dev.device = $tgtdev
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.DeviceChange += $dev
$vm.ReconfigVM($spec)
}
# Remove VMs from Inventory
get-folder -Name "Staging" | get-inventory | remove-inventory
# Add VMs to Inventory
$esxhost = prpp6-vmwhst23.i3global.net
$vm = $null
$fields = "01.Field 1"
foreach ($add in (import-csv staging.csv)) {
$vm = $vms | Where {$.Name -eq $add."01.Field 1"
$vmxfile = $datastore1 $vm/$vm.vmx
new-vm -VMfilepath $vmxfile -VMhost $esxhost
# Add hard Disk 2 and 3
New-harddisk -VM $vm -DiskPath [transient_deviceid] $vm/$vm.vmdk
new-harddisk -VM $vm -DiskPath [data_deviceid] $vm/$vm.vmdk
}
}