Sunday , June 25 2017

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.

http://www.careexchange.in/vmware-esxi-compliance-report/

About Satheshwaran Manoharan

Satheshwaran Manoharan is an Microsoft Exchange Server MVP , Publisher of CareExchange.in Supporting/Deploying/Designing Microsoft Exchange for some years. Extensive experience on Microsoft Technologies.

Check Also

SCCM Updates and Services Keep Downloading

Log Files Location – C:\Program Files\Microsoft Configuration Manager\Logs\dmpdownloader.log — Logs –  Failed to call Initialize. ...

2 comments

  1. Myfirstname Mylastname VII

    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)

Leave a Reply

Your email address will not be published.