Question

hoping to get some help with what i'am trying to achieve here.

i have two arrays $ActiveUsers and $InactiveUsers with 4 fields and following data in each

$ActiveUsers = fahe532|Allyson Sullivan|34563|Cain,Ashley J
               pers274|Martha Walsh|53732|Mckenny,Josh
               fgh8458|Kayla Benson|95463|Trot,Cook K
               ndcf846|Sam Huff|56737|Mcghee,John
               vdfg456|Mark Jones |67843|Hart,Josh W
               fasf345|Amber Sean|24678|John,Kneel G

$InavtiveUsers = fasd034|Colin Hart|35473|David, Casper
                 aertp56|Andy Matthews|56849|Debby, Gould K
                 ahshb86|Mark Michael|46848|Ty, Justin H
                 gkr5057|Josh Meeker|56848|Ashley, Rhonda R
                 irrk106|Tom Mortin|64838|Becks, Alan
                 eqer348|Nathan Willis|469894|Baker ,John T

Now, i'am trying to pull each row from $ActiveUsers and adding it to a excel file i'am creating. I did the same for each row in $InactiveUsers and added them into a new Excel file

There was a slight change in the requirement, instead of having two arrays added into two Excel files i have to put everything in one excel file, Also i need to highlight(with some color) the rows i'am getting from $InactiveUsers in that one excel file where i'am writing everything.

Is there a way i can loop through two arrays simultaneoulsly and write rows into one excel file by higlighting the rows i get from $InactiveUsers? Below is what i wrote so far

$ExcelObject = new-Object -comobject Excel.Application  
$ExcelObject.visible = $false 
$ExcelObject.DisplayAlerts =$false
$date= get-date -format "yyyyMMddHHss"
$strPath1="o:\UserCert\Active_Users_$date.xlsx" 
if (Test-Path $strPath1) {  
  #Open the document  
$ActiveWorkbook = $ExcelObject.WorkBooks.Open($strPath1)  
$ActiveWorksheet = $ActiveWorkbook.Worksheets.Item(1)  
} else {  
# Create Excel file  
$ActiveWorkbook = $ExcelObject.Workbooks.Add()  
$ActiveWorksheet = $ActiveWorkbook.Worksheets.Item(1)  

#Add Headers to excel file
$ActiveWorksheet.Cells.Item(1,1) = "User_Id"  
$ActiveWorksheet.cells.item(1,2) = "User_Name" 
$ActiveWorksheet.cells.item(1,3) = "CostCenter"
$ActiveWorksheet.cells.item(1,4) = "Approving Manager"
$format = $ActiveWorksheet.UsedRange
$format.Interior.ColorIndex = 19
$format.Font.ColorIndex = 11
$format.Font.Bold = "True"
} 
#Loop through the Array and add data into the excel file created.
foreach ($line in $Activeusers){
     ($user_id,$user_name,$Costcntr,$ApprMgr) = $line.split('|')
      $introw = $ActiveWorksheet.UsedRange.Rows.Count + 1  
      $ActiveWorksheet.cells.item($introw, 1) = $user_id  
      $ActiveWorksheet.cells.item($introw, 2) = $user_name
      $ActiveWorksheet.cells.item($introw, 3) = $Costcntr
      $ActiveWorksheet.cells.item($introw, 4) = $ApprMgr 
      $ActiveWorksheet.UsedRange.EntireColumn.AutoFit();
      }

I repeated the above for $InactiveUsers.

Was it helpful?

Solution

If both arrays have the same number of records you could do something like this:

for ($i = 0, $i -lt $activeUsers.Length; $i++) {
  ($user_id,$user_name,$Costcntr,$ApprMgr) = $activeUsers[$i].split('|')
  $row = 2 * $i + 2
  $ActiveWorksheet.Cells.Item($i, 1) = $user_id  
  $ActiveWorksheet.Cells.Item($i, 2) = $user_name
  $ActiveWorksheet.Cells.Item($i, 3) = $Costcntr
  $ActiveWorksheet.Cells.Item($i, 4) = $ApprMgr
  ($user_id,$user_name,$Costcntr,$ApprMgr) = $inactiveUsers[$i].split('|')
  $ActiveWorksheet.Cells.Item($i+1, 1) = $user_id  
  $ActiveWorksheet.Cells.Item($i+1, 2) = $user_name
  $ActiveWorksheet.Cells.Item($i+1, 3) = $Costcntr
  $ActiveWorksheet.Cells.Item($i+1, 4) = $ApprMgr
  $ActiveWorksheet.Rows.Item($i+1).EntireRow.Interior.ColorIndex = 3
}

If the length of both arrays differs, you could do something like this:

if ($activeUsers.Length -gt $inactiveUsers.Length) {
  $larger  = $activeUsers
  $smaller = $inactiveUsers
} else {
  $larger  = $inactiveUsers
  $smaller = $activeUsers
}
for ($i = 0, $i -lt $smaller.Length; $i++) {
  ($user_id,$user_name,$Costcntr,$ApprMgr) = $smaller[$i].split('|')
  ...
  ($user_id,$user_name,$Costcntr,$ApprMgr) = $larger[$i].split('|')
  ...
}
for ($i = $smaller.Length, $i -lt $larger.Length; $i++) {
  ($user_id,$user_name,$Costcntr,$ApprMgr) = $larger[$i].split('|')
  ...
}

A better solution, however, might be to introduce an additional column, indicating the state of the account:

$ActiveWorksheet.Cells.Item(1,5) = "Inactive"
...
for ($i = 0, $i -lt $activeUsers.Length; $i++) {
  ($user_id,$user_name,$Costcntr,$ApprMgr) = $activeUsers[$i].split('|')
  $row = 2 * $i + 2
  $ActiveWorksheet.Cells.Item($i, 1) = $user_id  
  $ActiveWorksheet.Cells.Item($i, 2) = $user_name
  $ActiveWorksheet.Cells.Item($i, 3) = $Costcntr
  $ActiveWorksheet.Cells.Item($i, 4) = $ApprMgr
  ($user_id,$user_name,$Costcntr,$ApprMgr) = $inactiveUsers[$i].split('|')
  $ActiveWorksheet.Cells.Item($i+1, 1) = $user_id  
  $ActiveWorksheet.Cells.Item($i+1, 2) = $user_name
  $ActiveWorksheet.Cells.Item($i+1, 3) = $Costcntr
  $ActiveWorksheet.Cells.Item($i+1, 4) = $ApprMgr
  $ActiveWorksheet.Cells.Item($i+1, 5) = "x"
}

Then you could use a conditional format to highlight rows where the value of the 5th column is "x" (the formula for that format would be =(INDIRECT("E"&ROW()))="x").

If you're going to introduce the abovementioned additional column, you could even simplify the script like this:

$users =  $activeUsers | % { $_ + "|" }
$users += $inactiveUsers | % { $_ + "|x" }
...
$ActiveWorksheet.Cells.Item(1,5) = "Inactive"
...
for ($i = 0, $i -lt $users.Length; $i++) {
  ($user_id,$user_name,$Costcntr,$ApprMgr,$inactive) = $users[$i].split('|')
  $ActiveWorksheet.Cells.Item($i+2, 1) = $user_id  
  $ActiveWorksheet.Cells.Item($i+2, 2) = $user_name
  $ActiveWorksheet.Cells.Item($i+2, 3) = $Costcntr
  $ActiveWorksheet.Cells.Item($i+2, 4) = $ApprMgr
  $ActiveWorksheet.Cells.Item($i+2, 5) = $inactive
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top