Thursday, March 30, 2017

Add every member of the Users group to the Hyper-V Administrators group.


I recently had a challenge to make Hyper-V available to everyone on a PC and it had to work in other languages. So net localgroup is out since you have to use the SID of the groups. Here is the PowerShell script I used for that.






#Gets all of the local groups
$GetGroups = Get-WmiObject -Query "select * from win32_group where Domain='$env:computername'"

#Selects the Hyper-V Administrators group from the array using the well known SID
$hyperVgroup= $GetGroups | Where-Object {$_.SID -eq 'S-1-5-32-578'}
$HVgroupName= $hyperVgroup.Name

#Selects the Users group from the array using the well known SID
$UserGroup = $GetGroups | Where-Object {$_.SID -eq 'S-1-5-32-545'}
$Usergroupname = $UserGroup.Name

#Gets the members of the Users group.
$Group = [ADSI]"WinNT://$env:Computername/$usergroupname,group"
if ($Group.Path)
            {
                $Members = @($Group.psbase.Invoke("Members"))
                [Array]$MemberNames = $Members |
                    ForEach-Object {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)} }

<#Takes the members from the Users group and puts them into the Hyper-V Admin group.
This will result in errors since some of the accounts are not "NT Authority" #>
foreach($member in $MemberNames){
([adsi]”WinNT://./$HVgroupName,group”).Add(“WinNT://NT Authority/$member,user”) > $null
}

No comments:

Post a Comment