WMI is agreat way to extend PowerShell’s possibilities and create new functions that will collect data, or create things in our favorite tools.
It is possible to write functions in powershell that will create objects based on the .put() functionality.
In my case I have been pretty much using PowerShell to write funcitons for SCCM 2007 / 2012. I bumped in a problem while coding a function for SCCM 2007, and more precisly the “Software update deployments”.
As you probably know, trying to script functions that use the .Put() methods, it can be sometimes a hell of a problem when it doesn’t work immediatly and you need to troubleshoot it.
Espcially when you have a BUNCH of parameters to pass. And need to identify which one is making your day difficult, because the errors that WMI return us are generic errors, and there is not much to digg-in in order to troubleshoot.
In the above screenshot, i have passed around 20 parameters to my function. All are present, and based on the result of an SQL query.
The properties of the SQL queries return, can differ from the WMI inputs. Especially on their formats.
This means that for exmple for a specefic value SQL returns one type, and WMI asks for another one.
A good example is the StartTime of the Software update assignment. In SQL, it is returned as a string respectnig the following format : 06.12.2013 16:24:00 And of course, WMI wants a String (as well youpi !) BUT respecting this (very logic) format : 20130612162400.000000+120 (Yeah, why should our daily work be easy anyways ??)
So i wrote a function that converts a
So lets cast it !
Ok. That was easy.
Ho but wait. I haven’t set a start date THAT long ago in the past.
I actually made it available on the 06 of december 2013. Not the 12 of july 2013 as you can see on the screenshot below.
An importing thing to note here, is that the time format returned by the SQL query is in US time format.
Which means that if you work in Europe, well, you will have to convert that as well !
Actually, the solution here is pretty simple (once you know it of course !) but since casting doesn’t work, what other possibilites do we have ? Get-date ? Ok. Le’s give it a try !
So thats more like it ! Now we can use this value returned value with my Convert-ToWMITime function.
So, this was for the easy part. Because you can easily find informatino on the interent on what time format the WMI SMS_SoftwareUpdateAssignment class needs for the startime property.
But now we have all our properties, but the Put() methods still throws us generic error messages.
We got used in powershell to have a more specefic message. At least it would mention the parameter that is incorrect. But here, we have nada, niete, rien, nothing !
Even in the $error default variable there is not more informatino available. So how do we find detailed information on this WMI error ?
On the WMI error itself, i still dont know actually (Please let us know if YOU know how to have more information about an WMI error used in PowerShell).
Since i am scripting things related to SCCM, there is actually one log file that is going to help us A LOT : The SMSprov.log
On Windows-noob.com it says that Smsprov.log – Records WMI provider access to the site database
Which is exactly what we are looking for !
[important]The SMSProv.log file is generaly located here : “C:\Program Files (x86)\Microsoft Configuration Manager\Logs\smsprov.log“[/important]
(Detailed information about configMgr log files can be found here : http://www.windows-noob.com/forums/index.php?/topic/1105-sccm-logs/)
So lets look into this error file :
And tadaa ! We have finally INFO !!
We now can see that the problem seems to come from the ‘ UseGMTTimes’ property. It is NULL, but ican’t be NULL.
After a quick verification, i could see the following:
$UseGMTime = 0
Logic, since the SQL query only returns strings. (Is it logic ??).
Anyways, i resolved this by casting the SQL return directly to a bolean value.
(Casting any number, negative or positive value except for 0, to a [bool] will convert the value to $true.
Casting 0 to [bool] will return $false)
So i fixed the first issue like this :
$UseGMTime = [bool]0
$NewSoftwareUpdateAssignment.UseGMTTime = $UseGMTime
By relaunching the console i had another error.
(Actually it was the same as you can see on the image above).
I checked the SMSProv again, and found this this time:
After checking my function, i noticed i had a typo in my variable “$LogComplianceToWinEvent”.
I correct it. And tadaaa ! It worked !!
So, if there is one thing you need to remeber :
- SMSProv.log (If you are scripting with SCCM)
- Types ! (Especially if you are working with results that are extracted from SQL queries and not from WMI).
- Culture. As in the example of the starttime, it might work during your tests, but the dates could be wrong. Rember that if you work in europe, the SQL times returned are probably not fitting the european datetime format.
But that is 3 things ! ? Yes yes, i know. Doesn’t matter. Let’s say it is one thing that is composed of three elements 🙂