Difference between revisions of "Powercli"

From UVOO Tech Wiki
Jump to navigation Jump to search
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
# Example Scripts
 +
https://github.com/vmware/PowerCLI-Example-Scripts/tree/master/Modules/Get-VMmaxIOPS
 +
 +
# Quick getters
 +
```
 +
get-vm | Select Name, UsedSpaceGB, ProvisionedSpaceGB
 +
```
 +
 +
 
# Install from powershell
 
# Install from powershell
 
- https://www.altaro.com/vmware/install-powercli-ubuntu-linux-18-04-lts/
 
- https://www.altaro.com/vmware/install-powercli-ubuntu-linux-18-04-lts/
Line 4: Line 13:
 
Find-Module -Name VMware.PowerCLI
 
Find-Module -Name VMware.PowerCLI
 
Install-Module -Name VMware.PowerCLI -Scope CurrentUser -AllowClobber
 
Install-Module -Name VMware.PowerCLI -Scope CurrentUser -AllowClobber
Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false
+
# Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false
 
Set-PowerCLIConfiguration -InvalidCertificateAction:Ignore # For self signed certs
 
Set-PowerCLIConfiguration -InvalidCertificateAction:Ignore # For self signed certs
 
Get-Command -Module *VMWare*
 
Get-Command -Module *VMWare*
 +
```
 +
 +
```
 +
$cred = Get-Credential; $cmd="hostname"; foreach($i in get-vm){ $os=$i.GuestId; if("$os" -like "*windows*"){Invoke-VMScript -vm $i.Name -ScriptText $cmd -ScriptType Powershell -GuestCredential $cred } }
 
```
 
```
  
Line 165: Line 178:
  
 
main
 
main
 
  
 
# NOTES ===============================================
 
# NOTES ===============================================
Line 172: Line 184:
 
```
 
```
  
 +
## Get snaps
 +
```
 +
$Report = Get-VM | Get-Snapshot | Select VM,Name,Description,@{Label="Size";Expression={"{0:N2} GB" -f ($_.SizeGB)}},Created
 +
If (-not $Report)
 +
{  $Report = New-Object PSObject -Property @{
 +
      VM = "No snapshots found on any VM's controlled by $VIServer"
 +
      Name = ""
 +
      Description = ""
 +
      Size = ""
 +
      Created = ""
 +
  }
 +
}
 +
# $Report = $Report | Select VM,Name,Description,Size,Created | ConvertTo-Html -Head $Header -PreContent "<p><h2>Snapshot Report - $VIServer</h2></p><br>" | Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd
 +
 +
echo $Report
 +
```
  
 
- https://www.altaro.com/vmware/use-invoke-vmscript-powercli-cmdlet/
 
- https://www.altaro.com/vmware/use-invoke-vmscript-powercli-cmdlet/
Line 177: Line 205:
 
```
 
```
 
Invoke-VMScript -VM <VM> -ScriptText <commands> -GuestUser <administrator> -GuestPassword <password> -ScriptType <Bat, PowerShell, Bash>
 
Invoke-VMScript -VM <VM> -ScriptText <commands> -GuestUser <administrator> -GuestPassword <password> -ScriptType <Bat, PowerShell, Bash>
 +
```
 +
 +
https://stackoverflow.com/questions/52219973/get-current-cpu-usage-using-powercli
 +
```
 +
$vmhost | Get-Stat -Stat cpu.usagemhz.average -Realtime -MaxSamples 1
 
```
 
```
  
 
# Ref
 
# Ref
 
- https://thesysadminchannel.com/install-vmware-powercli-module-powershell/
 
- https://thesysadminchannel.com/install-vmware-powercli-module-powershell/

Latest revision as of 15:53, 12 September 2022

Example Scripts

https://github.com/vmware/PowerCLI-Example-Scripts/tree/master/Modules/Get-VMmaxIOPS

Quick getters

get-vm | Select Name, UsedSpaceGB, ProvisionedSpaceGB

Install from powershell

Find-Module -Name VMware.PowerCLI
Install-Module -Name VMware.PowerCLI -Scope CurrentUser -AllowClobber
# Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false
Set-PowerCLIConfiguration -InvalidCertificateAction:Ignore # For self signed certs
Get-Command -Module *VMWare*
$cred = Get-Credential; $cmd="hostname"; foreach($i in get-vm){ $os=$i.GuestId; if("$os" -like "*windows*"){Invoke-VMScript -vm $i.Name -ScriptText $cmd -ScriptType Powershell -GuestCredential $cred } }
#!/usr/bin/env pwsh
$vCenters = @("v1", "v2")
Connect-VIServer -Server $vCenters
Get-VM myhost | Sort-Object | Get-View -Property @("Name", "Config.GuestFullName", "Guest.GuestFullName") | Select -Property Name, @{N="Configured OS";E={$_.Config.GuestFullName}},  @{N="Running OS";E={$_.Guest.GuestFullName}} | Format-Table -AutoSize
# Get-VM | Sort-Object | Get-View -Property @("Name", "Config.GuestFullName", "Guest.GuestFullName") | Select -Property Name, @{N="Configured OS";E={$_.Config.GuestFullName}},  @{N="Running OS";E={$_.Guest.GuestFullName}} | Format-Table -AutoSize

Simple Example Script(s)

Param(
   [string] [Parameter(Mandatory=$false)] $VmName,
   [switch] [Parameter(Mandatory=$false)] $GetCred,
   [switch] [Parameter(Mandatory=$false)] $GetVmDisk,
   [switch] [Parameter(Mandatory=$false)] $GetVmInfo,
   [switch] [Parameter(Mandatory=$false)] $InvokeCmdAllVms,
   [switch] [Parameter(Mandatory=$false)] $SetVmDisk
)
if ($GetCred){
  $Cred = Get-Credential
}
$minDiskSize = 80  # In GBs
$ErrorActionPreference = 'Stop'

$vCenters = @("vCenter1", "vCenter2")
Connect-VIServer -Server $vCenters


function getGuestOs($vmGuest){
  if ( $vmGuest -Like "*Windows*" ) {
    # Write-Host "vm: $vm os: $vmGuest is Windows"
    return "Windows"
  } elseif ( $vmGuest -Like "*Linux*" ) {
    # Write-Host "vm: $vm os: $vmGuest is Linux"
    return "Linux"
  } else {
    # Write-Host "$vm is Undetected/Unsupported.\n"
    return "Undetected"
  }
}


function guestIsWindows($vmGuest){
  if ( $vmGuest -Like "*Windows*" ) {
    Write-Output "$vmGuest is Windows"
    return $true
  } else {
    return $false
  }
}


function guestIsLinux($vmGuest){
  Write-Output $vmGuest
  if ( $vmGuest -Like "*Linux*" ) {
    return $true
  } else {
    return $false
  }
}


function resizeDiskToMaxSize ($name){
  Invoke-Command -ComputerName $name -ScriptBlock { Update-Disk -Number 0; Resize-Partition -DriveLetter C -Size $(Get-PartitionSupportedSize -DriveLetter C).SizeMax} -cred $cred
}


function Invoke-Command-On-All-VMs($cmd) {
  # $vmlist = Get-Cluster $vCluster | Get-VM
  $vmlist = Get-VM
  # | Select @{N="IP Address";E={@($_.guest.IPAddress[0])}}

  foreach ($vm in $vmlist) {
    # $vm = Get-VM $vm | Select-Object -Property *
    $vmGuest = $vm.Guest
    $vmPowerState = $vm.PowerState
    Write-Host "$vm $vmGuest $vmPowerState"
    $os = getGuestOs($vmGuest)
    if ($os -eq "Windows"){
      Write-Output "OS is Windows."
      try {
        $r = Invoke-VMScript -vm $vm -ScriptText $cmd -ScriptType Powershell -GuestCredential $Cred
        Write-Output $r
      } catch { $e = $_.Exception.Message; Write-Output $e }
    } elseif ($os -eq "Linux"){
      Write-Output "OS is Linux."
      try {
        $r = Invoke-VMScript -vm $vm -ScriptText $cmd -ScriptType Bash -GuestCredential $Cred
        Write-Output $r
      } catch { $e = $_.Exception.Message; Write-Output $e }
    } else {
      Write-Output "W: OS is unsupported."
    }
    Start-Sleep 5
  }
}


function Get-vmInfo ($vmName) {
  $vm = Get-VM -Name $vmName | Select-Object -Property *
  Write-Output $vm
  $vmDisk = Get-VM $vmName | Get-Harddisk | Select-Object -Property *
  Write-Output $vmDisk
}


function Get-vmDisk ($vmName) {
  $vm = Get-VM -Name $vmName | Select-Object -Property *
  # $vmDisk = Get-HardDisk -VM $vm
  $vmDisk = Get-VM $vmName | Get-Harddisk | Select-Object -Property *
  if ( $vmDisk.CapacityGB -lt $minDiskSize ) {
    Write-Output "W: $vmName current size $($vmDisk.CapacityGB) GB is < than min $minDiskSize GB"
    if (guestIsWindows($vm.Guest)){
      Write-Output "OS is Windows."
    }
  } else {
    Write-Output "I: $vmName current size $($vmDisk.CapacityGB) GB is > than min $minDiskSize GB"
  }
}


function SetIncreaseVmDiskToMinSize($vmName){
  $vmDisk = Get-VM -Name $vmName | Get-Harddisk | Select-Object -Property *
  Write-Output $vmDisk.CapacityGB
  if ( $vmDisk.CapacityGB -lt $minDiskSize ) {
    Write-Output "W: $vmName current size $($vmDisk.CapacityGB) GB is < than min $minDiskSize GB"
    Get-HardDisk -VM $vm | Set-HardDisk -CapacityGB $minDiskSize
    if (guestIsWindows($vm.Guest)){
      Write-Output "Expand $vmName drive C to max partition size."
      $script0 = "Update-Disk -Number 0; Resize-Partition -DriveLetter C -Size $(Get-PartitionSupportedSize -DriveLetter C).SizeMax"
      Invoke-VMScript $script0 -vm $vmName -GuestCredential $Cred
    }
  } else {
    Write-Output "I: $vmName current size $($vmDisk.CapacityGB) GB is > than min $minDiskSize GB"
  }
}


function main(){
  if ($GetVmDisk) {
    Get-VmDisk($vmName)
  }
  if ($GetVmInfo) {
    Get-VmInfo($vmName)
  }
  if ($SetVmDisk) {
    SetIncreaseVmDiskToMinSize($vmName)
  }
  if ($InvokeCmdAllVms) {
    Invoke-Command-On-All-VMs("hostname")
    # Invoke-Command-On-All-VMs("pwd; ls")
  }
}


main

# NOTES ===============================================

# Register-DNSClient

Get snaps

$Report = Get-VM | Get-Snapshot | Select VM,Name,Description,@{Label="Size";Expression={"{0:N2} GB" -f ($_.SizeGB)}},Created
If (-not $Report)
{  $Report = New-Object PSObject -Property @{
      VM = "No snapshots found on any VM's controlled by $VIServer"
      Name = ""
      Description = ""
      Size = ""
      Created = ""
   }
}
# $Report = $Report | Select VM,Name,Description,Size,Created | ConvertTo-Html -Head $Header -PreContent "<p><h2>Snapshot Report - $VIServer</h2></p><br>" | Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd

echo $Report
Invoke-VMScript -VM <VM> -ScriptText <commands> -GuestUser <administrator> -GuestPassword <password> -ScriptType <Bat, PowerShell, Bash>

https://stackoverflow.com/questions/52219973/get-current-cpu-usage-using-powercli

$vmhost | Get-Stat -Stat cpu.usagemhz.average -Realtime -MaxSamples 1

Ref