Chris Halverson
Why use PowerCLI
 The biggest question to ask is:
 Why would I use PowerShell (PoSH) if there
is a perfectly good Graphical User Interface?
 When there is Administration of Multiple
systems there is always a need for
scripting.
 Basic Example:
○ Get a list of all VM, just by their names
Script and Output
Get-VM | select Name
Name
====
Server1
Server2
Server3
Server4
Server5
Server6
Server7
…
Real World Scenario
 Joe is an Administrator at a small Oil
and Gas company that has 4 ESXi
servers and 120 VMs running on them.
 Joe’s manager requests a report for a
list of Servers in the environment, with
the amount of CPUs, Memory, Disk
Space used.
 “Piece of Cake” Joe says as he goes to
the VIClient to pull a report from the
GUI.
Using the GUI
 Joe selects Inventory 
Hosts and Clusters,
Selects the vCenter host
and then selects the
“Virtual Machines” Tab.
 He sorts the Virtual
Machines by name and
examines what fields are
able to be displayed.
Shortcomings of Using the GUI
 Joe assesses that using the GUI can be
done but he knows his manager and he
will ask for this report again, and again,
and again, and he would like to either
automate this or have this running via a
single script.
 Joe also wants to exclude certain
servers as they are test machines and
are not needed for this report for his
manager. (Names Start with Test)
Attempting the first script
Needs:
Name, vCPUs, Memory and Disk Used
Script:
Get-VM | Select Name, NumCPU, MemoryGB,
UsedDiskGB
First Script Output
Output:
Name NumCPU MemoryGB UsedDiskGB
==== ====== ======== ==========
Server1 2 4.0000008 31.0103883
Server2 4 7.999293 48
TestServer1 1 1.9199201 48
 As you can see the script produced the
results we were looking for, however with
some mixed output results, and what about
the exclusion of Test machines.
First Script Revamp
Script:
Get-VM | Select Name, NumCPU,
@{Name=“Memory”;Expression={[System.Math]::Round(
$_.MemoryGB, 2)}}, UsedSpaceGB
Explanation:
 Okay, this looks complicated but when you break it down it is
very similar to the first attempt, but we are naming the
MemoryGB – “Memory” and using a system method using a
Math Function to Round the variable to 2 decimal points.
 We could also do this to the UsedSpaceGB to make it easier
to read.
First Script Revamp Output
Output:
Name NumCPU Memory UsedDiskGB
==== ====== ======== ==========
Server1 2 4 31.0103883
Server2 4 7.99 48
TestServer1 1 1.91 48
 As you can see the script produced the
results we were looking for, however with
some mixed output results, and what about
the exclusion of Test machines.
First Script Revamp (part 2)
Script:
Get-VM | where{$_.Name –notmatch “Test”} | Select
Name, 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.
Script Revamp Output (2)
Output:
Name NumCPU Memory Used Disk
==== ====== ======== ==========
Server1 2 4 31
Server2 4 7.99 48
Notice the formatting is good
the test server is removed
balance to the force has been restored.
Now for Something Useful
 Who has to deal with aging snapshots
when other administrators in your group
think that a snapshot is a backup?
 Your datastores show a red exclamation
mark and seem to be almost full???
 Script:
The easiest would be to blanket remove all the
snapshots via this script
Get-VM | Get-Snapshot | Remove-Snapshot
Snapshot Script
 The problem with this is that it may take
DAYS to reconcile the snapshots and you
want to see how bad the damage is before
you run this.
 So you run a variation on the original script
to firstly produce a list and size of the
snapshots and secondly limit the scope of
reconciliation.
Get-VM | Get-Snapshot | Where {$_.SizeGB -lt 1 -and
$_.Created.Day -lt (Get-Date).Day}
Snapshot Script Output
Script:
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 that
script. You can pull the information about the
Snapshot by adding a “| fl *” after it. This
displays all the information available (fl is an alias
for Format-List)
Where to go next
 I started by using Powershell.exe as my
main command line interpreter, instead of
cmd.exe.
 I also tried to use the command line
wherever possible to ensure memory of the
commands
 Document your findings!
 Maintain a script repository with specific
naming. Native commands use Get, New
and Set, consider using different verbs to
differentiate your scripts (Do, Put, or
Change)
Questions?
Where do YOU want to go next?
1 Liners Link
http://darusintegration.blogspot.ca/2011/11
/quick-and-easy-powershell-one-
liners.html

More Related Content

PDF
Production MongoDB in the Cloud
PDF
What the Unity engine documentation does not tell you?
PDF
PostgreSQL Streaming Replication Cheatsheet
PDF
Cache management obiee 10g
PDF
NetApp ontap simulator
PPTX
Feb 2010 Intro To Remoteing Part1
PDF
How to add non root user account to netapp for commvault
PDF
PostgreSQL Troubleshoot On-line, (RITfest 2015 meetup at Moscow, Russia).
Production MongoDB in the Cloud
What the Unity engine documentation does not tell you?
PostgreSQL Streaming Replication Cheatsheet
Cache management obiee 10g
NetApp ontap simulator
Feb 2010 Intro To Remoteing Part1
How to add non root user account to netapp for commvault
PostgreSQL Troubleshoot On-line, (RITfest 2015 meetup at Moscow, Russia).

What's hot (18)

PDF
Как PostgreSQL работает с диском
PDF
Ceph Day Shanghai - Ceph Performance Tools
PDF
GitLab PostgresMortem: Lessons Learned
PDF
Out of the box replication in postgres 9.4
PDF
PostgreSQL 9.6 새 기능 소개
PDF
Managing PostgreSQL with PgCenter
PDF
Backup workflow for SMHV on windows 2008R2 HYPER-V
PDF
Building images from dockerfiles
ZIP
Rubinius 1.0 and more!
DOC
Huge pages
PDF
Peeking into the Black Hole Called PL/PGSQL - the New PL Profiler / Jan Wieck...
PPTX
100500 способов кэширования в Oracle Database или как достичь максимальной ск...
PDF
Deep dive into PostgreSQL statistics.
PDF
Machine learning 2 - Neural Networks
PDF
Machine Learning - Introduction
PPTX
Building cloud stack at scale
KEY
PostgreSQL
PDF
PostgreSQL Configuration for Humans / Alvaro Hernandez (OnGres)
Как PostgreSQL работает с диском
Ceph Day Shanghai - Ceph Performance Tools
GitLab PostgresMortem: Lessons Learned
Out of the box replication in postgres 9.4
PostgreSQL 9.6 새 기능 소개
Managing PostgreSQL with PgCenter
Backup workflow for SMHV on windows 2008R2 HYPER-V
Building images from dockerfiles
Rubinius 1.0 and more!
Huge pages
Peeking into the Black Hole Called PL/PGSQL - the New PL Profiler / Jan Wieck...
100500 способов кэширования в Oracle Database или как достичь максимальной ск...
Deep dive into PostgreSQL statistics.
Machine learning 2 - Neural Networks
Machine Learning - Introduction
Building cloud stack at scale
PostgreSQL
PostgreSQL Configuration for Humans / Alvaro Hernandez (OnGres)
Ad

Similar to Where to start with power cli (20)

ODP
AutoScaling and Drupal
PDF
Powervc upgrade from_1.3.0.2_to_1.3.2.0
PPTX
Openstack Rally - Benchmark as a Service. Openstack Meetup India. Ananth/Rahul.
PPT
Php classes in mumbai
PPTX
Rally - Benchmarking_as_a_service - Openstack meetup
PDF
Exch2007 sp1 win2008
PDF
Memcached Study
PDF
Deep Dive on Amazon EC2 Instances (March 2017)
PDF
VMworld 2013: Virtualizing and Tuning Large Scale Java Platforms
PDF
Live deployment, ci, drupal
PDF
Reducing Risk When Upgrading MySQL
PDF
php & performance
PDF
TrinityCore server install guide
ODP
Performance Optimization of Rails Applications
PPTX
SharePoint 2010 Virtualization - SharePoint Saturday L.A.
PPTX
SharePoint 2010 Virtualization - Norway SharePoint User Group
PDF
Meb Backup & Recovery Performance
PPTX
SharePoint 2010's Virtual Reality - SPC2C
PPTX
SharePoint 2010's Virtual Reality
ODP
Introduction to LAVA Workload Scheduler
AutoScaling and Drupal
Powervc upgrade from_1.3.0.2_to_1.3.2.0
Openstack Rally - Benchmark as a Service. Openstack Meetup India. Ananth/Rahul.
Php classes in mumbai
Rally - Benchmarking_as_a_service - Openstack meetup
Exch2007 sp1 win2008
Memcached Study
Deep Dive on Amazon EC2 Instances (March 2017)
VMworld 2013: Virtualizing and Tuning Large Scale Java Platforms
Live deployment, ci, drupal
Reducing Risk When Upgrading MySQL
php & performance
TrinityCore server install guide
Performance Optimization of Rails Applications
SharePoint 2010 Virtualization - SharePoint Saturday L.A.
SharePoint 2010 Virtualization - Norway SharePoint User Group
Meb Backup & Recovery Performance
SharePoint 2010's Virtual Reality - SPC2C
SharePoint 2010's Virtual Reality
Introduction to LAVA Workload Scheduler
Ad

Recently uploaded (20)

PDF
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
PDF
A novel scalable deep ensemble learning framework for big data classification...
PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PDF
Five Habits of High-Impact Board Members
PDF
August Patch Tuesday
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PPT
Geologic Time for studying geology for geologist
PDF
A review of recent deep learning applications in wood surface defect identifi...
PDF
CloudStack 4.21: First Look Webinar slides
PDF
Architecture types and enterprise applications.pdf
PPTX
Benefits of Physical activity for teenagers.pptx
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PPTX
Chapter 5: Probability Theory and Statistics
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Getting Started with Data Integration: FME Form 101
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
A novel scalable deep ensemble learning framework for big data classification...
Taming the Chaos: How to Turn Unstructured Data into Decisions
Five Habits of High-Impact Board Members
August Patch Tuesday
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
Geologic Time for studying geology for geologist
A review of recent deep learning applications in wood surface defect identifi...
CloudStack 4.21: First Look Webinar slides
Architecture types and enterprise applications.pdf
Benefits of Physical activity for teenagers.pptx
A contest of sentiment analysis: k-nearest neighbor versus neural network
sustainability-14-14877-v2.pddhzftheheeeee
Final SEM Unit 1 for mit wpu at pune .pptx
WOOl fibre morphology and structure.pdf for textiles
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
Chapter 5: Probability Theory and Statistics
A comparative study of natural language inference in Swahili using monolingua...
Getting Started with Data Integration: FME Form 101
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf

Where to start with power cli

  • 2. Why use PowerCLI  The biggest question to ask is:  Why would I use PowerShell (PoSH) if there is a perfectly good Graphical User Interface?  When there is Administration of Multiple systems there is always a need for scripting.  Basic Example: ○ Get a list of all VM, just by their names
  • 3. Script and Output Get-VM | select Name Name ==== Server1 Server2 Server3 Server4 Server5 Server6 Server7 …
  • 4. Real World Scenario  Joe is an Administrator at a small Oil and Gas company that has 4 ESXi servers and 120 VMs running on them.  Joe’s manager requests a report for a list of Servers in the environment, with the amount of CPUs, Memory, Disk Space used.  “Piece of Cake” Joe says as he goes to the VIClient to pull a report from the GUI.
  • 5. Using the GUI  Joe selects Inventory  Hosts and Clusters, Selects the vCenter host and then selects the “Virtual Machines” Tab.  He sorts the Virtual Machines by name and examines what fields are able to be displayed.
  • 6. Shortcomings of Using the GUI  Joe assesses that using the GUI can be done but he knows his manager and he will ask for this report again, and again, and again, and he would like to either automate this or have this running via a single script.  Joe also wants to exclude certain servers as they are test machines and are not needed for this report for his manager. (Names Start with Test)
  • 7. Attempting the first script Needs: Name, vCPUs, Memory and Disk Used Script: Get-VM | Select Name, NumCPU, MemoryGB, UsedDiskGB
  • 8. First Script Output Output: Name NumCPU MemoryGB UsedDiskGB ==== ====== ======== ========== Server1 2 4.0000008 31.0103883 Server2 4 7.999293 48 TestServer1 1 1.9199201 48  As you can see the script produced the results we were looking for, however with some mixed output results, and what about the exclusion of Test machines.
  • 9. First Script Revamp Script: Get-VM | Select Name, NumCPU, @{Name=“Memory”;Expression={[System.Math]::Round( $_.MemoryGB, 2)}}, UsedSpaceGB Explanation:  Okay, this looks complicated but when you break it down it is very similar to the first attempt, but we are naming the MemoryGB – “Memory” and using a system method using a Math Function to Round the variable to 2 decimal points.  We could also do this to the UsedSpaceGB to make it easier to read.
  • 10. First Script Revamp Output Output: Name NumCPU Memory UsedDiskGB ==== ====== ======== ========== Server1 2 4 31.0103883 Server2 4 7.99 48 TestServer1 1 1.91 48  As you can see the script produced the results we were looking for, however with some mixed output results, and what about the exclusion of Test machines.
  • 11. First Script Revamp (part 2) Script: Get-VM | where{$_.Name –notmatch “Test”} | Select Name, 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. Script Revamp Output (2) Output: Name NumCPU Memory Used Disk ==== ====== ======== ========== Server1 2 4 31 Server2 4 7.99 48 Notice the formatting is good the test server is removed balance to the force has been restored.
  • 13. Now for Something Useful  Who has to deal with aging snapshots when other administrators in your group think that a snapshot is a backup?  Your datastores show a red exclamation mark and seem to be almost full???  Script: The easiest would be to blanket remove all the snapshots via this script Get-VM | Get-Snapshot | Remove-Snapshot
  • 14. Snapshot Script  The problem with this is that it may take DAYS to reconcile the snapshots and you want to see how bad the damage is before you run this.  So you run a variation on the original script to firstly produce a list and size of the snapshots and secondly limit the scope of reconciliation. Get-VM | Get-Snapshot | Where {$_.SizeGB -lt 1 -and $_.Created.Day -lt (Get-Date).Day}
  • 15. Snapshot Script Output Script: 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 that script. You can pull the information about the Snapshot by adding a “| fl *” after it. This displays all the information available (fl is an alias for Format-List)
  • 16. Where to go next  I started by using Powershell.exe as my main command line interpreter, instead of cmd.exe.  I also tried to use the command line wherever possible to ensure memory of the commands  Document your findings!  Maintain a script repository with specific naming. Native commands use Get, New and Set, consider using different verbs to differentiate your scripts (Do, Put, or Change)
  • 17. Questions? Where do YOU want to go next? 1 Liners Link http://darusintegration.blogspot.ca/2011/11 /quick-and-easy-powershell-one- liners.html