ADSI Local Group Enum From Fancy Powershell to Simple Foreach Rewrite
hey all,
i've been racking brain couple days , think must not understanding code in question.
the script wish write connects machine, local or otherwise, evaluate member of local administrators group on machine in question. the first block of code works fine not understand foreach loop decided rewrite more readable , more traditional foreach loop. goal rewrite following:
$group =[adsi]"winnt://127.0.0.1/administrators" $members = @($group.psbase.invoke("members")) $members | foreach {$_.gettype().invokemember("name", 'getproperty', $null, $_, $null)}
i rewrite form below:
$objcomputer = "127.0.0.1" $enumgrp = "administrators" $objgroup =[adsi]"winnt://$objcomputer/$enumgrp" $members = @($objgroup.psbase.invoke("members")) foreach ($item in $members) { write-host $item.gettype().getproperty("name") }
i have couple of reasons doing this;
- i don't understand why invokemember requires 5 arguments, although i've guessed why needed first two.
- the first code block spits out block of text. i want more granular control on foreach loop add more code once i'm returning users of group.
1. can check methods overload directly powershell. try $members[0].gettype().invokemember
(without brackets should show definition of method.
clearly that's last overload available (name, invokeattr, binder, target, args) used method.
there no overload 2 arguments. , without target - can't anything... :)
2. fix foreach:
foreach ($member in $members) { $member.gettype().invokemember('name','getproperty',$null,$member,$null) }
other names work here: http://msdn.microsoft.com/en-us/library/aa705950(v=vs.85).aspx
hth
bartek
Windows Server > Windows PowerShell
Comments
Post a Comment