Wednesday, March 31, 2021

Powershell Logon hours comparison - Updated

Not so long ago, I posted my functioning logon hours comparison script . . a few years and more powershell know-how has made my current self a-bit shocked at how my old script attempts look to my eyes now.

And so , I had a feeling to do a newer re-write: 

- same premise as the old script
- only edits required are the "username" values
- the CSV to XLSX conversion needs Excel installed on the machine running the script. 

**************************Begin Script***********************************

Function CSVtoXLSX
{
    [cmdletbinding()]
    Param
    (
        [Parameter(Mandatory=$true, Position=0)]
        [string]$arg1,
       
        [Parameter(Mandatory=$true, Position=1)]
        [string]$arg2
    )
    $XL = new-object -comobject Excel.application
    $XL.visible=$false
    $XL.displayalerts=$false
    $XL.workbooks.open("$arg1").SaveAs("$arg2",51)
    $XL.quit()
    #
    $XL2=new-object -comobject Excel.application
    $XL2.visible=$false
    $XL2.displayalerts=$false
    $WB=$XL2.workbooks.open("$arg2")
    $ws1=$wb.worksheets.item(1)
    $ws1.activate()
    #
    # Find the column count, to define our working-range.
    #
    $findrange=$ws1.usedrange.cells
    $colcount=$findrange.columns.count
    $workrange=$ws1.range($ws1.cells.item(1, 1), $ws1.cells.item(1, $colcount))
    #
    # Adjust the first row into a Header type Row
    #
    $workrange.font.bold=$true
    $workrange.interior.colorindex=15
    #
    # Set autofilter for all columns, and then autofit all columns.
    #
    $xl2.selection.autofilter() | out-null
    [void]$ws1.cells.entirecolumn.autofit()
    #
    # Save changes and close out.
    #
    $wb.saveas("$arg2")
    $wb.close()
    $XL2.quit()
}

import-module activedirectory

$report=@{}

$Default=get-aduser username -properties * # Adjust the username value 
$Defaultcheck=$default.logonhours
$Allhours=get-aduser username -properties * # Adjust the username value
$Allhourscheck=$allhours.logonhours

$users=get-aduser -filter 'enabled -eq $true' -properties *

foreach ($user in $users)
{
$Name=$user.name
$Manager=$user.manager
$Userhours=$user.logonhours
$Temp=[ordered]@{}
$Temp.Name=$Name
#
If ($userhours -eq $null)
{
$Temp.LogonHours="Not Set"
If ($manager -eq $null)
{
$Temp.Manager="Not Set"
$Report +=new-object -typename psobject -property $Temp
}
ELSE
{
$Manquery=get-aduser $Manager
$Manname=$manquery.name
$Temp.Manager=$Manname
$Report +=new-object -typename psobject -property $Temp
}
}
ELSE
{
$Compare1="$Userhours" -match "$Defaultcheck"
If ($Compare1 -eq $True)
{
$Temp.LogonHours="Default"
If ($manager -eq $null)
{
$Temp.Manager="Not Set"
$Report +=new-object -typename psobject -property $Temp
}
ELSE
{
$Manquery=get-aduser $Manager
$Manname=$manquery.name
$Temp.Manager=$Manname
$Report +=new-object -typename psobject -property $Temp
}
}
$Compare2="$Userhours" -match "$Allhourscheck"
If ($Compare2 -eq $True)
{
$Temp.Logonhours="All hours access"
If ($manager -eq $null)
{
$Temp.Manager="Not Set"
$Report +=new-object -typename psobject -property $Temp
}
ELSE
{
$Manquery=get-aduser $Manager
$Manname=$manquery.name
$Temp.Manager=$Manname
$Report +=new-object -typename psobject -property $Temp
}
}
}
$csv="C:\temp\logonhoursCompareReport.csv"
$xlsx="C:\temp\logonhoursCompareReport.xlsx"
$Report | export-csv "$csv" -notypeinformation
CSVtoXLSX $csv $xlsx
RI $CSV 
***********************end Script********************************************

No comments:

Post a Comment