This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
Yesterday I did read about this MMS session from Jim Bradbury:
LC09 – Automating SMS Tasks with Monad Scripting, here :
Day 1 - MMS 2006 - Part 2Nice to hear he did mention my blog, I did see this session on the agenda of the MMS and did already think it did look interesting, as I just did an SMS 2003 implementation and still I'm busy with it, and I also Used Monad (and still do use PowerShell) a lot with SMS.
So I hope there will be a transcript of this session.
but then I did realize that I only have one SMS example yet on my blog and it's hidden in the CSV series. (it's in part 1),
I made this into a 36 lines select statement (one-liner ;-) to do a Crosstab software report building from the CSV sample using a lot of SMS queries AD and CMDB info that way in PowerShell.
the rest I mostly did prototyping in MSH, but to make it up a bit I post some basic examples here :
basicly I used 3 methods to use SMS with PowerShell,
1) WMI queries like the one in the CSV example.
2) Using the Client COM Objects :
getting available actions (as in controlPanel)
MowPS>$mgr = new -com CPApplet.cpappletmgr
MowPS>$mgr.GetClientActions()
ActionID Name DisplayNameResID DisplayNameResDLL
-------- ---- ---------------- -----------------
{00000000-0000-0000-0000-0... MSI Product Source Update ... 10010 cfg_res.dll
{00000000-0000-0000-0000-0... Hardware Inventory Collect... 10001 cfg_res.dll
{00000000-0000-0000-0000-0... Discovery Data Collection ... 10004 cfg_res.dll
{3A88A2F3-0C39-45fa-8959-8... Request & Evaluate User Po... 10007 cfg_res.dll
{8EF4D77C-8A23-45c8-BEC3-6... Request & Evaluate Machine... 10008 cfg_res.dll
and start them :
# Performing a Computer and User Policy Evaluation
$mgr.GetClientActions() |? {$_.name -like 're*'} |% {$_.PerformAction()}
# You can also use the Object directly, without putting it into a variable first :
# Trigger a Hardware Invertory
(new-object -com CPApplet.cpappletmgr).GetClientActions() |? {$_.name -like 'hardware*'} |% {$_.PerformAction()}
The triggering of the computer policy is very handy after sending software to the client, as this has to be done remote on the client, in the end I used a Vbscript and PSExec, as I don't have PowerShell deployed yet ;-)
But I did a lot of prototyping in MSH (PS).
3) you can Find a .NET DLL in the SMS SDK you can use to manage SMS :
# Make Managed Connection
[System.Reflection.Assembly]::LoadFile('c:\sms\Microsoft.SystemsManagementServer.Automation.dll')
$cred = get-credential
$sms = new Microsoft.SystemsManagementServer.Automation.smsprovider("Mowsms001",$cred.username,($cred.GetNetworkCredential().password),"Site_M03")
# list all defined queries
$sms.Queries.get('Y030003A') | fl QueryID,Name,SQLExpression
# Add some direct rules
$Col = $sms.COLLECTIONS.Get('M03000AE')
$rule = new-object Microsoft.SystemsManagementServer.Automation.SMSCollectionRuleDirect("SMS_R_System",69438,"MowPC44")
$col.CollectionRules.add($rule)
$rule = new-object Microsoft.SystemsManagementServer.Automation.SMSCollectionRuleDirect("SMS_R_System",63535,"MowPC23")
$col.CollectionRules.add($rule)
$col.save()
You can see, that I can just load the DLL into PowerShell and start using it.
This where just some basic examples, but I hope that this, together with the CSV power of PowerShell (see the CSV series) and working with SMS
I'm sure Jim Bradbury did a better job at this, but I wanted to do a post for the ones that,as me, could not be there and have to hope for a transcript
also you have to watch for big WMI queries in SMS that will take a long time and a lot of memory. (but most SMS admins I think will be used to that)
but in MSH it could stall the pipeline and also stop Ctrl-C from working (ctrl-Break will work but quits the Shell)
See this example :
$oq = new system.management.objectquery
$oq.QueryString = "select * from SMS_CM_RES_COLL_MOW000B6"
$mos = new-object system.management.ManagementObjectSearcher($oq)
$mos.scope.path = "\\SMSServer\root\sms\Site_MOW"
# Streaming (items one-by-one)
$mos.get()
# gathering / blocking (Items all at the same time)
$mos.get() | select name,ResourceID
$mos.get() | ft
b.t.w. this is only a collection, hence is still workable, the real
problems begin when I do this with big software queries.
the workaround is this :
MSH>1 | foreach { $mos.Get() } | select name,ResourceID
See also this
:
NewsGroup thread (end of the thread)
I could only do a quick post a few examples, as I also was planning an item about some new features in PowerShell As Ref variables and switch-parameters to functions.
but still hope this is useful.
Dont't forget to use Get-Member on the SMS Objects to See whet they can do this helps a lot.
I will do this in an example to get the AD subnet and sitename of an IP adress or a Hostname, I hope to post later this evening .
Greetings /\/\o\/\/
Tags : Monad msh PowerShell