Author Posts

July 24, 2014 at 11:51 am

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"

July 24, 2014 at 11:55 am

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

July 24, 2014 at 11:57 am

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

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

July 24, 2014 at 12:00 pm

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.

July 24, 2014 at 12:04 pm

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

July 24, 2014 at 12:10 pm

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 ","

}

July 24, 2014 at 12:18 pm

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


}  

July 24, 2014 at 5:48 pm

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.

July 25, 2014 at 5:30 am

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

July 25, 2014 at 5:45 am

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.