Last active
August 29, 2015 14:22
-
-
Save chipitsine/b748fd3893bd6f3eaa99 to your computer and use it in GitHub Desktop.
blank / changepassword
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#requires -Modules ActiveDirectory | |
ipmo ActiveDirectory | |
cls | |
$CurrentDir = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition) | |
$processedFile = (Join-Path $CurrentDir 'processed.txt') | |
$processedCSV = (Join-Path $CurrentDir 'processed.csv') | |
$d = (Get-date).AddDays(-30) | |
$c = Get-ADComputer -Filter { (Enabled -eq $true) -and (PasswordLastSet -ge $d) -and (OperatingSystem -notlike "*Server*")} | |
If(Test-Path -path $processedFile){ | |
$processed = Get-Content $processedFile | |
}Else{ | |
$processed = [array] @(); | |
} | |
$c | % { | |
[String] $computer = $_.Name | |
If($processed -notcontains $computer){ | |
'examining : {0}...' -f $computer | |
$de = [ADSI]('WinNT://{0}' -f $computer) | |
$users = $de.psbase.children | where{$_.psbase.schemaClassName -match 'user'} | select @{n='Name';e={$_.name}} | |
$users | % { | |
[String] $user = $_.Name | |
[String] $chg_result = '' | |
$is_disabled=([adsi]“WinNT://$computer/$user”).invokeget('AccountDisabled') | |
if($is_disabled -eq $false){ | |
try{ | |
([adsi]“WinNT://$computer/$user”).ChangePassword('','') | |
}catch{ | |
$chg_result=$_.ToString() | |
} | |
} | |
} | |
$userCsvObj = New-Object PSObject | |
$userCsvObj | Add-Member NoteProperty -Name "Computer" -Value $computer | |
$userCsvObj | Add-Member NoteProperty -Name "User" -Value $user | |
$userCsvObj | Add-Member NoteProperty -Name "Is_disabled" -Value $is_disabled | |
$userCsvObj | Add-Member NoteProperty -Name "Result" -Value $chg_result | |
$userCsvObj | Export-Csv -NoTypeInformation -Append -path $processedCSV -delimiter ';' -Encoding UTF8 | |
} | |
if($users.Count -gt 0){ Add-Content $processedFile $computer } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment