Powercli

From roonics
Revision as of 08:23, 20 March 2022 by Jlambert (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Powercli

Useful Commands

Create credential store

New-VICredentialStoreItem -Host <vcenter-hostname_or_vcenter-ip> -User "<username>" -Password "<password>"

Update vmTools with no reboot

Get-VM "<vm_name>" | Update-Tools -NoReboot

Create snapshot

new-snapshot -vm <vm_name> -name snapshot_test

Remove snapshot

get-snapshot -vm <vm_name> | remove-snapshot

vMotion

get-vm -Name <vm_name> | move-vm -Destination <hostname_or_host_ip>

Storage vMotion

Get-VM "<vm_name>" | Move-VM -Datastore <datastore_name>

List all virtual machines with snapshots

get-vm | get-snapshot | format-list vm,name

Example output:

VM   : webserver01
Name : pre Windows updates

VM   : ad01
Name : pre disk expansion

VM   : exchange01
Name : pre database move

List all virtual machines with snapshots older than 3 days

Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-3)} | Select-Object VM, Name, Created, SizeMB

List vmx file locations for vm's

Get-View -ViewType VirtualMachine | % { $_.Config.Files.VmPathName }

Example output:

[iscsi-datastore-01] DATABASE/DATABASE.vmx
[iscsi-datastore-02] webserver/webserver.vmx
[fc-datastore-01] ad01/ad01.vmx
[fc-datastore-02] exchange01/exchange01.vmx

Updated command with better formatting:

Get-VM | select Name,@{E={$_.ExtensionData.Config.Files.VmPathName};L=”VM Path”}

Example output:

Server01          [ISCSI-STORAGE01] Server01/Server01.vmx
Server02          [ISCSI-STORAGE04] Server02/Server02.vmx
Server03          [ISCSI-STORAGE06] Server03/Server03.vmx
Server04          [FC-STORAGE01] Server04/Server04.vmx

Get host boot up time/date

$esxis = Get-VMHost
Get-ViEvent -entity $esxis -Start (Get-Date "10:00 10/09/2016") -Finish (Get-Date "15:00 10/09/2016") -MaxSamples ([int]::MaxValue) | Where {$_.FullFormattedMessage -eq "Host has booted."}

Example output:

EventTypeId          : esx.audit.host.boot
Severity             :
Message              :
Arguments            :
ObjectId             : host-155553
ObjectType           : HostSystem
ObjectName           : vsphere-host-01
Fault                :
Key                  : 117978286
ChainId              : 117978286
CreatedTime          : 10/09/2016 13:11:21
UserName             :
Datacenter           : VMware.Vim.DatacenterEventArgument
ComputeResource      : VMware.Vim.ComputeResourceEventArgument
Host                 : VMware.Vim.HostEventArgument
Vm                   :
Ds                   :
Net                  :
Dvs                  :
FullFormattedMessage : Host has booted.
ChangeTag            :
DynamicType          :
DynamicProperty      :

List virtual machines with RDM disks

Get-VM | Get-HardDisk -DiskType "RawPhysical","RawVirtual" | Select Parent,Name,DiskType,ScsiCanonicalName,DeviceName | fl

Example output:

Parent            : webserver01
Name              : Hard disk 3
DiskType          : RawPhysical
ScsiCanonicalName : naa.6019cb61f5397ffc45a0959620009933
DeviceName        : vml.02000000006019cb61f5397ffc45a0959620009933313030452d30

Parent            : exchange01
Name              : Hard disk 2
DiskType          : RawPhysical
ScsiCanonicalName : naa.68b7b2cc76b0e016966ec56d600540b1
DeviceName        : vml.020000000068b7b2cc76b0e016966ec56d600540b1313030452d30

Start virtual machine

start-vm <virtual machine name>

Shutdown virtual machine

shutdown-vmguest <virtual machine name> -confirm:$false

Restart virtual machine

restart-vm <virtual machine name>

Get VM and ESX Events Between Hours

Get just host entries:

$esxs = Get-VMHost                                                                                      
$start = Get-Date "12:00 28/09/2015"
$finish = Get-Date "18:00 29/09/2015"
$events = Get-ViEvent -entity $esxs -Start $start -Finish $finish -MaxSamples ([int]::MaxValue)         
$events | Select CreatedTime, UserName, ObjectName, EventTypeId, FullFormattedMessage | out-gridview

Get both host and vm entries:

$objs = Get-VMHost                                                                                      
$objs += Get-VM                                                                                         
$start = Get-Date "12:00"                                                                               
$finish = Get-Date "18:00"
$events = Get-ViEvent -entity $esxs -Start $start -Finish $finish -MaxSamples ([int]::MaxValue)         
$events | Select CreatedTime, ObjectName, EventTypeId, FullFormattedMessage | out-gridview

Rescan HBA/VMFS on all hosts in cluster

Get-Cluster -Name "CLUSTER01" | Get-VMHost | sort-object name | Get-VMHostStorage -RescanAllHba -RescanVmfs | out-null

List all vms on cluster and what datastore they are on

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

Example output:

Name                               Datastore
----                               ---------
Server01                         iscsi_datastore01
Server02                         iscsi_datastore02
Server03                         fc_datastore01
Server04                         iscsi_datastore03
Server05                         fc_datastore03

Enable SSH on all hosts in a cluster

Get-cluster -name "CLUSTER01" | Get-VMHost | Foreach {Stop-VMHostService -HostService ($_ | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"} )}

Example output:

Key                  Label                          Policy     Running  Required
---                  -----                          ------     -------  --------
TSM-SSH              SSH                            off        True     False
TSM-SSH              SSH                            off        True     False
TSM-SSH              SSH                            off        True     False

Find how many virtual machines on each datastore

Get-Datastore | Select Name, @{N="NumVM";E={@($_ | Get-VM).Count}} | Sort Name

Example output:

Name                             NumVM
----                             -----
iscsi_datastore01                   2
iscsi_datastore02                   4
fc_datastore01                      4
iscsi_datastore03                   4
fc_datastore02                      0
iscsi_datastore04                   0

Get ESX software version

 get-vmhost -name esxserver0* | %{(Get-View $_.ID).Config.Product}

Example output:

Name                  : VMware ESXi
FullName              : VMware ESXi 5.5.0 build-4345813
Vendor                : VMware, Inc.
Version               : 5.5.0
Build                 : 4345813
LocaleVersion         : INTL
LocaleBuild           : 000
OsType                : vmnix-x86
ProductLineId         : embeddedEsx
ApiType               : HostAgent
ApiVersion            : 5.5
InstanceUuid          :
LicenseProductName    : VMware ESX Server
LicenseProductVersion : 5.0

Get configured VLANs on ESX server

Get-VMHost esxserver01 | Get-VirtualPortGroup | Sort VLanId | Select VLanId, Name

Example output:

VLanId Name
------ ----
777    Management
2      Backup
6      Prod-traffic
34     DMZ
22     Test

Get HA Cluster primaries

 Get-Cluster | Get-HAPrimaryVMHost | Select Name

Example output:

Name
----
labesx01.lab.local
dmzlabesx01.lab.local
prodesx01.lab.local

Run a Powershell command inside a virtual machine

Invoke-VMScript -VM server01 -ScriptText "dir C:\"

Example output:


ScriptOutput
------------------------------------------------------------------------------
|
|
|      Directory: C:\
|
|
|  Mode                LastWriteTime     Length Name
|  ----                -------------     ------ ----
|  d----        30/04/2019     05:58            APPS
|  d----        01/10/2017     23:23            info
|  d----        23/10/2017     09:47            Nmap
|  d----        22/08/2013     16:52            PerfLogs
|  d-r--        28/06/2019     17:22            Program Files
|  d----        15/02/2019     16:24            Program Files (x86)
|  d----        21/05/2018     23:59            Quarantine
|  d----        08/09/2019     06:00            Temp
|  d-r--        11/09/2019     12:27            Users
|  d----        29/07/2019     18:30            Windows
|  d----        15/02/2019     16:11            WINNT
|
|
|
------------------------------------------------------------------------------

How many vms on each host

Get-VMHost | Select @{N=“Cluster“;E={Get-Cluster -VMHost $_}}, Name, @{N=“NumVM“;E={($_ | Get-VM).Count}} | Sort Cluster, Name

Example output:


Cluster                    Name                         NumVM
-------                    ----                         -----
ESXI-CLUSTER-01            esxiserver01.local           10
ESXI-CLUSTER-02            esxiserver02.local           2
ESXI-CLUSTER-03            esxiserver03.local           6
ESXI-CLUSTER-04            esxiserver04.local           7
ESXI-CLUSTER-05            esxiserver05.local           1

List virtual machines used disk space rounded

get-vm | Select-Object Name, @{n="Usedspace(GB)"; E={[math]::round($_.UsedSpaceGB)}}

Example output:

Name                                            Usedspace(GB)
----                                            -------------
Server01                                          1629
Server02                                          58
Server03                                          48
Server04                                          63
Server05                                          35

ESX IP Addresses

Provides a dump of all IP addresses for an ESX (useful for finding the ESX host from its iSCSI IP address)

Get-Datacenter cluster01 | Get-Cluster | Get-VMHost | Get-VMHostNetworkAdapter | Select -ExpandProperty IP

List all virtual machines with thick provisioned disks

Get-Datastore | Get-VM | Get-HardDisk | Where {$_.storageformat -eq "Thick" } | Select Parent, Name, CapacityGB, storageformat | FT -AutoSize

Example output:

Parent          Name                       CapacityGB StorageFormat
------          ----                       ---------- -------------
Server01        Hard disk 4                       500         Thick
Server02        Hard disk 2                       300         Thick
Server03        Hard disk 3                       100         Thick
Server04        Hard disk 4                        50         Thick

Supress the SSH is enabled warning on hosts

Get-VMHost | Get-AdvancedSetting UserVars.SuppressShellWarning | Set-AdvancedSetting -Value 1

Enable SSH on all hosts in a cluster

Get-Cluster -name <CLUSTER> | Get-VMHost | ForEach {Start-VMHostService -HostService ($_ | Get-VMHostService | Where {$_.Key -eq "TSM-SSH"})}

Check progress of a task running on a virtual machine

Get-Task | where{$_.ObjectId -match '^VirtualMachine'} | Select StartTime,PercentComplete,State,@{N='server01';E={Get-View -Id $_.ObjectId -Property Name | select -ExpandProperty Name}},Description

Example output:

StartTime       : 8/4/2021 6:36:39
PercentComplete : 100
State           : Success
Server          : server01
Description     : Consolidate virtual machine disk files

‎<comments />