mgroms
June 19, 2017, 8:29am
1
when I run the above mentioned script in server 2008, I am not getting output with the local user name…pls let me know if you can assist me in that…But its working fine in 2008R2 and also in 2012.
$adsi = [ADSI]"WinNT://$env:COMPUTERNAME"
$adsi.Children | where {$_.SchemaClassName -eq 'user'} | Foreach-Object
{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
$_ | Select-Object @{n='UserName';e={$_.Name}},@{n='Groups';e={[string]::Join(';',$groups)}}
dirk
June 19, 2017, 2:49pm
2
The last line of your code seems not be connected to the rest of the script. You are using $_ outside a pipeline. To get the local user names you could simply your script using:
$adsi = [ADSI]"WinNT://$env:COMPUTERNAME"
$adsi.Children | Where-Object {$_.SchemaClassName -eq 'user'} | ForEach-Object {
$_.Name
}
mgroms
June 19, 2017, 2:59pm
3
Thanks for your reply Dirk. I tried that
$adsi = [ADSI]"WinNT://$env:COMPUTERNAME" $adsi.Children | Where-Object {$_.SchemaClassName -eq 'user'} | ForEach-Object {$_.Name}
I am getting error
Unexpected token 'adsi' in expression or statement.
At line:1 char:48
+ $adsi = [ADSI]"WinNT://$env:COMPUTERNAME" $adsi. <<<< Children | Where-Object {$_.SchemaClassName -eq 'user'} | ForEach-Object {$_.Name}
Anything I can change in that script, pls let me know…
mgroms
June 19, 2017, 3:02pm
4
I also tried this mentioned below -
$adsi = ([ADSI]"WinNT://$env:COMPUTERNAME").Children | Where-Object {$_.SchemaClassName -eq 'user'} | ForEach-Object {$_.Name}
I dont see any output…
dirk
June 19, 2017, 3:05pm
5
It appears, that you have copied and pasted the code into the console instead of running it from ISE. If you do that you should add semicolons:
#ugly "one" liner
$adsi = [ADSI]"WinNT://$env:COMPUTERNAME";$adsi.Children | Where-Object {$_.SchemaClassName -eq 'user'} | ForEach-Object {$_.Name}
Olaf
June 19, 2017, 5:01pm
6
mgroms
June 20, 2017, 7:19am
7
Thanks Dirk. I used the script that you mentioned as well and not getting any output. any advice, pls let me know…
mgroms
June 20, 2017, 7:20am
8
@ Olaf Soyk, Yes, I understand what you mean, but we have few servers with version 1.0. Thats why I am trying to find a solution. Hope you can assist as well…
dirk
June 21, 2017, 12:34am
9
Try it piece by piece. Do you see any output?:
([ADSI]"WinNT://$env:COMPUTERNAME")
Do you see output that has SchemaClassName “User”, when you run this one?:
([ADSI]"WinNT://$env:COMPUTERNAME").Children | select SchemaClassName
Olaf
June 21, 2017, 3:11am
10
What is it actually what you’re trying to do with this code?
mgroms
June 21, 2017, 6:41am
11
Hi Dirk, I only see output as mentioned below -
PS C:\Temp> ([ADSI]"WinNT://$env:COMPUTERNAME")
distinguishedName
-----------------
PS C:\Temp> [ADSI]"WinNT://$env:COMPUTERNAME"
distinguishedName
-----------------
PS C:\Temp> ([ADSI]"WinNT://$env:COMPUTERNAME").Children | select SchemaClassName
SchemaClassName
---------------
mgroms
June 21, 2017, 6:43am
12
Hi Olaf Soyk, I am trying to get the local users from server that has powershell v1.0 using the ADSI.
But I came to know that v1.0 didn’t implement an [ADSI] accelerator…which makes sense…
dirk
June 21, 2017, 9:26am
13
Seems that ADSI doesn’t work on v1. You could use WMI instead:
Get-WmiObject -Class Win32_UserAccount