整合PowerCLI与PowerShell

早先我有写一篇文章关于如何手工整合PowerCLI和PowerShell。最近重装了系统,不得不再做一次。以前我习惯用PowerGUI写脚本,但不知道怎么搞的PowerGUI总是丢失菜单界面,这困扰了我很久一直没办法完全解决,所以这次我在想能不能把PowerCLI和PowerShell整合了一劳永逸,毕竟内置的好用一些啊。

Continue reading “整合PowerCLI与PowerShell”

How to Automate Snapshot on Virtual Machine

I always treat virtual machine snapshots like a big risk. It caused several outages in our infrastructure. Please check out Best practices for virtual machine snapshots in the VMware to understand how it impacts production.

虚拟机快照对我来说绝对是个大威胁,已经在我的生产环境里发生过好几次由此引发的故障了。如果你要了解快照对生产环境的影响可以看看:Best practices for virtual machine snapshots in the VMware

Continue reading “How to Automate Snapshot on Virtual Machine”

How to integrate PowerCLI with PowerShell and PowerShell ISE

I wrote a post about how to integrate PowerCLI with PowerShell manually. I rebuilt my computer few days ago, need to integrate PowerCLI again. I used to scripting by PowerGUI, but something always lead to PowerGUI lost menu, it frustrated me a long time. I cannot figured out what’s the root cause. So I wondered is it possible use PowerShell ISE instead of PowerGUI?

Continue reading “How to integrate PowerCLI with PowerShell and PowerShell ISE”

How to get HP ProLiant blade server and enclosure information

An enterprise infrastructure administrator needs to run plenty of reports for firmware, software version, or any kind of infrastructure data in their day-to-day operation. Some vendors provide powerful tools to pull out data from their solution, but what if you don’t have such tools? It is pain to get data manually especially for large number of servers. I’m going to share my trick to you. I’ll use HP ProLiant blade system for example, as it’s very common case in enterprise datacenter.

Continue reading “How to get HP ProLiant blade server and enclosure information”

Extremely slow when run PowerShell script by scheduled taks

I like automition/programming as much as I like Windows, my first offical training in University was VB, then I learned PHP and HTML after graduated. I can’t believe my first PHP program was welcomed in internel. But I stopped develop the skill.

I got lot of special request for virtual environment. Some people want to monitor snapshot, some want to check space status, and some request to create backup for VM…etc. Regular operation cannot satisify the requests, most of the requests are time consuming operation. Automation can save my ass. 🙂

You may know PowerCLI – VMware automation product for PowerShell. If you put PowerCLI scripts as scheduled task, you will be able to do any workflow you want by schedule.

But there is a problem stuggled me for a long time. The scheduled PowerCLI script is instable, sometimes script ran very well, sometimes script ran extremely slow, sometimes script hung, or sometimes script unexpected stopped. In troubelshooting I noticed it need a long waiting time (maybe  5 – 10 mintues) till execute first line of script when I ran it on Scheduled Task Window. Initially I thought it’s a bug of PowerShell 2.0. I upgraded to 3.0 but no lucky. I also suspected loading PowerCLI Snap-in may slow down the script exectue time, or bugs on PowerCLI components, tried to google but nothing was found.

Finally my colleague Amnon gave my some idea, he asked me change the scheduled taks priority. I figured out the trick after did some testing:

  1. Create a task as usual.
  2. Export the taks to a xml file.
  3. Open the xml file by editor.
  4. Search keyword priority.
  5. Change the default value 7 to 3. ( More detail about priority of Scheduled Task )
  6. Save the xml file and delete created task.
  7. Import the xml as a task.

It signaficantly improved the executive time! Same script was 2 hours, now only 10 minutes!

Here is my another post ($array.count does not show anything in PowerCLI) regarding a bug of PowerShell, it may helpful for your PowerCLI script.

$array.count does not show anything in PowerCLI

I developed a script to take snapshot on VM and delete older snapshot regularly. I  used  variable $snapshots to receive data from Get-Snapshot command, then determine how many existing snapshots according to return of $snapshots.count. Somehow the script always ran incorrect behavior. When I deep looked into $snapshots, I found a interesting things.

Computer A, $snapshots.count return nothing if no, or one snapshot received.

Computer B, $snapshots.count returned 0 if no, or returned 1 if one snapshots received.

Then I checked out $PSVersionTable, found computer A powershell version is 2.0, B is 3.0, looks like this is a bug in 2.0.

The issue got fixed after upgrade to 3.0, one more benefit is I got performance improve to load PowerCLI components after upgrading.

You could install Widnows Management Framework 3.0 to upgrade PowerShell. I tested it on PowerCLI 5.5 Update1.

Get specific advanced configuration of ESXi host

Storage team said the best practics of QFullSampleSize is 32, they want to check how it’s going in our environment. It’s easy to check individual host, but pretty time consuming if you want to check 300+ hosts.
Here is a one line PowerShell script to export QFullSampleSize and QFullThreshold to a csv file.

Get-VMHost | %{ $HostName=$_.Name; $HostCluster=$_.Parent; Get-VMHostAdvancedConfiguration -VMHost $_ | % { $_.getEnumerator()| ? {$_.Key -like "*QFull*"} | select Name,Value,@{N='host';E={$HostName}},@{N='Cluster';E={$HostCluster}} } } | export-csv c:qSetting.csv

 

 

 

How to retrieve RDM information by PowerCLI

I worked on move RDM LUNs of Microsoft Cluster virtual machine from one iGroup to another. To make sure the moving safe, we should record RDM LUN information before migration.

We had two VMs with almost 20 RDM LUNs, it’s pretty time consume to get the information manually, I used following script to retrieve information:

$RMDinfo = Get-HardDisk -VM virtual machine name -DiskType rawPhysical

$RDMinfo | select Parent,Filename,CapacityGB,ScsiCanonicalName,Name

 

How to remove multiple snapshot by PowerCLI

My SMVI backup job was crashed few days ago, the stupid application generated a lot of snapshots for virtual machine!!! It’s  hundred!

I really don’t like to remove one by one! That’s what I used to clean up the snapshot.

Get-VM | Get-Snapshot -Name smvi* | Remove-Snapshot

I used wildcard smiv*, it means all snapshot that name start with smvi.