Exported users from AD into xlsx file with each group in a seperate sheet. I am attempting to simply loop through the sheets and list the users for each group (sheet).
the script works great…except the first 2 iterations. The 1st and 2nd iteration results are combined into 1.
$WBPath = "R:\group-export.xlsx"
write-host " Starting data import"
#gets names of all sheets from workbook
$sheets = (Get-ExcelSheetInfo -Path $WBPath).Name
$allusers = @()
foreach ($sheet in $sheets) {
$data = @()
Write-Host " "
Write-Host " Getting Data for "$sheet
Write-Host " "
$data = Import-Excel -WorksheetName $sheet -Path $WBPath
start-sleep -seconds 1
#if allusers array doesn't have name in it yet, add it
if($alldata -notcontains $data.Name){
#exclude Write-protected
if($data.Name -notcontains "Write-protected" -or "-Write Protected"){
$allusers += $data.Name
}
}
#echo data table
$data
}
Write-Host " "
write-host "Data Imported"
Write-Host " "
start-sleep -seconds 2
Write-Host " "
write-host "Listing Users"
Write-Host " "
$allusers
The first 2 groups are called Accouting and Administrative.
I get the follow result
Starting data import
Getting Data for Accounting
Getting Data for Administrative
SamAccountName Name
-------------- ----
debbie.barnes Debbie Barnes
millie.edwards Millie Edwards
robyn.cartwright Robyn Cartwright
amanda.sanford Amanda Sanford
contract Contract Employee
accounting.contract Accounting Contract
amy.west Amy West
randy.atherton Randy Atherton
ocvadmin ocvadmin
The problem is the result should look like this.
Starting data import
Getting Data for Accounting
SamAccountName Name
-------------- ----
debbie.barnes Debbie Barnes
millie.edwards Millie Edwards
robyn.cartwright Robyn Cartwright
amanda.sanford Amanda Sanford
contract Contract Employee
accounting.contract Accounting Contract
amy.west Amy West
Getting Data for Administrative
randy.atherton Randy Atherton
ocvadmin ocvadmin
As you can see the first iteration for the Accounting group does not print data, the 2nd iteration for Administrative is printing Both Accounting and Administrative sheet data combined.
This is only happening at the start of the loop. There are several more sheets in the source .xlsx file, but only the first 2 iterations are not behaving correctly.
What am I doing wrong???