This will enable lockdown mode for your host.
Be careful with this because it will disable your ability to use the root account when you SSH to your host.

(get-vmhost “host1.pcli.me” | get-view).EnterLockdownMode()

To disable use:
(get-vmhost “host1.pcli.me” | get-view).ExitLockdownMode()

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

You can use this code to send that array data, var output, or CSV file to anyone via email.
Two ways you can do this:

The quick way (no attachments):
– For this you will need a from, to, and valid SMTP server to send the message to.
– To email your script output, just replace the $body var or assign the var to $body.
– $body += $yourscriptdata

$emailFrom = “me@pcli.me”
$emailTo = “you@pcli.me”
$subject = “Email subject”
$body = “Body of the message”
$smtpServer = “smtp.pcli.me”
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)

The detailed way (using attachments)
– I added $thedate var so you can add a date string to your body or subject.
– You can add more files, just add more $fileX parms.
– You can send it to more than one person or group by adding more “$msg.To.Add” lines.

$thedate = (get-date).tostring(‘ddMMMyyy’)
$smtpServer = “smtp.pcli.me”
$file1 = “C:\temp\file1.csv”
$file2 = “C:\temp\vcenterExport.csv”
$att1 = new-object Net.Mail.Attachment($file1)
$att2 = new-object Net.Mail.Attachment($file2)
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = “me@pcli.me”
$msg.To.Add(“you@pcli.me”)
$msg.To.Add(“afriend@pcli.me”)
$msg.To.Add(FriendsFriend@pcli.me)
$msg.Subject = “EMail Subject”
$msg.Body = “Body of message – See attached CSV files `n`n`n`n”
$msg.Attachments.Add($att1)
$msg.Attachments.Add($att2)
$smtp.Send($msg)
$att1.Dispose()
$att2.Dispose()
sleep 10
del c:\temp\file1.csv
del c:\temp\vcenterExport.csv

*****Its important to run the $attX.Dispose() to free up system memory.

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

Need to run a quick DNS pull for a list of hosts, VM, vmotion IPs?
Run this, it takes seconds to pull 50+ dns items into a list that you can dump into Excel, csv, etc.

$myDNS = @()
$myDNS += [System.Net.Dns]::GetHostAddresses(“host1.pcli.me”) | %{$_.IPAddressToString}
$myDNS += [System.Net.Dns]::GetHostAddresses(“host2.pcli.me”) | %{$_.IPAddressToString}
$myDNS += [System.Net.Dns]::GetHostAddresses(“host3.pcli.me”) | %{$_.IPAddressToString}
$myDNS += [System.Net.Dns]::GetHostAddresses(“hostX.pcli.me”) | %{$_.IPAddressToString}
$myDNS

 

OR you can reformat it with a loop + Var if you like using arrays for your host list
$hostname = @()
$hostname += “host1.pcli.me”
$hostname += “host2.pcli.me”
$hostname += “host3.pcli.me”
$hostname += “hostX.pcli.me”

$myDNS = @()
Foreach($singleHost in $Hostname){
$myDNS += [System.Net.Dns]::GetHostAddresses(“$singlehost”) | %{$_.IPAddressToString}
}
$myDNS

If your hosts can see the CDP info from your physical network, this will help check or map your port configs.

$Clustername = “hotness”
$report = @()
$allhosts = get-cluster $Clustername | get-vmhost
foreach($singlehost in $allhosts){
Get-VMHost $singlehost | Where-Object {$_.State -eq “Connected”} |
%{Get-View $_.ID} |
%{$esxname = $_.Name; Get-View $_.ConfigManager.NetworkSystem} |
%{foreach($physnic in $_.NetworkInfo.Pnic){
$pnic = $_.QueryNetworkHint($physnic.Device)
foreach($thing in $pnic){
$ReportRow = “” | select Hostname,vmnic,Ttl,Samples,DevId,Address,PortId,HardwarePlatform,Vlan,FullDuplex,Mtu
if( $thing.ConnectedSwitchPort ) {
$reportRow.Hostname = $esxname
$reportRow.vmnic = $physnic.Device
$reportRow.Ttl = $thing.ConnectedSwitchPort.ttl
$reportRow.Samples = $thing.ConnectedSwitchPort.samples
$reportRow.DevId = $thing.ConnectedSwitchPort.Devid
$reportRow.Address = $thing.ConnectedSwitchPort.address
$reportRow.PortId = $thing.ConnectedSwitchPort.portid
$reportRow.HardwarePlatform = $thing.ConnectedSwitchPort.HardwarePlatform
$reportRow.Vlan = $thing.ConnectedSwitchPort.vlan
$reportRow.FullDuplex = $thing.ConnectedSwitchPort.fullduplex
$reportRow.Mtu = $thing.ConnectedSwitchPort.mtu
}}$report += $reportrow}}}

 Then you can “export-csv”  the $report var or just throw it on the command line to see the output.

We found an issue where the SAN wasn’t using VAAI commands correctly.
So,  we need to disable it.
Here is the code to do that:

$allhosts = get-vmhost
foreach($singlehost in $allhosts){
Set-VMHostAdvancedConfiguration -VMHost $singlehost -Name DataMover.HardwareAcceleratedMove -Value 0
Set-VMHostAdvancedConfiguration -VMHost $singlehost -Name DataMover.HardwareAcceleratedInit -Value 0
Set-VMHostAdvancedConfiguration -VMHost $singlehost -Name VMFS3.HardwareAcceleratedLocking -Value 0
}

Value of 1 = Enabled
Value of 0 = Disabled

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

This code will help you step though the creation of VMotion networks on your hosts.

######### the vars ####
$thehost = “host1.pcli.me”
$theVswitch = “vSwitch1”
$vmotion1IP = “1.1.1.1”
$vmotion2IP = “1.1.1.2”
$theSubnetMask = “255.255.255.254”

### the code ###
### Create the Vmotion Port groups
get-vmhost -name $thehost | Get-VirtualSwitch -name $theVswitch | new-VirtualPortGroup -name vmotion1 -vlanid 0
get-vmhost -name $thehost | Get-VirtualSwitch -name $theVswitch | new-VirtualPortGroup -name vmotion2 -vlanid 0

### Create the Vmotion VMKernels
New-VMHostNetworkAdapter -VMHost $thehost -PortGroup vmotion1 -VirtualSwitch $theVswitch -IP $vmotion1IP -SubnetMask $theSubnetMask -VMotionEnabled:$true
New-VMHostNetworkAdapter -VMHost $thehost -PortGroup vmotion2 -VirtualSwitch $theVswitch -IP $vmotion2IP -SubnetMask $theSubnetMask -VMotionEnabled:$true

### Change the Network Teaming Policy to Active/Standby on both vmotion network port groups.
### Use this code if you are using two vmotion networks and want them to run on seperate NICs
$thingy = get-virtualswitch -vmhost $thehost -name $theVswitch | Get-virtualportgroup -name “vmotion1” | get-nicteamingPolicy
$thingy | Set-NicTeamingPolicy -MakeNicActive “vmnic1” -MakeNicStandby “vmnic2”
$thingy = get-virtualswitch -vmhost $thehost -name $theVswitch | Get-virtualportgroup -name “vmotion2” | get-nicteamingPolicy
$thingy | Set-NicTeamingPolicy -MakeNicActive “vmnic2” -MakeNicStandby “vmnic1”

VMWare Links:
http://www.vmware.com/support/developer/windowstoolkit/wintk40u1/html/New-VirtualPortGroup.html
http://www.vmware.com/support/developer/windowstoolkit/wintk40u1/html/New-VMHostNetworkAdapter.html
http://www.vmware.com/support/developer/windowstoolkit/wintk40u1/html/Set-NicTeamingPolicy.html

This will enable Storage IO Control for all datastores that exist within a datastore cluster for the entire vcenter.
Edit as needed:

set-datastore -datastore (Get-DatastoreCluster | get-datastore) -StorageIOControlEnabled $true

 

Quick edits if you want a narrow scope:
Single Cluster:
set-datastore -datastore (Get-DatastoreCluster “MyStorageCluster”| get-datastore) -StorageIOControlEnabled $true

With a filter to only change datastores with “ThisText” in the name:
set-datastore -datastore (Get-DatastoreCluster | get-datastore | where {$_.name -match “ThisText”}) -StorageIOControlEnabled $true

 

Vmware Links:
http://www.vmware.com/support/developer/PowerCLI/PowerCLI51/html/Set-DatastoreCluster.html
http://www.vmware.com/support/developer/PowerCLI/PowerCLI51/html/Get-Datastore.html