Tag Archives: PowerShell

Running a PowerCLI Scheduled task

There are a fair few PowerCLI scripts which can be run as scheduled tasks, some that email you or maybe a monitoring script but recently I have been asked how this can be done, what do you put in the Run line for the script to work properly.

There are two ways of doing this really:

1.  Add a line to a start of each of your scripts to make sure it loads the PowerCLI snapin, without which PowerShell will not recognise any of the PowerCLI cmdlets.

Continue reading →

PowerCLI: Host Hardware One-Liner

A quick one-liner which gives you an outline of your Host hardware information:

Get-VMHost |Sort Name |Get-View |
Select Name,
@{N
=“Type“;E={$_.Hardware.SystemInfo.Vendor+ “ “ + $_.Hardware.SystemInfo.Model}},
@{N
=“CPU“;E={“PROC:“ + $_.Hardware.CpuInfo.NumCpuPackages + “ CORES:“ + $_.Hardware.CpuInfo.NumCpuCores + “ MHZ: “ + [math]::round($_.Hardware.CpuInfo.Hz / 1000000, 0)}},
@{N
=“MEM“;E={“” + [math]::round($_.Hardware.MemorySize / 1GB, 0) + “ GB“}} | Export-Csv c:\hostinfo.csv

Output Example:

Name Type CPU MEM
prod01.domain.local Dell Inc. PowerEdge 2950 PROC: 2 CORES: 8 MHZ: 2993 32 GB
prod02.domain.local Dell Inc. PowerEdge 2950 PROC: 2 CORES: 8 MHZ: 2993 32 GB
prod03.domain.local Dell Inc. PowerEdge 2950 PROC: 2 CORES: 8 MHZ: 2993 32 GB
prod04.domain.local Dell Inc. PowerEdge 2950 PROC: 2 CORES: 4 MHZ: 2993 16 GB
tst10.domain.local Dell Inc. PowerEdge 2950 PROC: 2 CORES: 8 MHZ: 2660 16 GB
tst11.domain.local Dell Inc. PowerEdge 2950 PROC: 2 CORES: 8 MHZ: 2660 16 GB
tst12.domain.local Dell Inc. PowerEdge 2950 PROC: 2 CORES: 8 MHZ: 2660 16 GB
tst13.domain.local Dell Inc. PowerEdge 2950 PROC: 2 CORES: 8 MHZ: 2992 32 GB
tst14.domain.local Dell Inc. PowerEdge 2950 PROC: 2 CORES: 8 MHZ: 2992 32 GB
tst15.domain.local Dell Inc. PowerEdge 2950 PROC: 2 CORES: 8 MHZ: 2992 32 GB
tst16.domain.local Dell Inc. PowerEdge 2950 PROC: 2 CORES: 8 MHZ: 2992 32 GB

PowerCLI: More One-Liner Power

A few one-liners inspired by the people who I follow on twitter, firstly Jason Boche was working on a script to reset CPU and Memory limits to “Unlimited” for all VMs in vCenter, I joked with him that this was a one liner, firstly we can set the memory limits with the following:

Get-VM | Get-VMResourceConfiguration | Where-Object {$_.MemLimitMB -ne ‘-1‘} | Set-VMResourceConfiguration -MemLimitMB $null

The –1 equates to an “Unlimited” setting.  This doesn’t address the CPU limits, this can be done using the following:

Get-VM | Get-VMResourceConfiguration | Where-Object {$_.CpuLimitMhz -ne ‘-1‘} | Set-VMResourceConfiguration -CPULimitMhz $null

Continue reading →

PowerCLI: Stats One-liner

Ever wondered how your VM’s are doing ?  What resources are they using, do they have too many CPU’s or too much memory ?

This simple one-liner will give you a list of each of your vms, it will tell you how many cpu’s, the amount of memory, average cpu usage for x amount of days and the average memory usage for x amount of days, how cool is that ?!

Get-VM | Where {$_.PowerState -eq “PoweredOn”} | Select Name, Host, NumCpu, MemoryMB, @{N=”Cpu.UsageMhz.Average”;E={[Math]::Round((($_ |Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddHours(-24)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average).Average),2)}}, @{N=”Mem.Usage.Average”;E={[Math]::Round((($_ |Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-24)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average).Average),2)}} ` | Export-Csv c:\Temp\stats.csv

Update 03 July 2009: This has been updated as it was not collecting the average over the given timescales, the original code was selecting 1 sample from the timeframe.

Sample output:

Continue reading →

PowerCLI: vProfiles

Following my recent post, Easy vSwitch & PortGroup Setup I thought I would give PrimalForms a go and create a quick GUI, PrimalForms is a great app from Sapien which allows you create form based windows for which it gives you the PowerShell code, this means no install and yet the users still get a great looking GUI.

It turned out pretty good, even if I do say so myself.

Here’s what the script allows you to do:

  • Compares one host to another
  • Copies vSwitches if they do not currently exist
  • Copies PortGroups + VLAN Tags if they do not currently exist

What it doesn’t do at the moment is copy any of the custom settings for the vSwitches or PortGroups.

This is still very much a V1 and it will be added to, I hope to include other areas such as the NTP servers, advanced host configuration etc, if you have an idea then please add it to the comments and I will see what I can do.

To see this in action please check out the below video, this script can easily be run straight from the PowerCLI prompt also.

vProfiles from Alan Renouf on Vimeo.

Continue reading →

PowerCLI: Easy vSwitch & PortGroup Setup

To get VMotion working the networking setup plays a big part, any of the following could cause you big issues:

  • A spelling mistake in a PortGroup name
  • A missing PortGroup
  • A PortGroup configured incorrectly
  • A non-existing PortGroup on one of your hosts
  • etc

This is why automation and PowerCLI is key to the setup of your vSwitches and PortGroup’s, you could add each vSwitch manually and then create a PortGroup for each VLAN you have trunked to your hosts manually but what are the chances you will miss one, what are the chances you will get the VLAN ID incorrect ?  And you sure do get bored of the wizard when you have 4 vSwitches and around 20 PortGroups !

The following code will take you through copying all vSwitches and PortGroups from an existing ESX server over to a new server, ensuring they are exactly the same.  It sure does save me time !

$VISRV = Connect-VIServer (Read-Host "Please enter the name of your VI SERVER")
$BASEHost = Get-VMHost -Name (Read-Host "Please enter the name of your existing server as seen in the VI Client:")
$NEWHost = Get-VMHost -Name (Read-Host "Please enter the name of the server to configure as seen in the VI Client:")

$BASEHost |Get-VirtualSwitch |Foreach {
   If (($NEWHost |Get-VirtualSwitch -Name $_.Name-ErrorAction SilentlyContinue)-eq $null){
       Write-Host "Creating Virtual Switch $($_.Name)"
       $NewSwitch = $NEWHost |New-VirtualSwitch -Name $_.Name-NumPorts $_.NumPorts-Mtu $_.Mtu
       $vSwitch = $_
    }
   $_ |Get-VirtualPortGroup |Foreach {
       If (($NEWHost |Get-VirtualPortGroup -Name $_.Name-ErrorAction SilentlyContinue)-eq $null){
           Write-Host "Creating Portgroup $($_.Name)"
           $NewPortGroup = $NEWHost |Get-VirtualSwitch -Name $vSwitch |New-VirtualPortGroup -Name $_.Name-VLanId $_.VLanID
        }
    }
}

PowerCLI: More One Liners

On my previous post where we counted how many VMs were on each host and the cluster they were in I was asked a couple of questions:

1.  How about listing the number of VMs per Resource Pool?

2.  How can we extend this to get the templates as well ?

Ok, easy stuff (I thought).

Lets take them in turn…

Question 1.  How about listing the number of VMs per Resource Pool?

What you need to remember about this is that even if a VM is not in a resource pool it still has resources available to it which have to come from somewhere so in fact what VMware do behind the scenes is have a hidden resource pool called ‘Resources’ where it places the VM when it is not in any other resource pool.

If you remember this then yes, it is quite easy as below:

Get-ResourcePool | Where {$_.Name -ne “Resources“ }| Select Name, @{N=“NumVM“;E={($_ | Get-VM).Count}} | Sort Name|Export-Csv -NoTypeInformation c:\res-numvm.csv

All we needed to do was to put a Where statement in there to not count the VMs in the ‘pre-made resource pool’

Question 2. How can we extend this to get the templates as well ?

Ahhh, this one is simple I thought, all we need to do is put a count in for Get-Template so the one-liner looks like this:

Get-VMHost |Select @{N=“Cluster“;E={Get-Cluster -VMHost $_}},Name, @{N=“NumVM“;E={($_ |Get-VM).Count}}, @{N=“NumTemplates“;E={($_ |Get-Template).Count}} |Sort Cluster,Name |Export-Csv -NoTypeInformation c:\clu-host-numvm-numtempl.csv

Unfortunately as a Template is basically a special type of VM the cmdlets don’t work like this at the moment, you can not get a list of Templates which exist on a host without delving into the SDK so what I suggest you do is wait until the next version of PowerCLI is released and see if they have fixed this issue. – Current version is PowerCLI V4.0.0.1580

PowerCLI: Folder Name Mismatch

Isn’t it annoying when you are looking for a VM on your datastore and you cant find it because the folder name is not the same as the VM, use the below script and you will never have that problem again.

This script will compare the name of the VM to the folder it is stored in, any mismatches will be output.

I know a regex expert could probably do this better, let me know if you are one 🙂

Sample Output:

VM Path
New VM 1 [storage1] New VM
ABCDEV01 [storage1] Dev Test
MicroDS [SAN11] New VM
VM127 [SAN11] Windows 2003 Server

Continue reading →

PowerCLI: SnapReminder

Fed up of chasing those people who constantly create snapshots and leave them hanging around for weeks or even months on end ?

You no longer have to do the chasing, just use the following script to automatically find the offending snapshot, find the person who created it, get their email address from AD and send them an email reminding them of their mortal sin.

Continue reading →