Script exclusão de usuários windows server

Jackson Florêncio 0 Reputation points
2023-01-31T11:13:20.18+00:00

Estou com um problema em um script que criei para fazer a exclusão dos usuários com mais de 30 dias sem logar, e fazer a exclusão de suas respectivas pastas no servidor, porém quando rodo o script não acontece nada, ele não dá nenhum erro, porém também não faz a exclusão do usuário no AD e nem suas pastas.

Estou iniciando o powershell como administrador.

Script:

Import-Module ActiveDirectory

$time = (Get-Date).AddDays(-30)

$users = Get-ADGroupMember "Suporte_Call" -recursive

$users = $users | Where-Object {($_.LastLogonDate -lt $time) -and ($_.SamAccountName -notin "veeam", "Default", "Publico", "Administrator")}

foreach($user in $users) {

    Remove-ADUser -Identity $user.SamAccountName -Confirm:$false

    $userFolder = "C:\Users\" + $user.SamAccountName

    Remove-Item $userFolder -Recurse -Force

    Write-Host "Usuário $($user.SamAccountName) excluído com sucesso."

}
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
13,213 questions
Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,632 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,583 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 34,421 Reputation points
    2023-01-31T13:54:56.1733333+00:00

    Verify that your $users variable contains AD accounts.

    $users = Get-ADGroupMember "Suporte_Call" -recursive
    "We found {0} users." -f $users.count
    $users = $users | Where-Object {($_.LastLogonDate -lt $time)}
    "{0} users match the logon criteria." -f $users.count
    $users = $users | Where-Object {($_.SamAccountName -notin "veeam", "Default", "Publico", "Administrator")}
    "We will process {0} users." -f $users.count
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.