Author Archives: nerenther

Today I got a request from a colleague of mine that was doing inventory: What are the make, model, serial number and purchase date of your monitors? Seeing as this wasn’t something I had readily in my head I had to figure it out, but being the automation enthusiast that I am, I refused to bend over my monitors and snap a photo of it all. I wanted to find it a cooler way, the powershell way ūüôā This information is stored in WMI, so all I had to do was to grab it using Get-WmiObject and then format it rather nicely for him. The powershell script I came up with isn’t my most beautiful work, but here it is: $progress = 1 foreach ($monitor in (Get-WmiObject WmiMonitorID -Namespace root\wmi)) { Write-Host “Monitor #$($progress):” -ForegroundColor Green Write-Host “Manufactur: $(($monitor.ManufacturerName | ForEach-Object {[char]$_}) -join ”)” Write-Host “PN: ” ($($monitor.UserFriendlyName | ForEach-Object…

Read more

A while back we migrated from our old EMC VNX to a new HPE 3PAR array. It all went fine but what kinda slipped our mind is that HPE’s best practice is to create a custom SATP rule that uses RoudRobin as path selection policy and a IOPS limit to 1 in stead of the default 1000. The steps are documented in the¬†HPE 3PAR VMware ESX/ESXi Implementation Guide, but they only show you how to do it through the esx cli. If you have more then a few esx hosts you would be better of using the Get-EsxCli cmdlet. That way you can put it in a script that loops through all your esx hosts. Another thing to keep in mind is that the SATP rules are case sensitive and HPE is using the wrong case for the vendor parameter (3pardata in stead of 3PARdata), at least as of the…

Read more

Lately, I’ve had the pleasure of using Powershell to automate some of the basic tasks we do on our HPE 3PAR systems: creating volumes, adding them to volume sets, exporting them and so on. Since my experience with REST APIs was rather limited it was quite daunting at first but once you get the hang of how REST works and the Invoke-RestMethod cmdlet it’s really not that bad. Disclaimer: The examples below will vary somewhat in how I do certain things, simply because I had to learn all this from scratch. Hence, the first examples will sometimes do things “less correct” than the later examples since this also was quite the learning curve for me. The first step in doing anything with the REST API will always be to create a session key. If you’re not familiar with APIs, think of a session key as username and password combined into…

Read more

A tweet from @JanEgilRing caught my eye this morning, it¬†was showing¬†how you can use powershell to create passwords. The link in the tweet pointed here:¬†http://powershell.com/cs/blogs/tips/archive/2016/05/23/one-liner-random-password-generator.aspx Seeing that line and realizing how simple it was, it got me thinking on¬†how I could implement this in my scripts. The only issue I saw with that one-liner was that the passwords it creates do not necessarily comply¬†with high complexity rules. So, how can we approve on this? Firstly, we need to create a regex that we can use to validate that the password created complies with our rules. In our environment this means 12 characters, uppercase, lowercase and either a number or special character. The regex I ended up with is this one:¬†^.*(?=.{12,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$ (which I found here:¬†https://nilangshah.wordpress.com/2007/06/26/password-validation-via-regular-expression/¬†) Now that we have our regex we can simple throw the one-liner into a while loop: while ($pass -notmatch “^.*(?=.{12,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$”) { $pass = -join (‘abcdefghkmnrstuvwxyzABCDEFGHKLMNPRSTUVWXYZ23456789$%&*#’.ToCharArray() |…

Read more

I have been struggling for quite some time with mapping luns from our vnx 5600 to entire clusters in our vCenter. We used to utilize a custom workflow a consultant wrote for us, but that workflow got borked after an update to UCS Director nearly a year ago. Revisiting the issue i found this example from Cisco:¬†https://communities.cisco.com/docs/DOC-57382 That example seems to work for other people but in our case the custom task in it never gave the correct output, so I had to look for a way around it. The solution I came up with is overly complicated and can surely be simplified, but my lacking knowledge of javascript limits me quite a bit. My workflow to map luns to vSphere clusters consists roughly of these steps: A powershell task running a script that does the following: Queries vCenter for esxi hosts in given cluster Queries UCS Directors api for…

Read more

My last post described how to get around some issues with using Powershell tasks in workflows. While that post surely enables you to uilize powershell to do stuff for you, what about if you want Powershell to grab stuff for you and return them in a usable matter? This time I’m going to show you how you can return a string from Powershell and use it further down in the workflow. Cisco has provided an example on how to do that here:¬†https://communities.cisco.com/docs/DOC-58250 The example from Cisco is what I started with, but I have modified it a bit since I didn’t want anything that advanced. So let’s set the stage: Say you have a workflow that uses the execute powershell command task and you want that task to output something you can utilize further down ¬†in the workflow, e.g. sending that output in an email. In this case we will…

Read more

While the¬†Cisco PowerShell Agent (PSA) that can be used in UCS Director isn’t exactly perfect, it can still be put to good use. As long as you now how to use it properly ūüėČ The major issue with using the PSA is that it doesn’t stick around to see if the commands/script was successful or not. As long as it delivered the commands successfully, it’s happy and your workflow will continue to the next step. Jon Hildebrand describes a nice way around this in one of his blog posts:¬†http://snoopj.wordpress.com/2014/11/05/cisco-powershell-agent-service-and-vmware-vum-powercli/ Using his approach, I was able get the PSA to stick around until the job finishes. However, I ran into a challenge when I wanted to pass multiple arguments to start-job. The solution I came up with was declaring the UCSD inputs I wanted to use as powershell variables in the script, before calling the start-job cmdlet. So the commands/script input…

Read more

My very first PowerCLI related post was about this same topic: listing snapshot info using PowerCLI. In my original post (which you can see here) I only wrote a pretty simple one-liner. Which was kind of okay, but it was missing one crucial thing: who took the snapshot? Why vmware hasn’t found a way to include a username in the get-snapshot cmdlet is something I just can’t understand. There’s really not much code needed to add this to the output, and there’s several ways of doing so. I found that using Get-Snapshot and Get-VIEvent together was the easiest way to get all the info I want. It’s not a perfect solution, seeing as I really wanted to make use of the much faster Get-View instead of Get-Snapshot, but I have yet to figure out a good way to handle snapshot trees using Get-View. As usual I created a function for…

Read more

In case you ever need to empty out a datastore in you vmware environment, there is a nice little one-liner in PowerCLI for that: Get-VM -Datastore “datastore1″ | Move-VM -Datastore (Get-VMHost -Location ‘cluster1’ | Select-Object -First 1 | Get-Datastore | Where-Object {($_.Name -ne ‘datastore1’) -and ($_.FreeSpaceGB -gt ‘500’)} |¬†Sort-Object FreeSpaceGB -Descending | Select-Object -First 1) Where “datastore1” is the datastore you want to empty out and “cluster1” is the cluster where the datastore is available. The command will move VMs from datastore1 to the datastore in cluster1 with the most available space (minimum 500GB)

For almost a year ago, I posted a simple one-liner to list all VMs who has ISOs mounted. You can view that post here:¬†http://cloud.kemta.net/2013/10/powershell-vmware-list-all-vms-with-iso-mounted-and-dismount-them/ That post was written before I truly discovered the major advantages of using Get-View instead of Get-VM, Get-VMHost and so on. If used correctly, there’s a major difference in speed when using Get-View over Get-VM. When writing this post I checked the differences in speed when using the old way that I linked to above and my new function (which I’ll get to in a second or two..), the result was as follows: As you can see, the difference is pretty clear. 5 seconds vs. 1.6 minutes… So,¬†without further ado, I present to you the code for Get-ISOMounts: function Get-ISOMounts { [CmdletBinding()] Param ( [switch]$Dismount ) $VMs = Get-View -ViewType virtualmachine -Property name,Config.Hardware.Device $VMsWithISO = @() $progress = 1 foreach ($VM in $VMs) { Write-Progress -Activity…

Read more

10/76