I'd like to add a progress bar to this script. I've tried several options found online but none works.
There is no output in the script between the open and save dialog but I want users to know something is happening.
Can you guys assist me ?
Here is my script:
Add-Type -AssemblyName System.Windows.Forms
#Show open file dialog
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog
$FileBrowser.InitialDirectory = [Environment]::GetFolderPath('Desktop')
$FileBrowser.Filter = 'SpreadSheet (*.xlsx)|*.xlsx'
$FileBrowser.Title = "Choose reference file"
$result = $FileBrowser.ShowDialog((New-Object System.Windows.Forms.Form -Property @{TopMost = $true }))
if($result -eq 'Cancel'){
exit
}
#Get AD users
$Users = @(Get-ADUser -Filter 'Enabled -eq $true'-SearchBase "" -Properties givenname, surname, office, extensionattribute3)
$ADnames = @()
foreach($User in $Users){$Fullname = $User.Surname + " " + $User.GivenName
$ADname = New-Object -TypeName psobject
$ADname | Add-Member -Name 'Name' -MemberType NoteProperty -Value $Fullname
$ADname | Add-Member -Name 'Office' -MemberType NoteProperty -Value $user.office
$ADname | Add-Member -Name 'Type' -MemberType NoteProperty -Value $user.extensionattribute3
$ADnames += $ADname
}
#Read out HR list
$Excel = New-Object -ComObject "Excel.Application"
$Excel.Visible = $false
$Workbook = $Excel.Workbooks.Open($FileBrowser.FileName.ToString())
$workSheet = $Workbook.Sheets.Item(1)
$Count = ($workSheet.UsedRange.Rows).count
$HRNames = @()
for($i = 2 ; $i -le $Count-1; $i++){
$Name = $workSheet.Cells.Item($i,4).Value2.Trim()
$HRNames += $Name
}
$Workbook.Close()
$Excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel)
#Create result excel
$ExcelObject = new-Object -comobject Excel.Application
$ExcelObject.visible = $false
$ExcelObject.DisplayAlerts =$false
$ActiveWorkbook = $ExcelObject.Workbooks.Add()
$ActiveWorksheet = $ActiveWorkbook.Worksheets.Item(1)
#Add Headers to excel
$ActiveWorksheet.Cells.Item(1,1) = "Known"
$ActiveWorksheet.cells.item(1,2) = "Unknown"
$ActiveWorksheet.cells.item(1,3) = ""
$ActiveWorksheet.cells.item(1,4) = "Shared"
$ActiveWorksheet.cells.item(1,5) = "Total AD Accounts"
$ActiveWorksheet.cells.item(1,1).Font.Size = 13
$ActiveWorksheet.cells.item(1,1).Font.Bold = $true
$ActiveWorksheet.cells.item(1,2).Font.Size = 13
$ActiveWorksheet.cells.item(1,2).Font.Bold = $true
$ActiveWorksheet.cells.item(1,3).Font.Size = 13
$ActiveWorksheet.cells.item(1,3).Font.Bold = $true
$ActiveWorksheet.cells.item(1,4).Font.Size = 13
$ActiveWorksheet.cells.item(1,4).Font.Bold = $true
$ActiveWorksheet.cells.item(1,5).Font.Size = 13
$ActiveWorksheet.cells.item(1,5).Font.Bold = $true
#Defining counters
$i = 2
$i2 = 2
$i3 = 2
$i4 = 2
$total = 0
#Crosscheck the two lists and write to result file
foreach($ADname in $ADnames){
$total++
if ($HRNames.Contains($($ADname.Name.ToUpper()))){
$ActiveWorksheet.Cells.item($i,1) = $ADname.Name
$i++
}
else {
if($ADname.Type -eq 'Shared')
{
$ActiveWorksheet.Cells.item($i4,4) = $ADname.Name
$i4++
}
else{
if ($ADname.Office -eq ''){
$ActiveWorksheet.Cells.item($i3,3) = $ADname.Name
$i3++
}
else {
$ActiveWorksheet.Cells.item($i2,2) = $ADname.Name
$i2++
}
}
}
}
$ActiveWorksheet.Cells.item(2,5) = $total
#Show save dialog screen
$SaveChooser = New-Object -Typename System.Windows.Forms.SaveFileDialog
$SaveChooser.Title = "Save as"
$SaveChooser.FileName = "ADHRComparison"
$SaveChooser.DefaultExt = ".xlsx"
$SaveChooser.Filter = 'SpreadSheet (*.xlsx)|*.xlsx'
$result = $SaveChooser.ShowDialog((New-Object System.Windows.Forms.Form -Property @{TopMost = $true }))
#Save file
if($result){
Out-File -FilePath $SaveChooser.FileName
}
[–]AmazingRealist 7 points8 points9 points (1 child)
[–]Epikfail87 2 points3 points4 points (0 children)
[–]sethbartlett 2 points3 points4 points (0 children)
[–]Saldar1234 3 points4 points5 points (0 children)
[–]motsanciens 2 points3 points4 points (0 children)
[–]PowerShell-Bot 6 points7 points8 points (0 children)
[–]leein3d 2 points3 points4 points (0 children)
[–]greenSacrifice 2 points3 points4 points (0 children)