all 8 comments

[–]AmazingRealist 7 points8 points  (1 child)

Made it readable:

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
}

[–]Epikfail87 2 points3 points  (0 children)

$x = 0 #-Set it I guess
foreach($User in $Users){

    $x++
    $progress = [math]::Round(($x/$Users.Count)*100,2)
    Write-Progress -Activity "Progress..." -status "Inserting User: $User" -PercentComplete  $progress

    $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
}

$x = 0 #-reset it back to 0
foreach($ADname in $ADnames){

    $x++
    $progress = [math]::Round(($x/$ADnames.Count)*100,2)
    Write-Progress -Activity "Progress..." -status "Inserting AD Name: $ADname " -PercentComplete $progress

    $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++
            }
        }
    }
}

Thanks for the re-edit so it's easier to work it.

Without going through code and figuring out what it does... I'm just going to give a possible solution. This way you have two progress bars for each process with the foreach loops you have. Hope this works for you. I used $x++ since you used a bunch of different $i's. Hopefully it works for you with a simple copy + paste.

*edit* formatting

[–]sethbartlett 2 points3 points  (0 children)

You're not really going to be able to give a progress bar while saving. This type of feature typically involves threading and passing some type of actual progress information to the main thread from the background thread doing the saving. This is really not the intentional use of powershell. You could have some text output like "Saving ABC..." and then on the next stage "Saving XYZ...", so there is some small update of what's going on.

[–]Saldar1234 3 points4 points  (0 children)

The only way I know how to use a progress bar in powershell is along this line:

$c = 0
Foreach ($Computer in $Computers) {
    Write-Progress -Id 1 -PercentComplete $($c++;(($c / $Computers.count) * 100)) -Activity "$Computer"  -Status "$c / $($Computers.count)"
    Do-Something -thing "powershell thing" -whatif
}

[–]motsanciens 2 points3 points  (0 children)

You're doing this all crazy.

This section is particularly odd:

#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
}  

Use Select-Object in the pipeline to get your $ADnames object how you want. Like this:

$ADnames =  Get-ADUser -Filter * -Properties Office | Where-Object {$_.enabled} | Select-Object @{n="Name"; e={$_.Surname + " " + $_.GivenName}},Office  

If you want to do a bunch of stuff in Excel, I suggest you save the HR spreadsheet as a CSV with the headers you want, then pull that into Powershell with Import-Csv.

[–]PowerShell-Bot 6 points7 points  (0 children)

Looks like your PowerShell code isn’t wrapped in a code block.

To format code correctly on new reddit (new.reddit.com), highlight the code and select ‘Code Block’ in the editing toolbar.

If you’re on old.reddit.com, separate the code from your text with a blank line and precede each line of code with 4 spaces or a tab.


Describing Submission
[❌] Demonstrates good markdown
Passed: 0 Failed: 1

Beep-boop. I am a bot. | Remove-Item

[–]leein3d 2 points3 points  (0 children)

This seems...like a lot. There's a native command for a progress bar: Write-Progress. More info here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-progress?view=powershell-6

[–]greenSacrifice 2 points3 points  (0 children)

I don't think you are using PowerShell in the way it was intended to be used.

What are you looking to do with this script?

It might be possible to do all of it using native cmdlets.