May 312013
 

I’ve found that the Powershell Window performace is limited unless you make some changes.
Below are some quick things you can do to increase the performance and hopefully reduce the time of your script execution.

-  This command will check and set the Max Memory that each Powershell session can use.
Default is 150MB:
Get-item WSMan:\localhost\Shell\MaxMemoryPerShellMB
Set-item WSMan:\localhost\Shell\MaxMemoryPerShellMB 1024 -Force

-  This command, added to your script, will run garbage colection and free up some RAM.
[System.GC]::Collect()

- It was found that Windows Tasks will launch your Powershell scripts at “Below Normal” CPU priority.
This is how you can fix that issue.  Its a pain but VERY worth it if you use Windows Tasks to launch your scripts.
Step1 – Export your Task to an XML file.
-   schtasks /query /tn “\MyTask” /xml > “c:\temp\MyTask.xml”
Step2 – Edit the XML file (Notepad works) and change this part “<Priority>4</Priority>”
-  You should see that the orginal is set to a value of 7.  Setting it to 4 makes a world of difference.
Step3 – Import the XML back into the Tasks window.
- Within Task Scheduler, right click the folder you want the task to live and select “Import Task.”
- Browse to your file, Click Open, edit your task as needed and save  it

May 312013
 

This code will restore your distributed switch to a new vcenter or the existing vcenter (if the vDS is fully deleted or missing.)
Doing this restore will allow all VMs to go from “invalid backing” to the real portgroup name, and all hosts will show their nics attached to the vDS.

#edit the path and filename as needed:

Add-PSSnapin VMware.VimAutomation.Core
Add-PSSnapin VMware.VimAutomation.Vds

Connect-VIServer “MyvCenter” -username “username” -password “password”
new-vdswitch -backuppath “E:\VDSBackup\VDSconfig.zip” -keepIdentifiers -location “NameOfDatacenter”

Vmware Link:
http://www.vmware.com/support/developer/PowerCLI/PowerCLI51R2/html/New-VDSwitch.html

May 312013
 

This script will backup your Distributed Switch config to a zip file.
You can then use this zip file to restore the vDS to a new vcenter or if your vcenter database corrupts.

Add-PSSnapin VMware.VimAutomation.Core
Add-PSSnapin VMware.VimAutomation.Vds
Connect-VIServer “MyVcenter” -username “username” -pass “password”
$date= (get-date).tostring(‘ddMMMyyy-HHmm’)
$VDSwitches = get-VDSwitch | %{$_.name}
foreach($vdswitch in $VDSwitches){
$filename = “E:\VDSBackup\”+$vdswitch +”-”+ $date+”.zip”
Get-VDSwitch -Name $vdswitch | Export-VDSwitch -Destination $filename
}

Disconnect-viserver -confirm:$False

View the Restore code here >http://www.pcli.me/?p=44

Vmware Link:
http://www.vmware.com/support/developer/PowerCLI/PowerCLI51R2/html/Export-VDSwitch.html

May 312013
 

This code will get all of the host names in a cluster, parse the names (if you use FQDN for your hostnames), and rename the default “datastore1″ to “hostname-local” format
This will help resolve the issue of having a lot of “datastore1 (99)” showing in your Datastores view.
***Note, if you use the word “datastore1″ for other storage locations or have a host with more than one local storage device named “datastore1″ this may explode.

$hostnames = get-cluster mycluster | get-vmhost | %{$_.name}
foreach($ahost in $hostnames){
$one1 = $ahost.split(“.”)
$two2 = $one1[0]
$thename = $two2+”-local”
get-vmhost $ahost | get-datastore | where {$_.name -match “datastore1″} | set-datastore -name $thename
}

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

May 312013
 

This will work for a standard switch and only if a vswitch is already configured.
If you want to create the vSwitch first, check this post >http://www.pcli.me/?p=28

###Edit the four vars and paste the script.

$thecluster = “MyCluster”
$vswitch = “vSwitch1″
$PG = “MyProdNetwork”
$vlan = “1337″

$thehosts = get-cluster $thecluster | get-vmhost | %{$_.name}
foreach($ahost in $thehosts){
New-VirtualPortGroup -VirtualSwitch ( Get-VirtualSwitch -Name $vswitch -VMHost $ahost ) -Name $PG -VLanId $vlan
}

##########################################

If you need the portgroup to contain a vmKernel for vmotion, use the following instead:
#######  Set the four cluster vars then the hash table for the different vmkernel IP addresses
$thecluster = “MyCluster”
$vswitch = “vSwitch1″
$PG = “vmotion0″
$vlan = “0″

####### Create the vmk IP hashtable #######

$myvmkips = @{}
$myvmkips.add(“host1.pcli.me”,”10.10.10.10″)
$myvmkips.add(“host2.pcli.me”,”10.10.10.11″)
.
.
.
$myvmkips.add(“host9.pcli.me”,”10.10.10.19″)
####### end hashtable #######

 

$thehosts = get-cluster $thecluster | get-vmhost | %{$_.name}
foreach($ahost in $thehosts){
New-VirtualPortGroup -VirtualSwitch ( Get-VirtualSwitch -Name $vswitch -VMHost $ahost ) -Name $PG -VLanId $vlan
$vmkip = $myvmkips.get_item(“ahost”)
New-VMHostNetworkAdapter -VMHost $ahost -VirtualSwitch $vswitch -PortGroup $PG” -IP $vmkip -SubnetMask 255.255.255.0 -VMotionEnabled $true -Confirm:$false
}

 

Vmware Links:
VirtualPortGroup – http://www.vmware.com/support/developer/windowstoolkit/wintk40u1/html/New-VirtualPortGroup.html
VMHostNetworkAdapter = http://www.vmware.com/support/developer/PowerCLI/PowerCLI41/html/New-VMHostNetworkAdapter.html

May 312013
 

This will enable vmotion on an existing vmkernel portgroup for all hosts in a cluster.
If you want to create the port group first, check this post > http://www.pcli.me/?p=16

$ClusterName = “MyCluster”
$MyPortGroup = “MyPortGroup”
get-cluster $ClusterName | Get-VMHost | Get-VMHostNetworkAdapter -VMKernel | where {$_.PortGroupName -eq $MyPortGroup} | Set-VMHostNetworkAdapter -VMotionEnabled $true -Confirm:$false

This runs slow and you cannot use a “-RunAsync” to make it faster.

Vmware Link:
http://www.vmware.com/support/developer/windowstoolkit/wintk40u1/html/Set-VMHostNetworkAdapter.html