Question
script disable Computer d'une OU en particulier
- ML
- Auteur du sujet
- Hors Ligne
- Nouveau membre
-
Réduire
Plus d'informations
- Messages : 1
- Remerciements reçus 0
il y a 10 ans 8 mois #20528
par ML
script disable Computer d'une OU en particulier a été créé par ML
Bonjour,
Je cherche à désactiver des postes d'un fichier csv ou txt d'une OU spécifique
Ex: j'ai le computer toto dans mon csv , il est présent dans l'OU de migration également du coup je désactive...
J'ai mon script qui fonctionne ci dessous (une partie je n'ai pas tout mit) mais il prend tout le domaine.. je cherche à me limiter à une seule OU. La commande ci dessous n'a pas l'air de fonctionner (targetOU1 comprend le chemin de l'ou)
$ADcomputerObjects= get-adcomputer $_ -filter {DistinguishedName -like $TargetOU1 }
ne fonctionne pas...
Si je peux avoir une explication de comment je peux faire pour ne cibler qu'une seule OU spécifique
[code:1]
param([Parameter(Mandatory=$true)][String]$InputFile)
Clear-Host
$FontError = @{ ForeGroundColor = \"Red\" ; BackGroundColor = \"Black\" }
$FontInfo = @{ ForeGroundColor = \"Yellow\" ; BackGroundColor = \"Black\" }
$FontOK = @{ ForeGroundColor = \"Green\" ; BackGroundColor = \"Black\" }
if (!(Get-Module ActiveDirectory)) { Import-Module ActiveDirectory }
$yes = New-Object System.Management.Automation.Host.ChoiceDescription \"&Yes\",\"\"
$no = New-Object System.Management.Automation.Host.ChoiceDescription \"&No\",\"\"
$choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no)
Write-Host \"ATTENTION : L'ensemble des comptes ordinateurs spécifiés vont être désactivés\" @FontInfo
Write-Host \"Veuillez confirmer l'exécution\" @FontInfo
if($host.UI.PromptForChoice(\"\",\"\",$choices,1)) {
Exit
}
Write-Host \"\"
$LogName = \"Transcript-AD-DisableComputerObjects.txt\"
Write-Host \"[EXEC] Chargement de la liste des ordinateurs à désactiver...\"
try {
$ADComputerObjects = Get-Content $InputFile disable.csv -ErrorAction SilentlyContinue
$ADComputerObjects = @($($ADComputerObjects | Where-Object { $_ } | Sort-Object -Unique))
# if($ADComputerObjects -eq $null) { throw \"Le fichier $InputFile est vide\" }
Write-Host \"`t[OK] Liste des ordinateurs à désactiver chargée\" @FontOK
}
catch {
Write-Host \"`t[ERROR] Impossible de charger la liste des ordinateurs à désactiver (le fichier est vide, n'existe pas...)\" @FontError
[String]::Join(\"`r`n\",(Get-Content $LogName)) | Out-File $LogName
\"impossible pour ce poste :\" + $InputFile | Out-File $LogName -Append -NoClobber
Exit
}
Write-Host \"`n[INFO] $($ADComputerObjects.Length) comptes ordinateurs vont être désactivés`n\" @FontInfo
Write-Host \"[EXEC] Désactivation des comptes ordinateurs...\"
##prendre que les computers dans ou migration
$TargetOU1 = \"ou=Migration,ou=Ordinateurs,ou=TOTO,dc=testlab,dc=com\"
$ADcomputerObjects= get-adcomputer $_ -filter {DistinguishedName -like $TargetOU1 }
Write-Host $ADComputerObjects
$ADComputerObjects | ForEach-Object {
Write-Host -NoNewline \"`t[STATUS] Computer $($_) : \"
try {
$ComputerObject = Get-ADComputer $_
if (!$ComputerObject.enabled) {
Write-Host \"ALREADY DISABLED\" @FontInfo
\"ALREADY DISABLED : \" + $ComputerObject | Out-File $LogName -Append -NoClobber
}
else {
Disable-ADAccount $ComputerObject
Write-Host \"DISABLE\" @FontOK
\"DISABLE : \" + $ComputerObject | Out-File $LogName -Append -NoClobber
}
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {
Write-Host \"NOT EXIST\" @FontError
\"NOT EXIST : \" + $ComputerObject | Out-File $LogName -Append -NoClobber
}
catch {
Write-Host \"NOT DELETE\" @FontError
\"NOT DELETE : \" + $ComputerObject | Out-File $LogName -Append -NoClobber
}
}
# Force le retour à la ligne du transcript
[String]::Join(\"`r`n\",(Get-Content $LogName)) | Out-File $LogName
[/code:1]
en vous remerciant grandement par avance, je suis débutante... sorry
Je cherche à désactiver des postes d'un fichier csv ou txt d'une OU spécifique
Ex: j'ai le computer toto dans mon csv , il est présent dans l'OU de migration également du coup je désactive...
J'ai mon script qui fonctionne ci dessous (une partie je n'ai pas tout mit) mais il prend tout le domaine.. je cherche à me limiter à une seule OU. La commande ci dessous n'a pas l'air de fonctionner (targetOU1 comprend le chemin de l'ou)
$ADcomputerObjects= get-adcomputer $_ -filter {DistinguishedName -like $TargetOU1 }
ne fonctionne pas...
Si je peux avoir une explication de comment je peux faire pour ne cibler qu'une seule OU spécifique
[code:1]
param([Parameter(Mandatory=$true)][String]$InputFile)
Clear-Host
$FontError = @{ ForeGroundColor = \"Red\" ; BackGroundColor = \"Black\" }
$FontInfo = @{ ForeGroundColor = \"Yellow\" ; BackGroundColor = \"Black\" }
$FontOK = @{ ForeGroundColor = \"Green\" ; BackGroundColor = \"Black\" }
if (!(Get-Module ActiveDirectory)) { Import-Module ActiveDirectory }
$yes = New-Object System.Management.Automation.Host.ChoiceDescription \"&Yes\",\"\"
$no = New-Object System.Management.Automation.Host.ChoiceDescription \"&No\",\"\"
$choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no)
Write-Host \"ATTENTION : L'ensemble des comptes ordinateurs spécifiés vont être désactivés\" @FontInfo
Write-Host \"Veuillez confirmer l'exécution\" @FontInfo
if($host.UI.PromptForChoice(\"\",\"\",$choices,1)) {
Exit
}
Write-Host \"\"
$LogName = \"Transcript-AD-DisableComputerObjects.txt\"
Write-Host \"[EXEC] Chargement de la liste des ordinateurs à désactiver...\"
try {
$ADComputerObjects = Get-Content $InputFile disable.csv -ErrorAction SilentlyContinue
$ADComputerObjects = @($($ADComputerObjects | Where-Object { $_ } | Sort-Object -Unique))
# if($ADComputerObjects -eq $null) { throw \"Le fichier $InputFile est vide\" }
Write-Host \"`t[OK] Liste des ordinateurs à désactiver chargée\" @FontOK
}
catch {
Write-Host \"`t[ERROR] Impossible de charger la liste des ordinateurs à désactiver (le fichier est vide, n'existe pas...)\" @FontError
[String]::Join(\"`r`n\",(Get-Content $LogName)) | Out-File $LogName
\"impossible pour ce poste :\" + $InputFile | Out-File $LogName -Append -NoClobber
Exit
}
Write-Host \"`n[INFO] $($ADComputerObjects.Length) comptes ordinateurs vont être désactivés`n\" @FontInfo
Write-Host \"[EXEC] Désactivation des comptes ordinateurs...\"
##prendre que les computers dans ou migration
$TargetOU1 = \"ou=Migration,ou=Ordinateurs,ou=TOTO,dc=testlab,dc=com\"
$ADcomputerObjects= get-adcomputer $_ -filter {DistinguishedName -like $TargetOU1 }
Write-Host $ADComputerObjects
$ADComputerObjects | ForEach-Object {
Write-Host -NoNewline \"`t[STATUS] Computer $($_) : \"
try {
$ComputerObject = Get-ADComputer $_
if (!$ComputerObject.enabled) {
Write-Host \"ALREADY DISABLED\" @FontInfo
\"ALREADY DISABLED : \" + $ComputerObject | Out-File $LogName -Append -NoClobber
}
else {
Disable-ADAccount $ComputerObject
Write-Host \"DISABLE\" @FontOK
\"DISABLE : \" + $ComputerObject | Out-File $LogName -Append -NoClobber
}
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {
Write-Host \"NOT EXIST\" @FontError
\"NOT EXIST : \" + $ComputerObject | Out-File $LogName -Append -NoClobber
}
catch {
Write-Host \"NOT DELETE\" @FontError
\"NOT DELETE : \" + $ComputerObject | Out-File $LogName -Append -NoClobber
}
}
# Force le retour à la ligne du transcript
[String]::Join(\"`r`n\",(Get-Content $LogName)) | Out-File $LogName
[/code:1]
en vous remerciant grandement par avance, je suis débutante... sorry
Connexion ou Créer un compte pour participer à la conversation.
- Philippe
- Hors Ligne
- Modérateur
-
Réduire
Plus d'informations
- Messages : 1778
- Remerciements reçus 21
il y a 10 ans 8 mois #20529
par Philippe
Réponse de Philippe sur le sujet Re:script disable Computer d'une OU en particulier
bonjour tnia
tnia écrit:
tu trouvera l'explication ici dans l'exemple 4
tnia écrit:
Si je peux avoir une explication de comment je peux faire pour ne cibler qu'une seule OU spécifique
tu trouvera l'explication ici dans l'exemple 4
en rien et bienvenueen vous remerciant grandement par avance, je suis débutante...
Connexion ou Créer un compte pour participer à la conversation.
Temps de génération de la page : 0.048 secondes
- Vous êtes ici :
-
Accueil
-
forum
-
PowerShell
-
Entraide pour les débutants
- script disable Computer d'une OU en particulier