This code will reconfigure your Storage DRS Cluster Settings for a single VM.

### Set your vars ###
$theVM = “MyVM”
$theDatastoreCluster = “MyStorageCluster”

###
$SRMan = Get-View StorageResourceManager
$DSCluster = Get-DatastoreCluster -Name $theDatastoreCluster
$spec = New-Object VMware.Vim.StorageDrsConfigSpec
$DSCluster.ExtensionData.PodStorageDrsEntry.StorageDrsConfig.VmConfig |
where {(Get-View $_.VM | Select -ExpandProperty Name) -like $theVM} | %{
$entry = New-Object VMware.Vim.StorageDrsVmConfigSpec
$entry.Operation = “edit”
$entry.Info = $_
$entry.Info.Enabled = $false
$spec.vmConfigSpec += $entry
}
$SRMan.ConfigureStorageDrsForPod($DSCluster.ExtensionData.MoRef,$spec,$true)

This code will give you a list of all VM names and the OS configured for it. This code does not tell you what OS is running within your VM. You need vmware tools running within the guest to pull that info. You could you this script combined with the guest OS check to make sure they match.
**note – I added an if statement to filter if you only want Windows OS. Edit as needed.

connect-viserver “myvCenter”
$vmlist = @()
$GuestOSCheck = Get-VM | Get-View | Select name, @{N=”OS”;E={$_.Summary.Config.GuestFullName}}
foreach($single in $guestOSCHeck){
if($single.os -match “Windows”){
$vmlist += $single
}
}
Disconnect-viserver -confirm:$False
$vmlist

This code will upgrade your VMs virtual hardware level to the highest available value for the host its running on.
I’ve also added some checks and limits that you can remove, change, or add as needed.
**Note, you must power off the VM to upgrade the virtual hardware so be sure you want to do that before running this script.

The code will do the following:
– check if the tools are updated/current.
– – if tools are current, check for a windows OS config in the vmx.
– – – if windows VM, shutdown the vm OS, wait until its in a “poweredoff” state, then update the virtual hardware and confirm the prompt to upgrade.

All you need to do is change this var to your VMs name and paste the code below it.
$vmname = “MyVMName”

$checkToolsStatus = Get-VM $vmname | %{$_.Guest.Extensiondata.ToolsVersionStatus}
if($checkToolsStatus -match “guestToolsCurrent”){
$GuestOSCheck = Get-VM $vmname | Get-View | %{$_.Summary.Config.GuestFullName}
if($GuestOSCheck -match “Windows”){
Get-VM $vmname | Shutdown-VMGuest
do {
sleep 3
$vmcheck = get-vm $vmname | %{$_.powerstate}
} until ($vmcheck -match “PoweredOff”)
$id=(get-vm $vmname).id
$vmview=get-view -id $id
$param = @($null)
$vmview.gettype().GetMethod(“UpgradeVM_Task”).Invoke($vmview,$param)
sleep 5
get-vm $vmname | get-vmquestion | set-vmquestion -defaultOption -confirm:$false
}else{
$GuestOSCheck
}
}else{
$checkToolsStatus
}

VMware Links:
https://www.vmware.com/support/developer/PowerCLI/PowerCLI51/html/Get-VM.html
http://www.vmware.com/support/developer/windowstoolkit/wintk40u1/html/Get-VMQuestion.html

Quick vMotion code: (extra checks)
Get-VM -Name “MyVM” | Move-VM -Destination (get-vmhost “host1.pcli.me”) -runasync:$true

Many ways exist to complete this command.
Here is one without the double check so it will run faster:
Move-VM -vm “MyVM” -Destination host1.pcli.me” -runasync:$true

Some Special adds if needed, just tack these on to the above lines as needed:
-DiskStorageFormat “thin”
-DiskStorageFormat “thick”
-Datastore “myStorageLUN”
-Datastore (get-datastore “myStorageLUN”)

VMWare Link:
https://www.vmware.com/support/developer/PowerCLI/PowerCLI41U1/html/Move-VM.html

This will help you Export/Import the settings for a vApp so you can restore it if vCenter dies or if you need to migrate it to another location.
If you can power off all of your VMs an Export is easy.
Just run this and it will dump to a flat file:
 get-vapp -name “MyvApp” | export-vapp -destination “c:\temp\”

If you cannot power off your VMs or Cannot find some down time, this will do it without any interruption to your VMs.
It may seem interruptive but it works!

1) Screen cap or document all of the vapp settings. This includes the startup order, resources, and any special settings in the Advanced tab under Options. A manual backup will save you if some setting does not fully export using the API.
2) Disconnect all of the hosts and remove from inventory. (This is because you cannot export settings from a powered on vApp.)
– If you are migrating the vApp to another vcenter, just add the hosts to the new vcenter. It will disconnect them on the old vcenter without impacting the VMs.

3) Once all of the hosts that contain the vApp VMs are disconnected and removed, shutdown the vapp.
4) Export the config with this code:
 get-vapp -name “MyVapp” | export-vapp -destination “c:\temp\”

5) Enable DRS on the new cluster you want to import the vapp to. (You cannot Import a vApp without DRS enabled.)
6) Import it with this command
Import-vApp -Source “c:\temp\MyVapp.ovf” -VMHost (get-vmhost “MyHost.pcli.me”) -Location (get-cluster MyCluster) -Name “MyVapp” -force

7) Verify and edit the settings on imported vapp. (This is where your notes from step one comes in handy)
8) Move the vms into the imported Vapp. (Just drag the VMs from the host to the vApp.)
9) Once all of the VMs are in the vApp, Edit settings again and fix the startup order.

VMware Links:
https://www.vmware.com/support/developer/PowerCLI/PowerCLI51/html/Export-VApp.html
https://www.vmware.com/support/developer/PowerCLI/PowerCLI51/html/Import-VApp.html

This code will remove any mounted ISOs from a VM or set of VMs.
A VM with an ISO attached will not migrate off a host when the host is placed into Maintenance Mode or for a manual vmotion.

Single VM:
get-vm “MyVM” | get-cddrive |set-cddrive -nomedia -confirm:$false

VMs on a host:
get-vmhost “MyHost” | get-vm | get-cddrive |set-cddrive -nomedia -confirm:$false

VMs in a cluster:
get-cluster “MyCluster” | get-vm | get-cddrive |set-cddrive -nomedia -confirm:$false

VMs on a datastore:
get-vm -datastore “MyDatastore” | get-cddrive |set-cddrive -nomedia -confirm:$false

 

VMware Link:
https://www.vmware.com/support/developer/windowstoolkit/wintk40u1/html/Set-CDDrive.html