Monday, May 25, 2015

powercli one liners

This is for me to look back and refer when I need it.
First do a connect-viserver to the vcenter.

1.       Add a new VDS named DVSwitch1 to the vCenter server's datacenter named DC1
New-VDSwitch -Name "DVSwitch1" -Location "DC1" -NumUplinkPorts 2
#optionally you can add  -MaxPorts8 as the initial number of ports

2.       Add a new Distributed portgroup (DVPortgroup) dvport to the DVSwitch1 with a vlan id of 3064 and port numbers of 8
Get-VDSwitch -Name "DVSwitch1" | New-VDPortgroup -Name "dvport" -VlanId 3064 -NumPorts 8
If you have multiple dvswtiches and dvportgroups then just copy paste the same line multiple times with different values and copy paste the entire thing into the powercli

3.       If you want to add a portgroup to the host then
connect-viserver <hostip>
Get-VirtualSwitch -name vSwitch0 | new-VirtualPortGroup -name "new portgroup" -vlanid 10

4.       Remove a portgroup on all hosts
Get-VMHost | Get-VirtualSwitch -Name $vSwitch | Get-VirtualPortGroup -Name $VirtualPortGroup | Remove-VirtualPortGroup

5.       add a new vswitch to a host.
remove the vmhost name to add it all hosts of the vcenter
get-vmhost $vmhost | New-VirtualSwitch -Name vSwitch1

6.       add a new vswitch to all hosts
 get-vmhost | New-VirtualSwitch -Name vSwitch1

7.       new portgroup to a vswitch of a host
 get-vmhost $vmhost | get-virtualswitch -name vSwitch1 | New-VirtualPortGroup PG

8.       DNS and domain name (you may use multiple dns with comma as a separator)
get-vmhost | Get-VMHostNetwork | Set-VMHostNetwork -DomainName $domainname -DNSAddress $dns

9.       add ntpservers to all hosts
 get-vmhost | add-vmhostntpserver -ntpserver,

10. rename portgroup on all hosts of the vcenter
#dont forget to add the vlan option at the end. If you don't then the existing vlan will be changed to 0
get-vmhost | get-VirtualPortGroup -Name OLDNAME | Set-VirtualPortGroup -Name NEWNAME -vlan xxx

11. Add Datastores to hosts
new-datastore -vmhost "" -Name "CaptainAmerica" -path "naa.6006016054303c009430d2accaf8e411" -vmfs

12. add esxi hosts to cluser
get-cluster $cluster | add-vmhost $vmhost -user root -password $password -force confirm:$false

13.  add vmkernel portgroup
New-VMHostNetworkAdapter -VMHost (Get-VMHost $hostname) -PortGroup $portgroupname -VirtualSwitch $switchname -IP $ipaddress -SubnetMask $subnetmask

14. Rename datastore
get-vmhost -name $vmhost | get-datastore -name data* | set-datastore -name $newname

Disable IPV6
follow the 4 steps below
15. Disable ipv6
Get-VMHost | Get-VMHostNetwork | Set-VMHostNetwork -IPv6Enabled $false

16. exit maintenance mode of clusters
get-cluster cluster1,cluster2,cluster3,cluster98 | get-vmhost | Set-VMHost -State Connected
17. reboot hosts of hosts of clusters
get-cluster cluster1,cluster2,cluster3,cluster98 | get-vmhost | Restart-VMHost -force
18. enter maintenance mode of hosts of cluster
get-cluster cluster1,cluster2,cluster3,cluster98 | get-vmhost | Set-VMHost -State Maintenance
enter maintenance mode on a range of hosts
Get-VMHost -Name  [VMHost[1-10]* | Set-VMHost -State Maintenance
yes , it does exactly what you guessed it would do 
19. stop cluster (hosts) 
get-cluster cluster* | get-vmhost | stop-vmhost -confirm:$false 
20. scratch partition 
Set-VMHostAdvancedConfiguration -VMHost $fqdn -Name
"ScratchConfig.ConfiguredScratchLocation" -Value

21. Syslog 

adds $Syslog as the target syslog server to all the hosts
get-vmhost | Get-AdvancedSetting -Name | Set-AdvancedSetting -Value $Syslog -Confirm:$false

applies the advanced setting on all hosts
get-vmhost | Get-AdvancedSetting -Name NAME_OF_ADVANCED_SETTING | Set-AdvancedSetting -Value THE_VALUE_OF_YOUR_CHOICE -Confirm:$false
23. shutdown all hosts get-vmhost | stop-vmhost -confirm:$false
shutdown hosts of a cluster
get-cluster cluster11 | get-vmhost | stop-vmhost -confirm:$false
shutdown hosts of clusters whose name begins with cluster get-cluster cluster* | get-vmhost | stop-vmhost -confirm:$false
23. shutdown hosts in maintenance mode

What if i want to shutdown just the hosts which are in maintenance mode?

$VMHosts = Get-VMHost -state maintenance | sort
foreach ($VMHost in $VMHosts) {stop-vmhost $VMHost -confirm:$false}
24. set snmp string on all hosts
foreach ($vmhost in (Get-VMHost)) {(Get-EsxCli -VMHost $vmhost).system.snmp.set($null,"$string",$null,$null,$null,$null,$null,$null,$null,$null,$null,$null,$null,$null,$null,$null)} 
25. vswitch security settings
Get-VirtualSwitch -Name $vSwitch0 | Get-SecurityPolicy | Set-SecurityPolicy -MacChanges $false
Get-VirtualSwitch -Name $vSwitch0 | Get-SecurityPolicy | Set-SecurityPolicy -ForgedTransmits $false
Get-VirtualSwitch -Name $vSwitch0 | Get-SecurityPolicy | Set-SecurityPolicy -AllowPromiscuous $false
26. Nicteamingpolicy 
get-vmhost | Get-VirtualSwitch -name vSwitch0 | Get-NicTeamingPolicy | Set-NicTeamingPolicy -LoadBalancingPolicy LoadBalanceIP
get-vmhost | Get-VirtualSwitch -name vSwitch0 | Get-VirtualPortGroup | where {$_.Name -like "_vmk"} | Get-NicTeamingPolicy | Set-NicTeamingPolicy -LoadBalancingPolicy LoadBalanceIP 
27. Change vmkernel ip 
Get-VMHost $vmhost | Get-VMHostNetworkAdapter -name $vmk| Set-VMHostNetworkAdapter -IP $newIP -confirm:$false
28. Enable VMotion on THE portgroup 
Get-Cluster | Get-VMHost | Get-VMHostNetworkAdapter |?{ $_.PortGroupName -match "vMotion"} | Set-VMHostNetworkAdapter  -VMotionEnabled:$true -Confirm:$false
29. Delete DVPortgroup 
Get-VDPortGroup -Name "$portgroup" | Remove-VDPortGroup 
30. Add vmnic(physical adapter) 
get-vmhost | Get-VirtualSwitch -name vSwitch0 | Set-VirtualSwitch -Nic vmnic3 -confirm:$false

31. Power Management Policy

 (Get-View (Get-VMHost | Get-View).ConfigManager.PowerSystem).ConfigurePowerPolicy(1) 
Note: Here 1=HighPerformance /2=Balanced /3=LowPower

32.Enable Fault Tolerance
Get-VMHostNetworkAdapter | where { $_.PortGroupName -eq "$portgroup" } | Set-VMHostNetworkAdapter -FaultToleranceLoggingEnabled $true -confirm:$false  

33. MOB Status
get-vmhost | get-advancedsetting Config.HostAgent.plugins.solo.enableMob | Select-Object Entity, @{Expression="Value";Label="MOb Enabled"}
34. Remove vswitch from all hosts of cluster 
get-cluster cluster01,cluster02 | Get-VMHost | Get-VirtualSwitch -Name vSwitch0 | Remove-VirtualSwitch
35. Remove Virtual Portgroup 
get-vmhost | get-virtualswitch -name $vSwitchname | get-virtualportgroup -name $portgroupname | remove-virtualportgroup
36. Unmount all iso/cd/dvd from all VMs 
get-vm | get-cddrive | set-cddrive -NoMedia -Confirm:$false
37. Enable DRS on all clusters 
get-cluster | set-cluster -DrsEnabled:$true -confirm:$false
38. Enable HA on all clusters 
get-cluster | set-cluster -HAEnabled:$true -confirm:$false
39. set mtu for a vmkernel 
Get-VMHost | Get-VMHostNetworkAdapter | Where { $_.GetType().Name -eq "HostVMKernelVirtualNicImpl" } | where Name -EQ vmk2 | Foreach { $_ | Set-VMHostNetworkAdapter -Mtu 9000 -Confirm:$false }
40. Remove ntp server 
Remove-VmHostNtpServer -NtpServer -VMHost (get-vmhost) -Confirm:$false