<#
CompareComputerGroupMembershipsFromList.ps1
Powershell script to compare AD ComputerObject Group Memberships in Excel
[string](0..9|%{[char][int](32+("45737569004673677976").substring(($_*2),2))})-replace "\s{1}\b"
save list.txt file in same directory as script with ComputerNames (one per line), example follows:
comp1
comp2
comp3
comp4
#>
cls
function vars { get-variable | sort Name }
vars | remove-item #-whatif
$erroractionpreference = "SilentlyContinue"
Import-Module ActiveDirectory
$currentfolder = split-path $myinvocation.mycommand.path
$groups = ""
$computerlist = GC $currentfolder\list.txt | sort
if (test-path $currentfolder\temp.txt) {remove-item $currentfolder\temp.txt}
if (test-path $currentfolder\MasterGroupList.txt) {remove-item $currentfolder\MasterGroupList.txt}
if (test-path $currentfolder\computerCNs.txt) {remove-item "$currentfolder\computerCNs.txt"}
Foreach ($node in $computerlist)
{
$array = @()
$groups = Get-AdComputer -Identity $node -property "MemberOf"
$sortedGroups = $groups.memberof | Sort | Get-Unique
Foreach($group in $sortedGroups)
{$array +=$group}
$number = $array.count
foreach ($item in $array)
{
$1 = $item -replace "CN\=",""
$2 = $1 -replace "^*,OU\=.*$",""
add-content $currentfolder\temp.txt $2
}
}
$MasterGroupList = gc $currentfolder\temp.txt | sort | Get-Unique
remove-item $currentfolder\temp.txt
foreach ($schlub in $MasterGroupList)
{add-content $currentfolder\MasterGroupList.txt $schlub}
# some Excel Constants
# line styles
$xlLineStyleNone = -4142
$xlContinuous = 1
$xlDash = -4115
$a = New-Object -comobject Excel.Application
$a.Visible = $true
$b = $a.Workbooks.Add()
$WS=$a.ActiveSheet
$cells = $WS.Cells
$date = Get-Date
#define some variables to control navigation
$row=1
$col=2
$WS.UsedRange.Borders.LineStyle = $xlContinuous
$blue = 255
$green= 0
$red = 0
$a = 1
$WS.UsedRange.Borders.Color = $a
$WS.UsedRange.Borders.Weight = $xlThin
$WS.Application.ActiveWindow.SplitColumn = 1
$WS.Application.ActiveWindow.SplitRow = 1
$WS.Application.ActiveWindow.FreezePanes=$true
#insert column headings
GC $currentfolder\MasterGroupList.txt | foreach {
$cells.item($row,$col)=$_
$cells.item($row,$col).font.bold=$False
$cells.item($row,$col).Orientation = 90
$cells.EntireColumn.AutoFit() | Out-Null
$WS.UsedRange.Borders.LineStyle = $xlContinuous
$col++
}
$row=2
$col=1
$blarb = Get-Content ("$currentfolder\list.txt") | Sort | Get-Unique
foreach ($slarb in $blarb)
{
$CN = Get-AdComputer -Identity $slarb
Add-Content "$currentfolder\computerCNs.txt" $CN
}
$GroupList = @{}
Function IsMember ($ADObject, $GroupName)
{
If ($GroupList.ContainsKey($ADObject.sAMAccountName.ToString() + "\") -eq $False)
{
$GroupList.Add($ADObject.sAMAccountName.ToString() + "\", $True)
$ADObject.psbase.RefreshCache("tokenGroups")
$SIDs = $ADObject.psbase.Properties.Item("tokenGroups")
ForEach ($Value In $SIDs)
{
$SID = New-Object System.Security.Principal.SecurityIdentifier $Value, 0
$Group = $SID.Translate([System.Security.Principal.NTAccount])
$GroupList.Add($ADObject.sAMAccountName.ToString() + "\" + $Group.Value.Split("\")[1], $True)
}
}
If ($GroupList.ContainsKey($ADObject.sAMAccountName.ToString() + "\" + $GroupName)) {Return $True}
Else {Return $False }
}
$computerCNs = Get-Content("$currentfolder\ComputerCNs.txt")
foreach ($item in $computerCNs)
{
$Computer = [ADSI]"LDAP://$item"
$3 = $item -replace "CN\=",""
$4 = $3 -replace "^*,OU\=.*$",""
$x=1
$y=2
foreach ($habba in Get-content $currentfolder\MasterGroupList.txt)
{
If (IsMember $Computer $habba -eq $True)
{ write-host Yes,$habba,$Computer.sAMAccountName -Foregroundcolor green
$cells.item($row,$col)=$4
$cells.EntireColumn.AutoFit() | Out-Null
$cells.item($row,$col).font.bold=$True
$WS.Cells.Item($row,$y).Value() =1;$WS.Cells.Item($row,$y).Interior.ColorIndex = 34
}
Else
{
write-host No,$habba,$Computer.sAMAccountName -Foregroundcolor red
}
$y++
$WS.UsedRange.Borders.LineStyle = $xlContinuous
}
$Row++
}
if (test-path $currentfolder\MasterGroupList.txt) {remove-item $currentfolder\MasterGroupList.txt}
if (test-path $currentfolder\computerCNs.txt) {remove-item "$currentfolder\computerCNs.txt"}
Function Convert-NumberToA1
{
Param([parameter(Mandatory=$true)]
[int]$number)
$a1Value = $null
While ($number -gt 0) {
$multiplier = [int][system.math]::Floor(($number / 26))
$charNumber = $number - ($multiplier * 26)
If ($charNumber -eq 0) { $multiplier-- ; $charNumber = 26 }
$a1Value = [char]($charNumber + 64) + $a1Value
$number = $multiplier
}
Return $a1Value
}
# End Function
Function xlSum
{
$range = $WS.usedRange
$rows = $range.rows.count # Takes you to the last used row
$cols = $range.columns.count # Takes you to the last used column
$Sumrow = $rows + 1
$Sumcol = $cols + 1
$Q=2
$U=2
do
{
$Y=2
$p = Convert-NumberToA1 ($Q)
$r = ("" + $p + $Y + ":" + $p + $rows)
write-host range $r
$derp = $WS.Range($r)
write-host $Q$Sumrow
$WS.cells.item($SumRow,$Q) = $functions.sum($derp)
[void]$range.entireColumn.Autofit()
$Q++
}
while($Q -lt $Sumcol)
do
{
$G = Convert-NumberToA1 ($cols)
$r = ("" + "B" + $U + ":" + $G + $U)
write-host range $r
$derp = $WS.Range($r)
write-host $U$SumCol
$WS.cells.item($U,$Sumcol) = $functions.sum($derp)
[void]$range.entireColumn.Autofit()
$U++
}
while($U -lt $Sumrow)
$WS.cells.item($SumRow,$SumCol) = $functions.sum($range)
$cells.EntireColumn.AutoFit() | Out-Null
}
# End Function
xlSum
#End Script
CompareComputerGroupMembershipsFromList.ps1
Powershell script to compare AD ComputerObject Group Memberships in Excel
[string](0..9|%{[char][int](32+("45737569004673677976").substring(($_*2),2))})-replace "\s{1}\b"
save list.txt file in same directory as script with ComputerNames (one per line), example follows:
comp1
comp2
comp3
comp4
#>
cls
function vars { get-variable | sort Name }
vars | remove-item #-whatif
$erroractionpreference = "SilentlyContinue"
Import-Module ActiveDirectory
$currentfolder = split-path $myinvocation.mycommand.path
$groups = ""
$computerlist = GC $currentfolder\list.txt | sort
if (test-path $currentfolder\temp.txt) {remove-item $currentfolder\temp.txt}
if (test-path $currentfolder\MasterGroupList.txt) {remove-item $currentfolder\MasterGroupList.txt}
if (test-path $currentfolder\computerCNs.txt) {remove-item "$currentfolder\computerCNs.txt"}
Foreach ($node in $computerlist)
{
$array = @()
$groups = Get-AdComputer -Identity $node -property "MemberOf"
$sortedGroups = $groups.memberof | Sort | Get-Unique
Foreach($group in $sortedGroups)
{$array +=$group}
$number = $array.count
foreach ($item in $array)
{
$1 = $item -replace "CN\=",""
$2 = $1 -replace "^*,OU\=.*$",""
add-content $currentfolder\temp.txt $2
}
}
$MasterGroupList = gc $currentfolder\temp.txt | sort | Get-Unique
remove-item $currentfolder\temp.txt
foreach ($schlub in $MasterGroupList)
{add-content $currentfolder\MasterGroupList.txt $schlub}
# some Excel Constants
# line styles
$xlLineStyleNone = -4142
$xlContinuous = 1
$xlDash = -4115
$a = New-Object -comobject Excel.Application
$a.Visible = $true
$b = $a.Workbooks.Add()
$WS=$a.ActiveSheet
$cells = $WS.Cells
$date = Get-Date
#define some variables to control navigation
$row=1
$col=2
$WS.UsedRange.Borders.LineStyle = $xlContinuous
$blue = 255
$green= 0
$red = 0
$a = 1
$WS.UsedRange.Borders.Color = $a
$WS.UsedRange.Borders.Weight = $xlThin
$WS.Application.ActiveWindow.SplitColumn = 1
$WS.Application.ActiveWindow.SplitRow = 1
$WS.Application.ActiveWindow.FreezePanes=$true
#insert column headings
GC $currentfolder\MasterGroupList.txt | foreach {
$cells.item($row,$col)=$_
$cells.item($row,$col).font.bold=$False
$cells.item($row,$col).Orientation = 90
$cells.EntireColumn.AutoFit() | Out-Null
$WS.UsedRange.Borders.LineStyle = $xlContinuous
$col++
}
$row=2
$col=1
$blarb = Get-Content ("$currentfolder\list.txt") | Sort | Get-Unique
foreach ($slarb in $blarb)
{
$CN = Get-AdComputer -Identity $slarb
Add-Content "$currentfolder\computerCNs.txt" $CN
}
$GroupList = @{}
Function IsMember ($ADObject, $GroupName)
{
If ($GroupList.ContainsKey($ADObject.sAMAccountName.ToString() + "\") -eq $False)
{
$GroupList.Add($ADObject.sAMAccountName.ToString() + "\", $True)
$ADObject.psbase.RefreshCache("tokenGroups")
$SIDs = $ADObject.psbase.Properties.Item("tokenGroups")
ForEach ($Value In $SIDs)
{
$SID = New-Object System.Security.Principal.SecurityIdentifier $Value, 0
$Group = $SID.Translate([System.Security.Principal.NTAccount])
$GroupList.Add($ADObject.sAMAccountName.ToString() + "\" + $Group.Value.Split("\")[1], $True)
}
}
If ($GroupList.ContainsKey($ADObject.sAMAccountName.ToString() + "\" + $GroupName)) {Return $True}
Else {Return $False }
}
$computerCNs = Get-Content("$currentfolder\ComputerCNs.txt")
foreach ($item in $computerCNs)
{
$Computer = [ADSI]"LDAP://$item"
$3 = $item -replace "CN\=",""
$4 = $3 -replace "^*,OU\=.*$",""
$x=1
$y=2
foreach ($habba in Get-content $currentfolder\MasterGroupList.txt)
{
If (IsMember $Computer $habba -eq $True)
{ write-host Yes,$habba,$Computer.sAMAccountName -Foregroundcolor green
$cells.item($row,$col)=$4
$cells.EntireColumn.AutoFit() | Out-Null
$cells.item($row,$col).font.bold=$True
$WS.Cells.Item($row,$y).Value() =1;$WS.Cells.Item($row,$y).Interior.ColorIndex = 34
}
Else
{
write-host No,$habba,$Computer.sAMAccountName -Foregroundcolor red
}
$y++
$WS.UsedRange.Borders.LineStyle = $xlContinuous
}
$Row++
}
if (test-path $currentfolder\MasterGroupList.txt) {remove-item $currentfolder\MasterGroupList.txt}
if (test-path $currentfolder\computerCNs.txt) {remove-item "$currentfolder\computerCNs.txt"}
Function Convert-NumberToA1
{
Param([parameter(Mandatory=$true)]
[int]$number)
$a1Value = $null
While ($number -gt 0) {
$multiplier = [int][system.math]::Floor(($number / 26))
$charNumber = $number - ($multiplier * 26)
If ($charNumber -eq 0) { $multiplier-- ; $charNumber = 26 }
$a1Value = [char]($charNumber + 64) + $a1Value
$number = $multiplier
}
Return $a1Value
}
# End Function
Function xlSum
{
$range = $WS.usedRange
$rows = $range.rows.count # Takes you to the last used row
$cols = $range.columns.count # Takes you to the last used column
$Sumrow = $rows + 1
$Sumcol = $cols + 1
$Q=2
$U=2
do
{
$Y=2
$p = Convert-NumberToA1 ($Q)
$r = ("" + $p + $Y + ":" + $p + $rows)
write-host range $r
$derp = $WS.Range($r)
write-host $Q$Sumrow
$WS.cells.item($SumRow,$Q) = $functions.sum($derp)
[void]$range.entireColumn.Autofit()
$Q++
}
while($Q -lt $Sumcol)
do
{
$G = Convert-NumberToA1 ($cols)
$r = ("" + "B" + $U + ":" + $G + $U)
write-host range $r
$derp = $WS.Range($r)
write-host $U$SumCol
$WS.cells.item($U,$Sumcol) = $functions.sum($derp)
[void]$range.entireColumn.Autofit()
$U++
}
while($U -lt $Sumrow)
$WS.cells.item($SumRow,$SumCol) = $functions.sum($range)
$cells.EntireColumn.AutoFit() | Out-Null
}
# End Function
xlSum
#End Script