简体   繁体   中英

User's Manager Validation

I am trying to validate if a user has a manager in AD, if it is null/empty, I am supplying a value that it needs to be changed to. I currently have working code if the value isn't null and is different, but am receiving an error if it is null.

I have tried multiple variations on the lookup, but always come back to an error when value is null.

Import-Module ActiveDirectory
if ((Get-PSSnapin -Name Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin Quest.ActiveRoles.ADManagement
}
Connect-QADService foo.domain.com

#Specify User, then manager value to check against
Write-Host "What is the user's name?"
$User = Read-Host
Write-Host "Who is the manager we are checking against?"
$manager = Read-Host

#Check if Manager Value is null
if (-not ($ADemanager = (Get-ADUser (Get-ADUser $OBUID -Properties manager).manager).SamAccountName)) {
    Write-Host "User's Manager value is EMPTY or NULL in AD."
    $ADemanager = "Null Value"
    Write-Host "  NOTE: Changing the value from $ADemanager to $OBemanager per HR Records."
    Get-ADUser $OBUID |
        Set-ADUser -Manager $OBemanager -PassThru |
        Get-ADUser -Properties Manager |
        Select Name, Manager
} else {
    Write-Host "User's Manager Name not EMPTY in AD."
    Write-Host "Checking to see if manager's match in AD."
    if ($ADemanager -eq $OBemanager) {
        #Manager from AD matches manager listed on offboarding form
        Write-Host "The manager matches AD Value."
    } else {
        Write-Host "  NOTE: Manager from Offboarding form does not match manager listed in AD." -ForegroundColor Yellow
        Write-Host "  NOTE: Changing the value from $ADemanager to $OBemanager per HR Records."

        #Change Manager in AD (Make sure right variables used below)
        Get-ADUser $OBUID |
            Set-ADUser -Manager $OBemanager -PassThru |
            Get-ADUser -Properties Manager |
            Select Name, Manager
    }
}

I believe the code snippet below should do what you're asking: determine the current manager of a user, and if it's different from the desired manager (either because a different manager or no manager at all was assigned) update the user object with the new manager.

$ADuser     = Get-ADUser $OBUID -Properties Manager
$ADemanager = $ADuser |
              Where-Object { $_.Manager } |
              Select-Object -Expand Manager |
              Get-ADUser |
              Select-Object -Expand SamAccountName

if ($ADemanager -ne $OBemanager) {
    $ADuser |
        Set-ADUser -Manager $OBemanager -PassThru |
        Get-ADUser -Properties Manager |
        Select Name, Manager
}

I think it's easier to grab the correct manager's account and compare that with the user's manager - the following will work.

To write out an incorrect manager's name from the user's properties, I've just got a little -replace to shorten the distiguished name to the account CN. It helps if that matches your samAccountName . If they don't and you really want the samAccountName , you'll need another Get-ADUser for that purpose, but it seems a bit inefficient.

$ADuser     = Get-ADUser $OBUID -Properties Manager
# look up the manager account from user input
$OBemanager = Get-ADuser $mgrID

if ($ADuser.manager -eq $OBemanager) { #this will match the manager DN
    Write-Host "The manager matches AD Value."
}
else {
    #shorten the manager account DN from the user manager attribute
    if ($aduser.manager) {
        $mgrname = $ADuser.manager -replace '^CN=|,OU.*$'
    }
    else {
        $mgrname -eq 'null value'
    }
    Write-Host "NOTE: Changing the value from $mgrname to $($OBemanager.samAccountName) per HR Records."
    Set-ADUser -Manager $OBemanager -PassThru |
    Get-ADUser -Properties Manager | Select Name,Manager    
}

So I reworked the issue another way.

Most of the variables are loaded from a form, along with the

  $OBmanager = $cmbOBManager.Text.Split(",")
  $OBmanagername = $OBmanager[0]
  $OBemanager = $OBmanager[1].Trim()
  $OBestring = "@iconectiv.com"
  $OBEmpManager = $OBemanager + $OBestring


  #-check if manager from offboarding form is same listed in AD
  #Check User's manager name
  $userEntry = get-qaduser $OBUID
  [string]$man = $userEntry.manager
  #Check if manager name is null
  if (-not ($man -eq $null)) 
  {
    [array]$manName = $man.split('=,')
    $manager = $manName[1]
    #If null, change to HR specified value
    If($manager -eq $null)
    {
        Write-Host "The user's manager is Null in AD."
        Write-Host "Changing the user's manager to the HR supplied value."
        Get-aduser $OBUID | Set-ADUser -Manager $OBemanager -PassThru | get-aduser -Properties Manager | Select Name,Manager

        #Send Email
        ManagerChange
    }
    else
    {
        Write-Host "$manager is the manager of $OBUID";
        $ADemanager = (get-aduser (get-aduser $OBUID -Properties manager).manager).samaccountName
        #Check if AD value is the same as HR value
        If($ADemanager -eq $OBemanager)
        {
            Write-Host "The HR manager name supplied value matches the AD Value."
        }
        else
        {
            Write-Host "The HR manager name supplied value doesn't match the AD Value."
            Write-Host "Changing the manager value in AD to the HR supplied value."
            Get-aduser $OBUID | Set-ADUser -Manager $OBemanager -PassThru | get-aduser -Properties Manager | Select Name,Manager

            #Sending email
           ManagerChange

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM