1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#Importation du module AD pour powershell
Import-Module ActiveDirectory
#Get Date
$date = Get-Date
#Get Computers in a specific OU from AD
$systems = Get-ADComputer -SearchBase 'OU=ordinateurs,OU=001,OU=etablissements,dc=XXX,dc=XXXXX,dc=XX,dc=XX'-Server 'XXX.XXXXX.XX.XX' -filter * -Properties LastLogonDate,Name,Description,Created|
#Filter such as last login and 180 days
Where-Object {$_.LastLogonDate -lt $date.AddDays(-180)}|
#Where-Object {$_.Created -lt $date.AddDays(-180)}|
#Display in a grid View for selection
Select LastLogonDate,Name,Created,Description,DNSHostName,Enabled| out-gridview -PassThru -Title "Recherche des PC"
# If PC have not been selected write warning in host
if (!$systems)
{
Write-Warning "Aucun PC en liste."
}
Else
{
# Display PC names
Write-Warning "Choix en cours:"
$systems | ft -wrap -auto
# Confirm if you want to proceed
Write-Host -nonewline "Effectuer les modifications? (O/N): "
$Response = Read-Host
Write-Host " "
# If response was different that Y script will end
If ( $Response -ne "O" )
{
Write-Warning "Fin du Script."
}
Else
{
$results = @()
foreach($computer in $systems)
{
$results += $computer | select-object Name, OperatingSystem, DistinguishedName, LastLogonTimeStamp
$computer | disable-ADaccount
#$computer | Set-ADComputer -Description "Desactiver le $date. Peut-être supprimé après $expiryDate"
#$computer | move-ADobject -targetpath "OU=Anciens PC, DC=XXX, DC=XXXXX,DC=XX, DC=XX"
Write-Host "$computer"
Write-Warning "...est rendu dans le OU correspondant."
}
$dateForFilename = $date.ToShortDateString() | foreach {$_ -replace "/", ""}
$results | export-csv "C:\users\XXXXX\desktop\$dateForFilename - Inactive Computers Check.csv" -NoTypeInformation
}
} |
Partager