Visual Studio Online + VSCode = Easy PowerShell Source Control

In my daily work at CTGlobal we are all working a lot with PowerShell and therefor we have a big need to share code between each other and also having some version control in place when we are making changes. So for the last few months I has been playing a lot around with Visual Studio Code and Git repositories in Visual Studio Online to solve this.

Pre-reqs

To create a new code repository in VSTS go to the project you want the repository to reside in and select the Code tab. Click the repository drop down and click New repository. A box appears, enter the name for the new repository and select Git type. You now have a new repository.

clip_image001

To clone the repository to your local computer and work with the code click Clone in the right corner and copy the URL.

clip_image002

Open Visual Studio Code, press F1 to open the command palette and write Git: Cloneclip_image003

You will then be asked to enter the URL you just copied, and then the parent path where the local copy if the repository will be placed.

imageimage

The repository will then be cloned to your local computer and you can start to add files to it. When you have added some files or done any changes and you want to save it to the repository you have to commit it. You commit changes by going to the Source Control tab on the left in Visual Studio Code, review the changes and click Commit.

image

The changes are then committed to the local copy of the repository. To sync the changes back into Visual Studio Online press F1 to open the command palette again and write Git: Sync and press enter, the changes is now pushed back to master repository and your code is in control.

Happy source controlling!

Optimizing Cisco UCS Management Pack for larger(any) environments

Some months ago Cisco released a brand new management pack for the UCS hardware platform, this update was more or less a rewrite of the entire solution, including switching all alarms from monitor based to rule based. This was done in order to minimize the number of discoveries needed and therefor the objects created by the management pack, this could have quite a performance impact since every little memory module and fan was discovered as objects, and this could be many fans and memory blocks in a large UCS environment.

Since it is all rule based now we would miss the auto-resolve feature usually used by monitors, but to solve this Cisco created a rule using a simple schedule to close and update alerts as needed based on the event entries on the server running the Cisco UCS monitoring service. But with the first release (4.0.1) there was a problems, it was only checking for alerts with resolution state New (0) and i guess in most production environments many are using resolution states to route alerts to correct teams or set the progress of the alert, therefor when you changed resolution state on your alerts they would never update or close. This problem is now solved in version 4.1.1 but now a new problem appears! Cisco choose to use “Where-Object {$_.ResolutionState -ne 255” to find all the non-closed alerts, using this method in our large environment it takes 10-12 seconds to find all the closed alerts and to make it even worse this command is running for every event collected by the rule “Cisco.Ucs.Watcher.UCSFault.Event.Collection.Rule” for the selected interval meaning that the script would never complete in our case and end on timeout.

To resolve all this I found all the elements needed for the rule “Cisco.Ucs.Library.UpdateAndCloseAlert.Rule” in the original Cisco Management pack and created my own fix for this problem.

Instead of

$scomActiveAlerts = Get-SCOMAlert | Where-Object {$_.ResolutionState -ne 255}

I changed it to

$ActiveAlerts = Get-SCOMAlert -ResolutionState (0..254)

This simple change gives the same result but in 0,5 seconds instead of 10-12. And instead of running the command for each event collected I changed it to run one time for the script (just before the ForEach) and save the result in a variable $ActiveAlerts and use that in the ForEach instead.

This just shows how easy you can improve (and decrease) the performance of your PowerShell scripts.

I uploaded my fix management pack to GitHub, if you import this one remember to disable the original rule.

GitHub Download

 

 

Cleaning up unused management packs.

I recently did some house keeping in our OpsMgr environment, cleaning up old and unused Management Packs, and to easily identify potential unused Management Packs I of course turned to PowerShell!

This script can walk through the management packs and get the number of instances for each class. If there is no instances of a class it is very likely that it is not used. After running this script you will end up with a list of potential unused classes, you can now check each management pack for it’s content and see if there is other classes in it that is in use.

################################################################
#   Author: Andreas Sobczyk, CloudMechanic.net
#
#   Get Empty classes from Management Packs
################################################################

## Get all management packs starting with Contoso.
$MPs = Get-SCOMManagementPack -name Contoso*

## For each MP, For each Class, count classinstances.
$Overview = @()
Foreach($MP in $MPs){
$Classes = $mp.GetClasses()

foreach($Class in $Classes){
$ClassInstance = $null
$ClassInstance = Get-SCOMClassInstance -Class $class

## If you want you can change the number of class instances to sort on.
if($ClassInstance.Count -le 0){
$Overview += $Class
}
}
}

$Overview | Format-Table Name,Displayname,ManagementpackName

GitHub Download