Adding Variable to Hash Table.

This topic contains 9 replies, has 3 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 2 years, 4 months ago.

  • Author
    Posts
  • #17459
    Profile photo of Braven36 .
    Braven36 .
    Participant

    How would I add $hashAdmin to Hash Table?

    PS C:\Windows\system32> $hashadmin.gettype()

    IsPublic IsSerial Name BaseType
    ——– ——– —- ——–
    True True String[] System.Array

    PS C:\Windows\system32> $hashAdmin
    Domain="village"
    Name="catman"

  • #17460
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    $hashAdmin is apparently an array, not a hashtable, according to your output. What are you trying to do with it?

  • #17461
    Profile photo of Adam Bertram
    Adam Bertram
    Participant

    $ht = @{}
    $ht.SomeArray = $hashadmin

    You can add it just like any other element just as long as you assign it a key.

  • #17462
    Profile photo of Braven36 .
    Braven36 .
    Participant

    Would like to convert that variable to a object. I figured since it already in the correct hashtable format there should be a way to just add it.

    $obj= New-object PSobject -property $hashadmin

    But I think it still see it as an array.

  • #17463
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    I see. As luck would have it, your strings are in a format which makes it possible to use the ConvertFrom-StringData cmdlet (which will give you your hashtable):

    $stringArray = [string[]]@(
        'Domain="village"'
        'Name="catman"'
    )
    
    # at this point, $stringArray is the same as the $hashAdmin variable
    # from your original post.
    
    # ConvertFrom-StringData expects a single string, not an array, so we
    # join the array into multiple lines:
    
    $string = $stringArray -join "`r`n"
    
    $hashtable = ConvertFrom-StringData -StringData $string
    
    # Now, you can create a psobject from the hashtable.
    
    $object = New-Object psobject -Property $hashtable
    
    $object
    
  • #17464
    Profile photo of Braven36 .
    Braven36 .
    Participant

    Below is the code so far. I would like to add the value of the $hashAdmin to a PSobject. Because the array is already in the correct key = value format . Simple said I want to convert it from an array to a hash table

    $adminMember= Get-wmiobject win32_groupuser | where {$_.groupcomponent –like '*"Administrators"'}

    $hash=$null
    $hash=@{}

    Foreach($admin in $adminMember ){

    $groupComponent = $admin.Partcomponent

    $hashAdmin= ($groupComponent -split "\.")[1] -split ","

    }

  • #17465
    Profile photo of Braven36 .
    Braven36 .
    Participant

    That worked perfectly.
    output
    Domain Name
    —— —-
    "SecretComputername" "SecretUser"
    "almosttheweekend" "Domain Admins"

    
      
    $adminMember= Get-wmiobject win32_groupuser | where {$_.groupcomponent –like '*"Administrators"'}  
    
      
    
    $hash=$null
    $hash=@{}
    
    Foreach($admin in $adminMember ){
    
    $groupComponent = $admin.Partcomponent 
    
    
    $hashAdmin= ($groupComponent -split "\.")[1] -split ","
    
    
    $string= $hashAdmin -join "`r`n"
    
    $hashtable = ConvertFrom-StringData -StringData $string
     
    # Now, you can create a psobject from the hashtable.
     
    $object = New-Object psobject -Property $hashtable
     
    $object
    
    
    }  
  • #17466
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    You can get that same information quite a bit more easily, if you like (without resorting to string parsing steps):

    $group = Get-WmiObject Win32_Group -Filter 'Name = "Administrators"'
    $group.GetRelated('Win32_UserAccount') | Select-Object -Property Domain, Name
    

    Edit: I tested this on a home computer that's not a member of a domain. For domain-joined computers, you may need to modify it slightly to make sure Get-WmiObject returns the correct group.

  • #17474
    Profile photo of Braven36 .
    Braven36 .
    Participant

    I need to check for local admin account membership on my system. For some reason the code is not exiting when complete. Any idea why it might not exit?

    Thanks
    Jon

  • #17475
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    If you're in a domain environment, using WMI to check for group memberships (or anything account-related, for that matter) can take a ridiculously long time. I'd recommend using ADSI instead, or if you just want to display the information on screen, you can just run the old "net localgroup Administrators" command.

You must be logged in to reply to this topic.