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

But thats two lines, whilst we can do it on one line we cant easily add some checking to see if they are already set so the following one-liner will set them even if they are already set, this will take a bit longer than is ideal but it gets the job done and I can still hold my head up high knowing I can do it in one line 🙂

Get-VM | Get-VMResourceConfiguration | Set-VMResourceConfiguration -MemLimitMB $null -CpuLimitMhz $null

The next one-liner was inspired by Mark Stang who asked:

image

This is something that i too wish there was an easy way to do, if you could somehow pass the original object down the pipeline it would be so easy to do this and more powerfull things, if there is a way and you know then please tell me !

In any case, after a bit of messing I was able to get this down to one-line and still give the desired result:

Get-vm | Select Name, @{N=Network;E={$_ | Get-networkAdapter | ? {$_.macaddress -eq 00:50:56:A1:50:43}}} |Where {$_.Network-ne “”}

This gives both the name of the VM and the network adapter which has the Mac address we searched for.

I just also wanted to add that not everything in Powershell is a one-liner, I only do these to show how powerfull PowerShell can be, these are sometimes harder for beginners to understand but heck they are fun, kinda the PowerShell geeks version of Sudoku.

11 thoughts on “PowerCLI: More One-Liner Power”

  1. How to get allocated ip addresses on a network within a certain Organization using PowerCli-vCloud?
    Hi,
    I cannot figure out how to list all VMs and their IP Address on a Network of Allocated IP Addresses broken down by Organizations. I can view this in the vCloud GUI “Cloud Resources |Eternal Networks| Right Click and select IP Allocations” [“IP Allocations on Network: Network Name”] I can then view the IP address if it is deployed, the VM name, the vApp name, Edge gateways, Network and Organizations.

    I would like to have a filtered cvs or delimited file so I can run regular reports.

    Thanks

  2. This will also work:

    get-vm | Get-NetworkAdapter | ?{$_.MacAddress -eq “00:00:00:00:00:00”} | ft -auto parent,macaddress

  3. Take a look at Arnim van Lieshout his post PowerCLI: Reset CPU and Memory Limits at http://bit.ly/dc2O5a to see how this code can be made faster and added to the Community PowerPack.

  4. You can combine the first two oneliners into:
    Get-VM |
    Get-VMResourceConfiguration |

    Where-Object {$_.MemLimitMB -ne -1 -or $_.CpuLimitMhz -ne -1} | `
    Set-VMResourceConfiguration -MemLimitMB $null -CpuLimitMhz $Null

    This will be faster than the third oneliner because it doesn’t call Set-VMResourceConfiguration if it is not necessary.

  5. You can also find a vm mac and its guest host name with:

    Get-VM | where { (Get-NetworkAdapter $_).MacAddress -eq ’00:50:56:a1:50:d5′} | Format-Table Name,Host

Leave a Reply