pátek 13. března 2009

smsDiagram - Draw your SMS hierarchy with one script

The Story
I am an administrator of big SMS hiearchy. Few times I reported to management how our infrastructure looks like. You know managers - they doesn't understand to objects, pipes and other funny parts of admin life. They are like children - likes pictures and graphs. So it's nice to show them infrastructure drawn in Visio. Till now I created it manually but after we merged few sites together it's not fun anymore (we have about 300 servers).

Maybe you recognized small analogy in the title of this post. When I saw an article written by Alan Renouf I knew that my future is bright. So thanks to Alan to kick me (Virtu-Al(ly) of course :)

The Script
Script itself is not so complicated. The biggest part is related to Visio (I use 2003 version) drawing. But there are some parts you might find useful for your own scripts.

Load hierachy from WMI
  1. $siteCode = (Get-WmiObject -ComputerName $server -Namespace root/sms `
  2. -Class SMS_ProviderLocation -Filter "ProviderForLocalSite='True'").SiteCode
  3. # SMS Namespace path
  4. $SMSWMINamespace = 'root/sms/site_'+$siteCode
  5. # List of children servers from SMS_Site class
  6. $servers = Get-WmiObject -ComputerName $server -Namespace $SMSWMINamespace -Class SMS_Site | `
  7. Select ReportingSiteCode, SiteCode, ServerName, Type | `
  8. Sort ReportingSiteCode, SiteCode
At lines 1-2 we have one-liner which connects to WMI of the site server (specified as parameter of the script) and find sitecode in SMS_ProviderLocation class. Then we connect to the SMS site namespace. We can load all necessary information from that namespace (lines 7-9) and save it in $servers object.

Text representation of SMS hierarchy
If you don't have Visio or just want quick overwiew you have two choices:
  1. Use standard SMS report (Sites by hierarchy with time of last site status update)
  2. Pass parameter $textInfo to smsDiagram script :)
Second option will display your hierarchy in this way:

PS C:\> ./smsDiagram.ps1 -server SMS01 -textInfo
CC0 (P)
  CZ0 (P)
    500
    HU0 (P)
      501
  ZZ0 (P)
    003
    004
    ZZ1 (P)

It's easy and fast - as fast as your connectivity to the site server. (P) means that the displayed server is Primary server.

Voila
And here is the nicest part of the script. It uses standard Visio Network template - Detailed Network Diagram. If you want to show infrastructure as Visio diagram just provide site server name as a parameter. In the diagram you can also see which server is Primary (db icon) and which is Secondary.

PS c:\> smsDiagram.ps1 -server SMS01



Now I have latest SMS hierarchy at my desk and it looks awesome :) Final document is saved in your My Documents folder, name is smsDiagram.vsd.

Notes
I didn't test the script against ConfigMgr. My "test lab" is not active now (to be honest - my NB where I ran it crashed before few days and I haven't time to rebuild it now).
No error codes checking now. Be sure you run the script as admin able to connect to WMI of site server.
Sometimes the Visio diagram is "ugly" - it fully depends on how the Visio handle objects creation. Hopefuly will find some solution but it's not a priority now.
Default text info shows site code. For next version I plan to add switch which tells what do you want to show as server description.
For the future versions of the script I also would like to add some more features - display server roles (MP, CAP, ...), customize output (shape names, connectors).
Source code is available at PoSh Code.

Please let me know if you find it useful or if you find some errors. Any feedback for improvement is welcome.

pondělí 9. března 2009

Looking for HResult

In one of the last Hey, Scripting Guy! article is described how to install updates and check it's result codes. I was really surprised when Scripting guys told me: "We can use calc.exe" - OMG why not use converting directly in PowerShell?

PS C:\> "0x{0:x}" -f [int] -2145124318
0x80240022

When I tried this I start thinking about the function which will do the conversion and shows also description of the result code.

  1. # Name : Get-HResult.ps1
  2. # Author: David "Makovec" Moravec
  3. # Web : http://www.powershell.cz
  4. # Email : powershell.cz@googlemail.com
  5. #
  6. # Description: Finds meaning of HResult
  7. #
  8. # Version: 0.1
  9. # History:
  10. # v0.1 - (add) basic functionality
  11. #
  12. # Usage: Get-HResult -2145124318
  13. #
  14. #################################################################
  15. function Get-HResult {
  16. param (
  17. $HResult,
  18. $url = 'http://technet.microsoft.com/en-us/library/cc720442.aspx'
  19. )
  20. $calc = "0x{0:x}" -f [int]$HResult
  21. $lookFor = $calc+'.*?tr'
  22. $wc = New-Object system.net.webclient
  23. $wc.DownloadString($url) -match $lookFor | Out-Null
  24. $tmp = $matches[0]
  25. $searchString = '(?<ResCode>'+$calc+').*\<p\>(?<ResStr>.+?)\<.*\<p\>(?<Description>.+?)\</p.+'
  26. $tmp -match $searchString | Out-Null
  27. Write-Host "HResult: " $HResult
  28. Write-Host "Result Code: " $matches.ResCode
  29. Write-Host "Result String: " $matches.ResStr
  30. Write-Host "Description: " $matches.Description
  31. } #function

By default script return results for Windows Update Agent and has no checking for errors. It has parameter for providing different URL but I didn't try any. Usage is following:

PS C:\> Get-HResult -2145124351
Result Code: 0x80240001
Result String: WU_E_NO_SERVICE
Description: Windows Update Agent was unable to provide the service.

Source code will be available at PoSh Code when I'll be able to open it, now have troubles with connectivity.

pátek 6. března 2009

Hlasování

Trošku jsem přemýšlel nad obsahem tohoto webu. Vzhledem k tomu, že některé z příspěvků bych rád publikoval "mezinárodně", překládám částečně již existující příspěvky.
Proto bych se rád zeptal, jestli by vám vadilo, kdyby byl obsah webu psán v anglickém jazyce. Děkuji za vaše hlasy.