(apologies in advance for the wall of text)
I’ve been kicking around PS lately just learning the ropes, trying to do some automation on a W2k12 server.
This particular bit of code seems to be very basic, but I do not understand why it doesn’t work.
First code block. This works ok. It travels along the array and blindly sets the IISIntrinsics checkbox to true for each object.
$foo = @("CDSServices.CDSLogEngine","CDSServices.CDSMiscUtility","CDSServices.CDSSimpleReturnObject","CDSServices.CDSUserAuth","CDSServices.CDSUserLookUp") $count = 0 $comAdmin = New-Object -comobject COMAdmin.COMAdminCatalog $apps = $comAdmin.GetCollection("Applications") $apps.Populate() foreach ($app in $apps) { if ($app.Name -eq "CDS") { $components = $apps.GetCollection("Components",$app.key) $components.Populate() } } foreach ($component in $components) { if ($component.Name -eq $foo[$count]) { $component.Value("IISIntrinsics") = $true $components.SaveChanges() $count++ } }
Second code block. For purposes of this question, I set one of the objects back to false through the UI on the server. If I read through the array again, this time without setting any values, it correctly returns a list of each item state.
$foo = @("CDSServices.CDSLogEngine","CDSServices.CDSMiscUtility","CDSServices.CDSSimpleReturnObject","CDSServices.CDSUserAuth","CDSServices.CDSUserLookUp") $count = 0 $comAdmin = New-Object -comobject COMAdmin.COMAdminCatalog $apps = $comAdmin.GetCollection("Applications") $apps.Populate() foreach ($app in $apps) { if ($app.Name -eq "CDS") { $components = $apps.GetCollection("Components",$app.key) $components.Populate() } } foreach ($component in $components) { if ($component.Name -eq $foo[$count]) { $component.Value("IISIntrinsics") $count++ } } ## output True True True False True
Third code block, where we get to my problem. I want to read through the array again, this time checking each value and if it finds an object with a value if false, then set it to true. Seems simple enough. Yet running this code does absolutely nothing.
$foo = @("CDSServices.CDSLogEngine","CDSServices.CDSMiscUtility","CDSServices.CDSSimpleReturnObject","CDSServices.CDSUserAuth","CDSServices.CDSUserLookUp") $count = 0 $comAdmin = New-Object -comobject COMAdmin.COMAdminCatalog $apps = $comAdmin.GetCollection("Applications") $apps.Populate() foreach ($app in $apps) { if ($app.Name -eq "CDS") { $components = $apps.GetCollection("Components",$app.key) $components.Populate() } } foreach ($component in $components) { if ($component.Name -eq $foo[$count]) { if ($component.Value("IISIntrinsics") -eq $false) { $component.Value("IISIntrinsics") = $true $components.SaveChanges() $count++ } } }
In the second block, we see that the component name is read correctly and its output of the corresponding value is valid, so I know that this is (or should be) reading through the array correctly, yet it is not. It’s as if the comparison in
if ($component.Name -eq $foo[$count]) {
is completely skipped.
This can be shown by adding some output to the comparison loop. “Found match” never appears, so somehow, by adding the second value comparison, it seems to make the name comparison fail.
foreach ($component in $components) { if ($component.Name -eq $foo[$count]) { write-host "found match" if ($component.Value("IISIntrinsics") -eq $false) { $component.Value("IISIntrinsics") = $true $components.SaveChanges() $count++ } } }
I’m probably missing something really simple and stupid, but I just don’t see it.
submitted by /u/mkhopper
[link] [comments]
The post [?] Verifying COM object configuration – updating if needed appeared first on How to Code .NET.