23 C
Dubai
Wednesday, December 18, 2024

Vmware PowerCli Cheat Sheet – Daily Administration

NOTE _ Use powercli with proper understanding.For Example Get-vm | Set-VM , Pipeline Retrieves all the VMs and Applies to all vms.

Connecting to Vcenter –  ( Passes Credentials in Plain Text)

Two ways – One liner if your password doesn’t contain contradicting letters with powercli like $.

Connect-VIServer -Server vcenter.careexchange.in -Protocol https -User "care\manoharan" -Password Welcome..@01234

image

or Store cred to Variable and pass it. (Passes Credential Securely)

$cred = Get-Credential

Enter UPN – manoharan@careexchange.in

Enter Password – Welcome..@01234

Connect-VIServer -Server vcenter.careexchange.in -Protocol https -credential $cred

Loading ISO –

Get-VM “VMNAME” | Get-CDDrive | Set-cddrive -ISOPath "[DATASTORE-OS] ISOs/Win2012R2.iso" -StartConnected $true

Unloading ISO –

Get-VM “VMNAME” | Get-CDDrive | Set-CDDrive -NoMedia

To See How Machines have loaded with ISOs Unnecessarily –

Get-VM | Get-CDDrive | where-object{$_.isopath -notlike $null} | FT Parent,Isopath –AutoSize

image

Adding Notes to Mulitple Virtual Machines using CSV –

CSV looks like below

image

Import-Csv vms.csv | ForEach-Object{Set-VM $_.Name -Notes $_.Description -Confirm:$false}

To Check SSH and Esxi Shell Status –

Get-VMHost 10.* | Get-VMHostService | Where-Object {$_.Key -like "TSM*"}

TSM-SSH – Denotes Esxi SSH

TSM – – Denotes Esxi Shell

image

To Set – Without Confirmation – Add End of the command –     -Confirm:$false

Enabling SSH And Shell –

Get-VMHost 10.* | Get-VMHostService | Where-Object {$_.Key -like "TSM*"}| Set-VMHostService -policy "on"

Restarting VMHostService on Esxi for SSH and Shell to refresh

Get-VMHost 10.* | Get-VMHostService | Where-Object {$_.Key -like "TSM*"}| Restart-VMHostService

Disabling IPv6 without logging into Esxi

login to Esxi using putty.

esxcli system module parameters set -m tcpip4 -p ipv6=0

image

Disabling ipv6 – requires reboot

Get-VMHost "VMHostname" | Restart-VMHost

Assigning TAGS –

Name Category Description
Low WindowsUpdates Servers can be Rebooted any time for Maintenance – Normal Change Process
Medium WindowsUpdates Servers can be Rebooted agreeing with the owner – Normal Change Process
High WindowsUpdates Servers can be Rebooted only with Scheduled Change – Critical Change Process 
Team1 Ownership ServiceDesk Team
Team2 Ownership Architects Team
Team3 Ownership Network Team

Two Categories –

  • Ownership
  • WindowsUpdates

Each Categories have Tags. VMs can have only one tag from each category. Team1 = can be NetworkTeam

Getting Tag of a Virtual Machine – or use Web Client to manage tags using GUI

Get-VM VMName | Get-TagAssignment

image

Assigning Tags to VMs –

Get-VM VMName | New-TagAssignment –Tag (get-tag “tagname”)

To see Tags from Each Category –

Get-VM VMName | Get-TagAssignment -Category Windowsupdates

To Remove a Tags from a VM – Specific Category –

Get-VM VMName | Get-TagAssignment -Category Windowsupdates | Remove-TagAssignment

image

Get UUID for a Specific VMHost – (Used for Red hat licensing Servers -Satellite servers) 

(Get-VMHost 172.21.1.2 | Get-View).hardware.systeminfo.uuid

4c4c9944-0994-5710-9991-b3c04f4a999a

Now to Export my Data –

If you name the Categories like I did. My script will work or modify Ownership =,Windowsupdates =  lines in the script.

image

Write-host "Saving CSV Variable"
$ExportPath = "Vms.csv"
Write-host "Saved CSV Variable"
Write-host "Saving ALL VMs"
$VMs = Get-VM
Write-host "Saved ALL VMs"
$VCenter = @()
Write-host "Entering Loop"
foreach ($vm in $VMs)
{
Write-host "Collecting $VM info"
$HostServer = $vm.vmhost.Name
$VMSysInfo = Get-VMGuest -VM $VM
$MyObject = New-Object PSObject -Property @{
VMName = $VM.name
VMHostName = $VMSysInfo.HostName
VMIP = $VMSysInfo.IPAddress
VMInstalledOS = $VMSysInfo.OSFullName
PowerState = $vm.powerstate
HostServer = $HostServer
HostCluster = (Get-Cluster -VMHost $HostServer).Name
Notes = $vm.notes
Ownership = ($vm | Get-TagAssignment | Where-Object {$_.Tag -like "Ownership/*"}).tag.name
Windowsupdates = ($vm | Get-TagAssignment | Where-Object {$_.Tag -like "Windowsupdates/*"}).tag.name
}
$VCenter += $MyObject
}
$VCenter |
Select VMName,
VMHostName,
@{N='VMIPAddress';E={$_.VMIP -join '; '}},
VMInstalledOS,
PowerState,
HostServer,
HostCluster,
Notes,
Ownership,
Windowsupdates |
Export-Csv $ExportPath –NoTypeInformation

Helpful report – VMWare Esxi Compliance Report – To check ipv6,hostinfo,ssh,shell status etc.

https://www.azure365pro.com/vmware-esxi-compliance-report/

Satheshwaran Manoharan
Satheshwaran Manoharanhttps://www.azure365pro.com
Award-winning Technology Leader with a wealth of experience running large teams and diversified industry exposure in cloud computing. From shipping lines to rolling stocks.In-depth expertise in driving cloud adoption strategies and modernizing systems to cloud native. Specialized in Microsoft Cloud, DevOps, and Microsoft 365 Stack and conducted numerous successful projects worldwide. Also, Acting as a Technical Advisor for various start-ups.

Related Articles

3 COMMENTS

  1. Great Article, Thank you! In its present form connecting to VI Server or host with this PowerShell code could present a security risk since it passes in clear text as you depicted. I would suggest revamping the PS code to something more elegant:

    PS > Connect-VIServer -Credential (Get-Credential)

    This will pop up a login box where to supply your credentials.

    Just a thought. =0)

    • True. Yes. thanks for the thought.
      Useful.

      In my case – I always use the jump server within the secure network.

      Updated the post .

  2. Hi,

    I’m trying to list VMs that have the CD/DVD drive set to “Datastore ISO File”, but only those who doesn’t have the ISO “Connected”

    This lists the VMs with the CD/DVD drive set to “Datastore ISO File”:
    Get-VM | Get-CDDrive | Where {$_.ISOPath -ne $null} | Select Parent, ConnectionState | FT -AutoSize

    But this lists all VMs with the CD Drive set to “Datastore ISO File”, even if it’s Connected or NotConnected,
    I only want the script to list those VMs that has the CD Drive set to “Datastore ISO File” AND are NotConnected. Please advise.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

× How can I help you?