2020年12月31日星期四

Powershell AD Syntax - If Statement is duplicating a string

New scripter with hopefully an easy proofread.

I made an Powershell Active Directory script that processes people on Leave of Absence (LOA) within my organization from a daily CSV file I get.

In summary my problem segment should:

  • Check the $Line variable's currentStatus if that employee is "On Leave"
  • Disable that Active Directory account.
  • Grab that AD account's 'Description' field and look for the string part "LOA - " and add that ONLY if it is missing. Problem is that if that field already has "LOA - " in the description, it puts another one... and another, so on.

Example:

  • Description (Good): LOA - Chef
  • Description (Bad): LOA - LOA - Chef
  • Description (Please NO): LOA - LOA - LOA - Chef
  • Etc.

I'm fairly certain the problem is with this line here but I can't figure out how to fix it.

If ($null -ne ($newDescript | ? {$loaPhrases -notcontains $_}))

$loaPhrases = "LOA -|LOA-|LOA - |LOA- |LOA - LOA - "    ElseIf ($Line.currentStatus -eq "ON LEAVE")   {      #Add 'LOA - ' to description and disable AD      Set-ADUser $User.samAccountName -Enabled 0      'Disabled AD'        $Description = Get-ADUser $User.samAccountName -Properties Description | Select-Object -ExpandProperty Description      $newDescript = $Description.substring(0, $Description.IndexOf('-')+1)      If ($null -ne ($newDescript | ? {$loaPhrases -notcontains $_}))      {          $Description = "LOA - " + "$Description"          $Description = $Description.trim()          Set-ADUser $user.samAccountName -Description $Description      }    }                                            
https://stackoverflow.com/questions/65526793/powershell-ad-syntax-if-statement-is-duplicating-a-string January 01, 2021 at 09:51AM

没有评论:

发表评论