Question with Object Array

Sterling33

Member
Joined
Nov 17, 2005
Messages
12
Programming Experience
3-5
Hi all,
I was wondering if anyone could possibley help me with the code added below? It seems that I have an issue where I manage to go through the For Loop the first time with no issues, but manage to get an 'Unknow Error' when on the second Loop. I managed to isolate the problem to the line in red. It seems that during the first run through of the loop the instance is created and added as an object of the object array with no issues, but during the next loop around it does not. The instance seems not to be created and/or placed in the next element of the array. Is there something im missing?

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] instOSDetails(2) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] SWbemObject
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2][COLOR=#000000] Services [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#000000] SWbemServices[/COLOR]
[/SIZE]Dim[/COLOR][/SIZE][SIZE=2] Locator [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SWbemLocator[/SIZE]
 
[SIZE=2]Service = Locator.ConnectServer(Namespace, UserName, Password)[/SIZE]
[SIZE=2]
[/SIZE][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] 2
   [COLOR=red]instOSDetails(i) = Services.Get("SMS_OS_Details").SpawnInstance_[/COLOR]
   instOSDetails(i).MaxVersion = "5.99.999"
   instOSDetails(i).MinVersion = "5.00.000"
   instOSDetails(i).name = "Win NT"
   instOSDetails(i).Platform = "I386"
[/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] i
[/SIZE]

Any help would be great. Thanks,
- Sterling
 
Presumably the issue is in the Get or SpawnInstance_ member. Do you have the source code for them, or are they in compiled assemblies that are not of your own creation?
 
This is the old Microsoft WMI Scripting Library COM component (WBemScripting.dll).
Dare I suggest using .Nets System Management for handling WMI ?
 
Question with ManagementPath

I have changed over to .Nets System Management to handle WMI, but am having trouble translating the following:
VB.NET:
Dim strPackageID as String
Dim instPath as SWbemObjectPath
Dim instPackage as SWbemObject
 
instPath = instPackage.Put_
[COLOR=red]strPackageID = instPath.Keys("PackageID")[/COLOR]

PackageID is a unique, auto-generated key thats generated when a SMS package is created. I am able to create a package instance, but am unable to get a PackageID back. I tried using ManagementPath in place of SWbemObjectPath, but the object does not seem to have the 'Key' property like SWbemObjectPath does. Does anyone have a clue how I can do this?

Thanks,
- Sterling
 
I find very little info/sources about this, actually only 7 hits in Google for SMS_OS_Details - well, just thought VB.Net could do the same.. perhaps not. Since there is virtually nothing on the web about this, the few hits mention vbscript as common use, wouldn't there normally be some other way to do it? just thinking in the loud..
There is an example in MSDN with c++ and vb6 sources, but that is where the question originated from probably.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sms/hh/sms/usingsms_8do3.asp
If you still decide to stick with this, have a look over the example linked above. I notice that instOSDetails is an array that is eventually put into instProgram.SupportedOperatingSystems and defines the operating systems supported (one SMS_OS_Details for each). Not that it should have mattered.. but you tried to create 3 of these with same info, you did only need one element in array for that.
 
Hehe..The link you provided is actually the exact article I was referencing when I was using the Wbem Scripting Objects. After reading your comment regarding using the System Management Object, I proceeded to try to translate the code I had. So far I am able to successfull create a SMS package with the following code:

VB.NET:
[SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Create([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] strServerName [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] strNamespace [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2],_ [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] strUserName [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] strPassword [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2] 
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] WMIConnectionOptions [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] ConnectionOptions
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] WMIManagementScope [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] ManagementScope[/SIZE]
[SIZE=2] 
[/SIZE][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE][SIZE=2] WMIConnectionOptions
   .Impersonation = ImpersonationLevel.Impersonate
   .Authentication = AuthenticationLevel.Packet
   .Username = strUserName
   .Password = strPassword
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff] 
[/COLOR][/SIZE][SIZE=2]WMIManagementScope = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] ManagementScope("\\" & strServerName &_ "\root\" & strNamespace, WMIConnectionOptions)[/SIZE]
[SIZE=2] 
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] SMS_PackageClass [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] ManagementClass(WMIManagementScope,_ [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] ManagementPath("SMS_Package"), [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2] 
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] instPackage [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] ManagementObject =SMS_PackageClass.CreateInstance
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] instPath [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] ManagementPath[/SIZE]
[SIZE=2] 
instPackage("Description") = Description
instPackage("Manufacturer") = Manufacturer
instPackage("Version") = Version
instPackage("Language") = Language
instPackage("Name") = ApplicationName
instPackage("PkgSourceFlag") = PkgSourceFlag
 
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] PkgSourceFlag <> 1 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]   instPackage("PkgSourcePath") = PkgSourcePath
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff] 
[/COLOR][/SIZE][SIZE=2]instPackage("ShareType") = ShareType
[/SIZE][SIZE=2][COLOR=#0000ff][/COLOR][/SIZE] 
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] ShareType = 2 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]   instPackage("ShareName") = ShareName
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff] 
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] ForcedDisconnectEnabled = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]   instPackage("ForcedDisconnectEnabled") = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2]instPackage("ForcedDisconnectNumRetries") = ForcedDisconnectNumRetries
   instPackage("ForcedDisconnectDelay") = ForcedDisconnectDelay
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff] 
[/COLOR][/SIZE][SIZE=2]instPackage("PreferredAddressType") = PreferredAddressType
instPackage("Priority") = Priority
instPath = instPackage.Put()

[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

At this point the only problem I have now is actually getting the PackageID back from the created package instance.

According to the Microsoft article you provided the following code would use the returned object path to get the PackageID:

VB.NET:
Set instPath = instPackage.Put_
[COLOR=red]PackageID = instPath.Keys("PackageID")[/COLOR]

The part in red is where I think Im really stuck at cause there does not seem to be a way to get a Key qualifier using System Management Objects. Of course I might be wrong.
 
how about this?
VB.NET:
instPath = instPackage.Put()
Dim pid as new managementobject(instpath)
Dim PackageID As String = pid("PackageID").toString
 
I added the code to the end of mine and got a Access Denied error message once I ran it. I may have to fiddle around a bit to see. Although I can't understand why I am getting that message since we are only adding a new Management Object. The reference to the Managment Scope and options should still be valid in this instance. As soon as I test it more I'll let you know. By the way John, thanks for giving me a hand with this. Your help is much appreciated.

-Sterling
 
Edit: Did you get access denied with Put or when trying to get the value?

Check instPackage, the property and value should already be available there, I then presume that scope here relates to what they call context.
VB.NET:
Dim PackageID As String = instPackage("PackageID").toString
Reading the first VB6 comment in the MSDN article again, they say they assume something and they also add SMSContext when putting. From what I understand, this adds some info in PackageID property relating to the context. This is not explained nor in code. Neither does ManagementObject.Put method provide input of anything context.
Like I said, I presume that management scope is what they refer to as context.

Edit: You can do this before you Put.
 
Last edited:
It looks like the 'Access Denied' error comes after the Put Method, because I see the package is getting created in SMS. After the package is applied I get the error.

On a different note, I was able to extract the PackageID from the instPath Object by converting to string:

VB.NET:
[SIZE=2]
temp = instPath.ToString
[/SIZE]

From that I got a long string which included the ID. Not a clean way of doing it I know, but it did work.
 
I don't understand why you can't get it with: instPackage("PackageID").toString

The PackageID property is autogenerated, other properties you did set value for instance "instPackage("Description") = Description".

Some more, I found where the context stuff is in WMI. When creating scope you gave an empty WMIConnectionOptions, this object got a Context property of type ManagementNamedValueCollection, where you Add(name,value).

here is the reference for SMS_Package, if you didn't find it already:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/smssdk03/hh/sms/refsms23_2aw5.asp

here reference for context (sorry, but I don't understand what they want for SMS_Package):
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/smssdk03/hh/sms/refsms10_4c8j.asp
 
Back
Top