We know by now how easy it is to work with Active Directory leveraging the many AD commandlets. However, what about when it becomes necessary to get information about a local account or group on a specific computer.
Get a list of administrators on a specific computer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | $Computer = "COMPUTERNAME" TRY { ## OPEN TRY $ADSIComputer = [ADSI]("WinNT://" + $Computer + ",Computer") $AdminGroup = $ADSIComputer.PSbase.Children.Find("Administrators") $AdminGroupMembers= $AdminGroup.PSbase.Invoke("Members") | %{$_.GetType().InvokeMember("Name", 'GetProperty', $Null, $_, $Null)} } ## CLOSE TRY CATCH { Write-Warning "Unable to get Administrators Group Membership on $Computer `r " } Write-Output "$Computer Administrators: `r " Write-Output "========================= `r " Write-Output " `r " ForEach ($Member in $AdminGroupMembers) { ## OPEN ForEach Member in AdminGroupMembers $Member } ## CLOSE ForEach Member in AdminGroupMembers Write-Output " `r " |
Check for Domain Admins in the local Administrators group on a specific computer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | $Computer = "COMPUTERNAME" $AdminGroup = [ADSI]"WinNT://$Computer/Administrators,group" TRY { $AdminGroupMembers = @($AdminGroup.psbase.Invoke("Members")) } CATCH { ## OPEN Catch Write-Warning "Could not connect to $Computer. It may be offline. `r " } ## CLOSE Catch IF ($AdminGroupMembers) { ## OPEN IF AdminGroupMembers contains data $AdminGroupMembers | ForEach-Object ` { [array]$AdminGroupMemberList += $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null) } ForEach ($Member in $AdminGroupMemberList) { ## OPEN ForEach Member in MemberNames IF ($Member -like "*Domain Admins*") { Write-Output "Domain Admins has Administrative rights to $Computer } } ## CLOSE ForEach Member in MemberNames |
Get the name of the default local administrator account
1 2 3 4 | $Computer = "COMPUTERNAME" $LocalAdminInfo = Get-WmiObject –Query ‘Select * from Win32_UserAccount Where (LocalAccount="True" and SID like "%-500")’ -ComputerName $Computer $LocalAdmin = $LocalAdminInfo.Name Write-Output "The local administrator account on $Computer is $LocalAdmin `r " |
Set a local admin’s password:
1 2 3 4 5 6 7 | $Computer = "COMPUTERNAME" $LocalAdmin = "Administrator" $AdminPassword = "NewAdminPassword999!" $Admin = [adsi]("WinNT://$Computer/$LocalAdmin, user") Write-Output "Attempting to change password for $computer\$LocalAdmin..." `r $Admin.psbase.invoke("SetPassword", "$AdminPassword") |
Create new user on computer:
1 2 3 4 5 6 7 8 9 10 | $Computer = "COMPUTERNAME" $User = "NewUser" $Password = "NewPassword999!" Write-Verbose "Creating $User on $Computer `r " $ADSIUserCreate = $ADSIComputer.Create("User", $User) $ADSIUserCreate.setpassword($password) $ADSIUserCreate.SetInfo() $ADSIUserCreate.description = "" $ADSIUserCreate.SetInfo() |
Add a user to Administrators Group:
1 2 3 4 5 6 7 8 9 | $DomainDNS = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().Name $Computer = "COMPUTERNAME" $User = "NewUser" Write-Verbose "Adding $User to Administrators group on $Computer `r " IF ($Computer -like "*$DomainDNS") { $Computer = $Computer -replace(".$DomainDNS") } Write-Verbose "Adding $User to Administrators group on $Computer `r " $objUser = [ADSI]("WinNT://$Computer/$User") $objGroup = [ADSI]("WinNT://$Computer/Administrators") $objGroup.PSBase.Invoke("Add",$objUser.PSBase.Path) |