Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Where to start with power cli

  • Login to see the comments

  • Be the first to like this

Where to start with power cli

  1. 1. Chris Halverson
  2. 2. Why use PowerCLI The biggest question to ask is: Why would I use PowerShell (PoSH) if thereis a perfectly good Graphical User Interface? When there is Administration of Multiplesystems there is always a need forscripting. Basic Example:○ Get a list of all VM, just by their names
  3. 3. Script and OutputGet-VM | select NameName====Server1Server2Server3Server4Server5Server6Server7…
  4. 4. Real World Scenario Joe is an Administrator at a small Oiland Gas company that has 4 ESXiservers and 120 VMs running on them. Joe’s manager requests a report for alist of Servers in the environment, withthe amount of CPUs, Memory, DiskSpace used. “Piece of Cake” Joe says as he goes tothe VIClient to pull a report from theGUI.
  5. 5. Using the GUI Joe selects Inventory Hosts and Clusters,Selects the vCenter hostand then selects the“Virtual Machines” Tab. He sorts the VirtualMachines by name andexamines what fields areable to be displayed.
  6. 6. Shortcomings of Using the GUI Joe assesses that using the GUI can bedone but he knows his manager and hewill ask for this report again, and again,and again, and he would like to eitherautomate this or have this running via asingle script. Joe also wants to exclude certainservers as they are test machines andare not needed for this report for hismanager. (Names Start with Test)
  7. 7. Attempting the first scriptNeeds:Name, vCPUs, Memory and Disk UsedScript:Get-VM | Select Name, NumCPU, MemoryGB,UsedDiskGB
  8. 8. First Script OutputOutput:Name NumCPU MemoryGB UsedDiskGB==== ====== ======== ==========Server1 2 4.0000008 31.0103883Server2 4 7.999293 48TestServer1 1 1.9199201 48 As you can see the script produced theresults we were looking for, however withsome mixed output results, and what aboutthe exclusion of Test machines.
  9. 9. First Script RevampScript:Get-VM | Select Name, NumCPU,@{Name=“Memory”;Expression={[System.Math]::Round($_.MemoryGB, 2)}}, UsedSpaceGBExplanation: Okay, this looks complicated but when you break it down it isvery similar to the first attempt, but we are naming theMemoryGB – “Memory” and using a system method using aMath Function to Round the variable to 2 decimal points. We could also do this to the UsedSpaceGB to make it easierto read.
  10. 10. First Script Revamp OutputOutput:Name NumCPU Memory UsedDiskGB==== ====== ======== ==========Server1 2 4 31.0103883Server2 4 7.99 48TestServer1 1 1.91 48 As you can see the script produced theresults we were looking for, however withsome mixed output results, and what aboutthe exclusion of Test machines.
  11. 11. First Script Revamp (part 2)Script:Get-VM | where{$_.Name –notmatch “Test”} | SelectName, NumCPU, @{Name=“Memory”; Expression={[System.Math]::Round($_.MemoryGB, 2)}},@{Name=“Used Disk”; Expression={[int]$_.UsedSpaceGB}}Explanation: We added two additional components to this one First to remove the VMs with the name Test within it. Second to format the usedSpaceGB to a number with no decimal places.
  12. 12. Script Revamp Output (2)Output:Name NumCPU Memory Used Disk==== ====== ======== ==========Server1 2 4 31Server2 4 7.99 48Notice the formatting is goodthe test server is removedbalance to the force has been restored.
  13. 13. Now for Something Useful Who has to deal with aging snapshotswhen other administrators in your groupthink that a snapshot is a backup? Your datastores show a red exclamationmark and seem to be almost full??? Script:The easiest would be to blanket remove all thesnapshots via this scriptGet-VM | Get-Snapshot | Remove-Snapshot
  14. 14. Snapshot Script The problem with this is that it may takeDAYS to reconcile the snapshots and youwant to see how bad the damage is beforeyou run this. So you run a variation on the original scriptto firstly produce a list and size of thesnapshots and secondly limit the scope ofreconciliation.Get-VM | Get-Snapshot | Where {$_.SizeGB -lt 1 -and$_.Created.Day -lt (Get-Date).Day}
  15. 15. Snapshot Script OutputScript:Get-VM | Get-Snapshot | Where {$_.SizeGB -lt 1 -and$_.Created.Day -lt (Get-Date).Day}Output:Name Description PowerState---- ----------- ----------Test PoweredOff Here is an example of the typical output from thatscript. You can pull the information about theSnapshot by adding a “| fl *” after it. Thisdisplays all the information available (fl is an aliasfor Format-List)
  16. 16. Where to go next I started by using Powershell.exe as mymain command line interpreter, instead ofcmd.exe. I also tried to use the command linewherever possible to ensure memory of thecommands Document your findings! Maintain a script repository with specificnaming. Native commands use Get, Newand Set, consider using different verbs todifferentiate your scripts (Do, Put, orChange)
  17. 17. Questions?Where do YOU want to go next?1 Liners Link