Forum Discussion

TJCooper440's avatar
TJCooper440
Copper Contributor
Jan 28, 2025

I cant get this syntax correct

I have accounts with the prefix AB that correspond to normal accounts. For example, AB-Test and Test. I want to query an OU for all the prefixed accounts, strip the AB-, and then lookup the resultant string as an account name and check for accounts that are disabled.  I think I am close.

 

I dont think I have line 5 correct. I get "A positional Parameter cannot be found that accepts argument. Any help is appreciated.

$PrivAccounts= get-ADUser -Filter * -SearchBase "OU=ABAccounts,OU=DOT,DC=test,DC=LOC"

foreach ($PrivAccount in $PrivAccounts) {

$userobj = (Get-ADUser $(($PrivAccount.SamAccountName).Substring(3)) -Filter {Enabled -eq $false})

 

1 Reply

  • Hi TJCooper 

    Here you go:

     

    $PrivAccounts = Get-ADUser -Filter * -SearchBase "OU=ABAccounts,OU=DOT,DC=test,DC=LOC"
    
    foreach ($PrivAccount in $PrivAccounts) 
    {
        $SamAccountName = $PrivAccount.SamAccountName
        Write-Host "Working on: $SamAccountName"
        #Remove "AB-" Prefix
        If ($SamAccountName -like "AB-*")
        {
            $NormalAccount = $SamAccountName.Substring(3)
            $ADUser = Get-ADUser -Identity $NormalAccount
            
            If ($ADUser.Enabled -eq $True)
            {
                Write-Host "$(ADUser.SamAccountName) is Enabled"
            } else {
                Write-Host "$(ADUser.SamAccountName) is Disabled"
            }
        }
    }


    Kind Regards
    Andres

Resources