Powershell script for finding and disabling Inactive Users
hi,
we have requirement need generate list of inactive users , disable those. have tried in 2 different methods not able achieve both together( reporting , disabling)
script 1: finding , disabling users not giving output of id's have been disabled. have tried adding export-csv @ end of command coming blank. if run script.ps1 >output.csv coming blank
============================
$90days = (get-date).adddays(-90)
get-content "d:\inactiveusers\oupath.txt" |
foreach-object{
$oupath=$_
get-aduser -searchbase "ou=$oupath,dc=domain,dc=com" -filter {lastlogondate -le $90days -and passwordlastset -le $90days} -properties lastlogondate, passwordlastset | disable-adaccount
}
==================================
script 2: generating report in excel not disabling id. have tried adding different commands disable-adaccount, disable-qaduser @ end not working. have tried adding separate command disable ad account @ end of script still no help
====================================
add-pssnapin quest.activeroles.admanagement
set-qadpssnapinsettings -defaultsizelimit 0
$reportmonth=get-date -format "mmm-yy"
$comparedate=get-date
$numberdays=90
get-content "d:\inactiveusers\oupath.txt" |
foreach-object{
$oupath=$_
$excel = new-object -com excel.application
$excel.visible = $true
$wbook = $excel.workbooks.add()
$wsheet = $excel.worksheets.item(1)
$wsheet.cells.item(1,3) = "name:"
$wsheet.cells.item(1,4) = "userid:"
$wsheet.cells.item(1,5) = "lastlogontimestamp:"
$workbook = $wsheet.usedrange
$workbook.interior.colorindex = 9
$workbook.font.colorindex = 2
$workbook.font.bold = $true
$introw=2
$disusers=get-qaduser -includedproperties displayname,samaccountname,lastlogontimestamp -searchroot "ou=$oupath,dc=domain,dc=com" -enabled | { ($comparedate-$_.lastlogontimestamp).days -gt $numberdays } | select displayname,samaccountname,lastlogontimestamp
foreach ($disabled in $disusers)
{
$wsheet.cells.item($introw,3) = $disabled.displayname
$wsheet.cells.item($introw,4) = $disabled.samaccountname
$wsheet.cells.item($introw,5) = $disabled.lastlogontimestamp
$introw++
}
$workbook.entirecolumn.autofit()
$excel.displayalerts=$false
$check = test-path -pathtype container d:\divisional_inactive_users\$reportmonth
if($check -eq $false){new-item d:\divisional_inactive_users\$reportmonth -type directory}
$wsheet.saveas("d:\divisional_inactive_users\$reportmonth\$oupath-inactiveusers.xlsx")
$excel.quit()
#remove-variable * -force -erroraction silentlycontinue
}
=======================================
hi,
so there solution below. i've ommited multi ous or multi domain aspects, works on single ou. i've tested code - it's running without errors.
let me know if wanted.
don't forget test insert test ou.
function disable-inactiveuser { param ( $interactiveuser ) disable-qaduser $interactiveuser.dn } add-pssnapin "quest.activeroles.admanagement" set-qadpssnapinsettings -defaultsizelimit 0 $test_ou = "[your test ou]" $reportmonth = get-date -format "mmm-yy" $comparedate = get-date $numberdays = 90 $excel = new-object -com excel.application $excel.visible = $true $wbook = $excel.workbooks.add() $wsheet = $excel.worksheets.item(1) $wsheet.cells.item(1, 3) = "name:" $wsheet.cells.item(1, 4) = "userid:" $wsheet.cells.item(1, 5) = "lastlogontimestamp:" $wsheet.cells.item(1, 6) = "status:" $workbook = $wsheet.usedrange $workbook.interior.colorindex = 9 $workbook.font.colorindex = 2 $workbook.font.bold = $true $introw = 2 $disusers = get-qaduser -includedproperties displayname, samaccountname, lastlogontimestamp -searchroot "ou=$test_ou,dc=simcat-tech,dc=com" -enabled foreach ($disabled in $disusers) { if ($disabled.lastlogontimestamp -ne $null) { $diffdays = ($comparedate - $disabled.lastlogontimestamp).days if ($diffdays -gt $numberdays) { $wsheet.cells.item($introw, 3) = $disabled.displayname $wsheet.cells.item($introw, 4) = $disabled.samaccountname $wsheet.cells.item($introw, 5) = $disabled.lastlogontimestamp $wsheet.cells.item($introw, 6) = "inactive" $introw++ } disable-inactiveuser $disabled } else { $wsheet.cells.item($introw, 3) = $disabled.displayname $wsheet.cells.item($introw, 4) = $disabled.samaccountname $wsheet.cells.item($introw, 5) = $disabled.lastlogontimestamp $wsheet.cells.item($introw, 6) = "never logged on" $introw++ } } $workbook.entirecolumn.autofit() $excel.displayalerts = $false $check = test-path -pathtype container d:\inactive_users\$reportmonth if ($check -eq $false) { new-item d:\inactive_users\$reportmonth -type directory } $wsheet.saveas("d:\inactive_users\$reportmonth\inactiveusers.xlsx") $excel.quit()
Windows Server > Windows PowerShell
Comments
Post a Comment