Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Setting object properties

65 views
Skip to first unread message

Joachim Meyer

unread,
Aug 19, 2008, 1:17:34 PM8/19/08
to
Hi,

I'm having trouble setting the properties of an object I am using within a
PowerShell script. The code uses an assembly part of Configuration Manager
2007 (the managed SMS provider) and is based on C# example code included in
the Configuration Manager SDK. I also attached this example code to the
bottom of this posting.

Here's an excerpt from the PowerShell code:

$SMSCILocalizedProperties =
$wqlconnection.CreateEmbeddedObjectInstance('SMS_CI_LocalizedProperties')

$SMSCILocalizedProperties['DisplayName'].StringValue = $UpdateListName
$SMSCILocalizedProperties['LocaleID'].StringValue = $localeid

$newUpdateList = $wqlconnection.CreateInstance('SMS_AuthorizationList')
$newUpdateList['Updates'].IntegerArrayValue = $swupdates

So far the code works even though I do not really understand the last line.
I would have expected that the usual object syntax ($newUpdateList.Updates =
$swupdates) would work in this case but that's apparently not the case.

With the next line I receive an error:

$newUpdateList.SetArrayItems('LocalizedInformation',
@($SMSCILocalizedProperties))

Cannot convert argument "1", with value: "System.Object[]", for
"SetArrayItems" to type
"System.Collections.Generic.List`1[Microsoft.ConfigurationManagement.ManagementProvider.IResultObject]":
"Cannot convert "System.Object[]" to
"System.Collections.Generic.List`1[Microsoft.ConfigurationManagement.ManagementProvider.IResultObject]"."

Any idea how to set this property in PowerShell?

Thanks,
Joachim

PS C:\> $SMSCILocalizedProperties

OverridingObjectClass : SMS_CI_LocalizedProperties
ManagedObject :
Count : 1
DisplayString :
DisplayDescription :
HelpTopic :
ObjectClass : SMS_CI_LocalizedProperties
Properties :
instance of SMS_CI_LocalizedProperties
{
DisplayName = "Test";
LocaleID = 1033;
};

PropertyNames : {Description, DisplayName, InformativeURL, LocaleID}
PropertyList : {Description, DisplayName, InformativeURL, LocaleID}
UniqueIdentifier : 8316e7f5-4f9d-4e8e-9606-e9e08c0b8537
EmbeddedProperties :
EmbeddedPropertyLists :
GenericsArray :
RegMultiStringLists :
SecurityVerbs :
AutoCommit : True
AutoRefresh : True
UserDataObject :
ConnectionManager :
Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine.WqlConnectionManager
TraceProperties : True
NamedValueDictionary : {ProviderLocation, ProviderMachineName, Connection,
ConnectedSiteCode...}
AsyncOperationData :

PS C:\> $newUpdateList

ManagedObject :
EmbeddedProperties :
EmbeddedPropertyLists :
OverridingObjectClass : SMS_AuthorizationList
RegMultiStringLists :
SecurityVerbs : -1
GenericsArray :
Count : 1
DisplayString :
DisplayDescription :
HelpTopic :
ObjectClass : SMS_AuthorizationList
Properties :
instance of SMS_AuthorizationList
{
Updates = {2097, 2115, 2149, 2151, 2169, 2187,
2188, 2206, 2342, 2360, 2373, 2391, 2409, 24
27, 2433, 2451, 2469, 2487, 2546, 2570, 2583, 2601,
2650, 2668, 2686, 2725, 2726, 2744, 2766, 2
780, 2886, 2893, 2922, 2929, 2961, 2969, 2989, 3007,
3035, 3043, 3119, 3137, 3138, 3156, 3250,
3257, 3356, 3363, 3426, 3433, 3453, 3468, 3481,
3489, 3554, 3561, 3590, 3597, 3626, 3633, 3704,
3711, 3810, 3825, 3913, 3928, 3995, 4002, 4119,
4126, 4183, 4265, 4283, 4284};
};

PropertyNames : {ApplicabilityCondition, CategoryInstance_UniqueIDs,
CI_ID, CI_UniqueID...}
PropertyList : {ApplicabilityCondition, CategoryInstance_UniqueIDs,
CI_ID, CI_UniqueID...}
UniqueIdentifier : e045998e-efc6-49b2-9643-51d2b03b5170
AutoCommit : True
AutoRefresh : True
UserDataObject :
ConnectionManager :
Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine.WqlConnectionManager
TraceProperties : True
NamedValueDictionary : {ProviderLocation, ProviderMachineName, Connection,
ConnectedSiteCode...}
AsyncOperationData :


C# example code:

// Prework for CreateSUMUpdateList
// Create array list (to hold the array of Localized Properties).
List<IResultObject> newDescriptionInfo = new List <IResultObject>();
IResultObject SMSCILocalizedProperties =
WMIConnection.CreateEmbeddedObjectInstance("SMS_CI_LocalizedProperties");

// Populate the initial array values
// (this could be a loop to added more localized info).
SMSCILocalizedProperties["Description"].StringValue = "4 CI_IDs - 9,34,53,72
";
SMSCILocalizedProperties["DisplayName"].StringValue = "Test Display Name";
SMSCILocalizedProperties["InformativeURL"].StringValue = "Test URL";
SMSCILocalizedProperties["LocaleID"].StringValue = "1033";

// Add the 'embedded properties' to newDescriptionInfo.
newDescriptionInfo.Add(SMSCILocalizedProperties);

// Create the array of CI_IDs.
int[] newCI_ID = new int[] { 9, 34, 53, 72 };

// Call the CreateSUMUpdateList method.
SUMSnippets.CreateSUMUpdateList(WMIConnection,
newCI_ID,
newDescriptionInfo);

public void CreateSUMUpdateList(WqlConnectionManager connection,
int [] newUpdates,
List<IResultObject> newDescriptionInfo)
{
try
{
// Create the new SMS_AuthorizationList object.
IResultObject newUpdateList =
connection.CreateInstance("SMS_AuthorizationList");

// Populate the new SMS_AuthorizationList object properties.
// Updates is an int32 array that maps to the CI_ID in
SMS_SoftwareUpdate.
newUpdateList["Updates"].IntegerArrayValue = newUpdates;
// Pass embedded properties (LocalizedInformation) here.
newUpdateList.SetArrayItems("LocalizedInformation",
newDescriptionInfo);

// Save changes.
newUpdateList.Put();

Console.WriteLine();
Console.WriteLine("Created Update List. " );

}

catch (SmsException ex)
{
Console.WriteLine("Failed to create update list. Error: " +
ex.Message);
throw;
}
}


/\/\o\/\/

unread,
Aug 20, 2008, 4:32:31 PM8/20/08
to
Cna't test here but can you try to cast it like this :

$SMSCILocalizedProperties =
[System.Collections.Generic.List``1]$SMSCILocalizedProperties

Greetings /\/\o\/\/
http://thePowerShellGuy.com

"Joachim Meyer" <joach...@gmx.de> wrote in message
news:eph7I%23hAJH...@TK2MSFTNGP05.phx.gbl...

Joachim Meyer

unread,
Aug 24, 2008, 7:44:35 AM8/24/08
to
"/\/\o\/\/" <m...@thePowerShellGuy.moc> wrote:
> Cna't test here but can you try to cast it like this :
>
> $SMSCILocalizedProperties =
> [System.Collections.Generic.List``1]$SMSCILocalizedProperties

Thanks for the answer but that did not help either, the script still cannot
convert the object.

I think it's better to start with an array with the proper data type in the
beginning instead of performing the type cast later. However, I figured it's
not so easy doing this with PowerShell (at least for me).

The C# code is:

List<IResultObject> newDescriptionInfo = new List <IResultObject>();

Creating an instance of System.Collections.Generic.List using Strings works:

PS C:\> $a = New-Object "System.Collections.Generic.List``1[System.String]"
PS C:\> $a.gettype()

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True List`1 System.Object

But how do you create an instance of the generic list including objects
defined somewhere else?

PS C:\> $a = New-Object
"System.Collections.Generic.List``1[Microsoft.ConfigurationManagement.ManagementProvider.IResul
t
Object]"
New-Object : Cannot find type
[System.Collections.Generic.List`1[Microsoft.ConfigurationManagement.ManagementProvider.I
ResultObject]]: make sure the assembly containing this type is loaded.
At line:1 char:16
+ $a = New-Object <<<<
"System.Collections.Generic.List``1[Microsoft.ConfigurationManagement.ManagementProvider.IResul
tObject]"


<IResultObject> is defined in
microsoft.configurationmanagement.managementprovider.dll and loaded.

PS C:\> [AppDomain]::CurrentDomain.GetAssemblies() | ? {$_.location -match
"managementprovider.dll"} | select
FullName,GlobalAssemblyCache,ImageRuntimeVersion,ManifestModule | fl


FullName :
Microsoft.ConfigurationManagement.ManagementProvider, Version=4.0.6000.0,
Culture=neutral, Pub
licKeyToken=31bf3856ad364e35
GlobalAssemblyCache : False
ImageRuntimeVersion : v2.0.50727
ManifestModule :
Microsoft.ConfigurationManagement.ManagementProvider.dll

Any ideas on this? Thanks!

Joachim

0 new messages