keyongtech


  keyongtech > windows.* > windows.powershell > 09/2006

 #1  
08-31-06, 06:53 PM
Alex K. Angelopoulos [MVP]
I'm having trouble finding a simple scheme for getting all objects which are
members of a set B but not also members of another set A.

The specific real problem is a tool for loading all registered snapins
automatically, but only attempting to do so if they are not already loaded.
I could of course do this:
Get-PsSnapin -Registered | Add-PSSnapin -ea SilentlyContinue
for quiet output, but that would be cheating (and would kill errors due to
reasons other than the snapin being already loaded).

We can get all loaded snapins like this:
$Loaded = Get-PSSnapin
and we can get all registered snapins like this:
$Registered = Get-PSSnapin -Registered
but $Loaded will contain any registered snapins that are already loaded.

I initially tried to test for containment, but the -contains operator does
not appear to work correctly for objects; it apparently compares members by
identity, and the PSSnapInInfo objects never test as identical; e.g., even
if all of the registered snapins are already loaded,
Get-PSSnapin -Registered | ?{$Loaded -notcontains $_}
passes through all of the objects.

Trying to do a Select-Object Name on each set doesn't correctly filter
identical items either.

Since -notcontains works fine on a simple array, my current approach is
this:

$Loaded = Get-PSSnapin | ForEach-Object {$_.Name}
Get-PSSnapin -Registered | ForEach-Object {
Where-Object {$Loaded -notcontains $_.Name}
} | Add-PSSnapin



I also checked into using Compare-Object. I like the way it works in terms
of the thinking behind it, but it takes some low-level unboxing to work
right. The shorthand is pretty lengthy:

diff (gsnp) (gsnp -r) | ?{$_.SideIndicator -eq "=>"} | %{
$_.InputObject} | asnp

and a fully-described comparison is very long:
Compare-Object (Get-PSSnapin) (Get-PSSnapin -Registered) |
Where-Object {$_.SideIndicator -eq "=>"} |
ForEach-Object {$_.InputObject} |
Add-PSSnapin

Compare-Object also takes a while to run; this is not really the kind of
thing it was specifically designed to do.

Does anyone have any better ideas on how to approach this?
 #2  
08-31-06, 09:40 PM
Jouko Kynsijärvi
Alex K. Angelopoulos [MVP] wrote:
> I'm having trouble finding a simple scheme for getting all objects
> which are members of a set B but not also members of another set A.


> I also checked into using Compare-Object. I like the way it works in
> terms of the thinking behind it, but it takes some low-level unboxing
> to work right. The shorthand is pretty lengthy:
>
> diff (gsnp) (gsnp -r) | ?{$_.SideIndicator -eq "=>"} | %{
> $_.InputObject} | asnp


Here is another approach:

$a=@(gsnp | ?{ !$_.isdefault }); $a+=@(gsnp -r)
$a | group name | ?{ $_.count -eq 1} | asnp

> Compare-Object also takes a while to run; this is not really the kind
> of thing it was specifically designed to do.


In my testing Get-PSSnapin without -r is the slow one:
[~] (measure-command { get-pssnapin }).milliseconds
714
[~] (measure-command { get-pssnapin -r}).milliseconds
7

....Compare-Object on the other hand is quite fast:
[~] $a = 1..500; $b = 5..505
[~] (measure-command { compare-object $a $b }).milliseconds
61
 #3  
08-31-06, 11:57 PM
Alex K. Angelopoulos [MVP]
"Jouko Kynsijärvi" <jouko.kynsijarvi> wrote in message
news:4232
> Alex K. Angelopoulos [MVP] wrote:
>> I'm having trouble finding a simple scheme for getting all objects
>> which are members of a set B but not also members of another set A.


> Here is another approach:
>
> $a=@(gsnp | ?{ !$_.isdefault }); $a+=@(gsnp -r)
> $a | group name | ?{ $_.count -eq 1} | asnp
>> In my testing Get-PSSnapin without -r is the slow one:

> [~] (measure-command { get-pssnapin }).milliseconds
> 714
> [~] (measure-command { get-pssnapin -r}).milliseconds
> 7
>
> ...Compare-Object on the other hand is quite fast:
> [~] $a = 1..500; $b = 5..505
> [~] (measure-command { compare-object $a $b }).milliseconds
> 61


Whoops. :)
I had a spin-up pause because my Compare-Object had to evaluate Get-PSSnapin
before executing. Duh! :|
Similar Threads
How to copy multi object array contents into single object arrays?

Hi, I have an array with image source and caption object pairs, such as: var library = [{ img: 'img01.jpg', caption: 'Caption 1'}, { img: 'img02.jpg', caption: 'Caption...

Compare-Object and Get the name of object/File?

I want to compare two directories and copy the files that are not in directory $A to directory $B. My initial try to see what I get back: $A = Get-ChildItem -path...

Object-arrays - duplicate object-references

Hi, I've stumbled into a problem I just can't figure out. I found out that variables in JS don't have local scope inside brackets in say, a loop, but things still doesn't add...

Compare-object doesn't seem to work with Arrays

I think I'm doing this right... Any ideas? PS C:\Toolbox\Scripts\DNSCheck>...

Cannot serialize object of type System.Object[,]. Multidimensional arrays are not supported

Hi, I get this on server when trying to retun a 2 dim array. I apprecaite that they are not supported as per [..] however i'm looking for as a work around as my web service...


All times are GMT. The time now is 01:11 AM. | Privacy Policy