Must provide a value expression on the right-hand side of the '-' operator

This topic contains 13 replies, has 3 voices, and was last updated by  VT 1 month, 3 weeks ago.

  • Author
    Posts
  • #73187

    VT
    Participant

    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)}} 
  • #73229

    Dirk
    Participant

    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
    } 
    • #73237

      VT
      Participant

      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...

  • #73244

    VT
    Participant

    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...

  • #73246

    Dirk
    Participant

    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} 
    
    • #73268

      VT
      Participant

      Thanks Dirk. I used the script that you mentioned as well and not getting any output. any advice, pls let me know...

    • #73340

      Dirk
      Participant

      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
    • #73360

      VT
      Participant

      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
      ---------------
      
  • #73252

    Olaf Soyk
    Participant

    Do yourself a favor and use the PowerShell Module to Manage Local Users and Groups 😉 😀

    • #73270

      VT
      Participant

      @ 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...

    • #73352

      Olaf Soyk
      Participant

      What is it actually what you're trying to do with this code?

    • #73361

      VT
      Participant

      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...

    • #73372

      Dirk
      Participant

      Seems that ADSI doesn't work on v1. You could use WMI instead:

      Get-WmiObject -Class Win32_UserAccount
    • #73379

      VT
      Participant

      Thanks Dirk...

You must be logged in to reply to this topic.