jeudi 18 janvier 2018

powercli - Reference




Connexion avec le user courant:
Connect-VIServer 192.168.0.12

Conexion avec user explicite:
Connect-VIServer vc.domain.local -user readonly@domain.local -pass xxxxxx

Connexion avec menu
Connect-VIServer 192.168.0.12 -menu

Deconnexion forcée de tous les VC
Disconnect-VIServer -Server * -force

Gérer des credentials
New-VICredentialStoreItem -host xxxx -user Admin -Password xxxxxx
(Get-

(Remove-

Voir les connexions utilisées lors d'une session
$DefaultVIServers

---------------

get-vm -name *exch*
get-vm -name * | Out-GridView
get-vm -name SRVxxx | fl

get-vm | Where-Object{$_.Powerstate -eq "PoweredOff"} | Start-VM

get-VM vSRVxxx| Stop-VMguest

get-cluster | Get-VM | select name, @{N="Datastore";E={Get-Datastore -VM $_}} | sort name

Get-VM | Select Name, @{N="Network Adapter";E={$_.NetworkAdapters| foreach-object {$_.Type}}}, @{N="MacAddress";E={$_.NetworkAdapters| ForEach-Object {$_.MacAddress}}}, @{N="PortGroup";E={Get-VirtualPortGroup -VM $_}}

Get-VMHost | Sort-Object Name | Select Name,@{N="VM";E={ if ($_.ExtensionData.Vm -ne $null) { $_.ExtensionData.Vm.Count } else {0}}}

Get-Cluster "clustername" | get-vmhost "hostname" | get-vm | Get-NetworkAdapter | Format-Table parent, networkname, type, MACaddress

To create table with renamed property name, use @{Name="New_Name";Expression={$_.Old_Name}}
Like in above script:

Get-Cluster "clustername" | get-vmhost "hostname" | get-vm | Get-NetworkAdapter | Format-Table @{Name="VirtualMachine";Expression={$_.parent}}, @{Name="VLAN";Expression={$_.networkname}}

To list all harddisks of a VM with size,
Get-VM VM01 | Get-HardDisk | ft Parent, Name, Filename, CapacityKB –AutoSize

And if you want to export this to a CSV file then,
Get-VM VM01 | Get-HardDisk | Select Parent, Name, Filename, CapacityKB | Export-csv C:\file.csv

We can't export output of Format-Table cmdlet to CSV file so we need to use Select-Object instead.

Add PortGroup to all hosts in cluster with PowerCLI
Get-Cluster “clustername” | Get-VMHost | Get-VirtualSwitch -Name “vSwitch_name? | New-VirtualPortGroup -Name “VLAN-xx” -VLanId xx

Check Multi-path policy/configuration of connected storage,
Get-VMHost | Get-ScsiLun | Select VMHost, ConsoleDeviceName, Vendor, MultipathPolicy, LunType

To get list of VMs with snapshot,
Get-VM | Get-Snapshot | Select VM,Name,Created,sizemb

List of VMs having snapshot older than 3 days,
Get-VM | Get-Snapshot | Select VM,Name,Created,sizemb | Where {$_.Created -lt ((Get-Date).AddDays(-2))}

To remove snapshot older than 3 days,
Get-Snapshot | Select VM,Name,Created,sizemb | Where {$_.Created -lt ((Get-Date).AddDays(-2))} | Remove-Snapshot

To list time on all VMhosts,
Get-VMHost | sort Name | select Name,@{Name="Current VMHost Time";Expression={(Get-View
$_.ExtensionData.ConfigManager.DateTimeSystem).QueryDateTime()}}

To find a list of VMs having sync time with host option selected,
Get-View -viewtype virtualmachine -Filter @{'Config.Tools.SyncTimeWithHost'='True'} | select name


----------------------------

Mettre à jour les VmWareTools

vm = "vsrvtest"
**** deconnecter CD
get-vm $vm | get-cddrive | set-cddrive -nomedia -confirm:$false

PS C:\Users\plop> Get-CDDrive $vm | Set-CDDrive -IsoPath "[Datastore] vmware-tools-10.1.7/windows-vmwaretools-10.1.7.iso" -connected:$true -confirm:$false

Confirm
Are you sure you want to perform this action?
Performing the operation "Setting IsoPath: [Datastore] vmware-tools-10.1.7/windows-vmwaretools-10.1.7.iso,
Connected: True, NoMedia: False." on target "CD/DVD drive 1".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): Y

IsoPath HostDevice RemoteDevice
------- ---------- ------------
[Datastore] vm...



PS C:\Users\plop>

PS C:\Users\plop> $DriveLetter = Get-WmiObject Win32_CDROMDrive -ComputerName $vm -credential domain\great.adm | Where-Object {$_.VolumeName -match “VMware Tools”} | Select-Object -ExpandProperty Drive

PS C:\Users\plop> $DriveLetter
M:



PS C:\Users\plop> $ScriptText = "$DriveLetter\setup64.exe /S /v `"/qn /l*v ""%temp%\vmtoolupgrade.log"" REBOOT=R ADDLOCAL=ALL REMOVE=Hgfs`""

PS C:\Users\plop> Invoke-VMScript -VM $vm -GuestUser domain\great.adm -GuestPassword xxxxx -ScriptText $ScriptText -ScriptType bat
WARNING: The version of VMware Tools on VM 'vsrvtest' is out of date and may cause Invoke-VMScript to work improperly.