Difference between revisions of "PowerCLI Powerhsell"
Jump to navigation
Jump to search
Michael.mast (talk | contribs) |
Michael.mast (talk | contribs) |
||
Line 52: | Line 52: | ||
$num453=[int]$num453 + [int]1 | $num453=[int]$num453 + [int]1 | ||
}until ([int]$num453 -eq [int]$num1) | }until ([int]$num453 -eq [int]$num1) | ||
+ | </pre> | ||
+ | ==Add/Remove Network Interfaces== | ||
+ | The fun with this is that you can not remove a NIC from a running VM using powercli, or at least not in the way you would think. You actually need to rebuild the config with the changes you want then apply it back. Instead of doing this I decided to just disconnect the unwanted NIC and install the desired one. Thankfully this is in a DHCP environment so no modifications needed in the guest OS. | ||
+ | <br> | ||
+ | <br> | ||
+ | The reason for this change was due to Windows VMs losing connectivity to domain traffic using the emulated Intel NIC. According to our MS rep, we needed to change the NICs from e1000 to vmxnet3. | ||
+ | <pre> | ||
+ | connect-viserver -server localhost | ||
+ | $list=get-vm | ||
+ | foreach ($i in $list){ | ||
+ | $type=$i | Get-NetworkAdapter | select -ExpandProperty Type | ||
+ | if ($type -like "e1000"){ | ||
+ | $i | get-networkadapter -name "Network adapter 1" | set-networkadapter -connected $false -startconnected $false -confirm $false | ||
+ | $i | new-networkadapter -networkname VDI -StartConnected -type vmxnet3} | ||
+ | } | ||
</pre> | </pre> |
Revision as of 13:14, 10 July 2017
Installation
Allow powershell to run powercli cmdlets
Set-ExecutionPolicy RemoteSigned Install-Module -Name VMware.PowerCLI
Log into the server and start using commands
$cred=get-credential connect-viserver -server localhost -credential $cred get-vm
Clone VM
new-vm -name <new_machine_name> -vm <source_machine_name> -vmhost <ip or host name of the physical host, not the controller>
Delete VM
remote-vm -vm <namme_of_vm> -deletepermanently -confirm $false
Sample Script
format is <scriptname>.ps1 <number of VMs to make>
ie clone_vm.ps1 3
param([string]$num1) #Athenticate $cred=get-credential connect-viserver -server localhost -credential $cred #Check for enough space $neededspace=([int]$num1 * '<space needed for each VM>') $space12=(datastore -Name <datastore name> | Select-Object FreeSpaceGB | ForEach-Object { $_.FreeSpaceGB }) if ( $space12 -lt $neededspace ){ echo "Not enought space, $space12 is available and you need $neededspace" exit } #The following assumes you are creating VMs with an incrementing name based on a [a-z]+-[a-z]+-[0-9]+ format. #Get last VM created for incremental creation get-folder -name <folder name> | get-vm | select name > C:\powercli\outtest $vms=(Get-Content C:\powercli\outtest | %{"$($_.Split('-')[2])"}) $base=0 foreach($line in $vms){ if ([int]$line -gt [int]$base){ $base=$line } } $base = [int]$base + 1 $num453=0 Do{ $usehost=(Get-VMHost | Get-Random) new-vm -name vm-name-$base -vm clonefromthis -VMHost $usehost -RunAsync -WhatIf $num453=[int]$num453 + [int]1 }until ([int]$num453 -eq [int]$num1)
Add/Remove Network Interfaces
The fun with this is that you can not remove a NIC from a running VM using powercli, or at least not in the way you would think. You actually need to rebuild the config with the changes you want then apply it back. Instead of doing this I decided to just disconnect the unwanted NIC and install the desired one. Thankfully this is in a DHCP environment so no modifications needed in the guest OS.
The reason for this change was due to Windows VMs losing connectivity to domain traffic using the emulated Intel NIC. According to our MS rep, we needed to change the NICs from e1000 to vmxnet3.
connect-viserver -server localhost $list=get-vm foreach ($i in $list){ $type=$i | Get-NetworkAdapter | select -ExpandProperty Type if ($type -like "e1000"){ $i | get-networkadapter -name "Network adapter 1" | set-networkadapter -connected $false -startconnected $false -confirm $false $i | new-networkadapter -networkname VDI -StartConnected -type vmxnet3} }