Home > database >  Powershell -Build a dynamic variable of not null strings, then check if that dynamic variable and as
Powershell -Build a dynamic variable of not null strings, then check if that dynamic variable and as

Time:01-18

I'm trying to dynamically build a variable that will contain strings. Then I want to check that new variable and if it's not empty, prepend ";" to each added string.

Basically, I'm building a Set-ADUser -Replace @{dynamicVarialble} command and want that dynamicVariable to contain up to three different values but only those not empty or null. If it's the first value in the string don't prepend the ';' otherwise do add it. So outcome would be something like:

Set-ADUser -identity $person -Replace @{mobilenumber=$number;phoneNUmer=$phoneNumber;pager=$pagerNumber}

but do it so it's -Replace @{$dynamicVariable} -which below will be $ADUpdater variable or array in my code below.

Note: if one of those input variables is empty, don't include it in "dynamic variable" or array. And if it's the first variable added, do NOT prepend a semicolon.

Hope that makes sense...so if input was:

$otherMobile = "555-222-1111"
$otherHomePhone ="" ##(empty or null)
$otherPager = "555-555-5555"

then it would be:

Set-ADUser -identity $person -Replace @{otherMobile="555-222-111";otherPager="555-555-5555"}

     $person =$userID
     $number= $otherMobile
     $homeNumber = $otherHomePhone
     $pagerNumber = $otherPager

     [String]$ADupdater = @()
     if (![string]::IsNullOrEmpty($otherMobile)){
          [string]$ADupdater  = "otherMobile=$number"
     }
     if (![string]::IsNullOrEmpty($otherHomePhone)){
          [string]$ADupdater  = "otherHomePhone=$homeNumber"    
     }
     if (![string]::IsNullOrEmpty($otherPager)){
          [string]$ADupdater  = "otherPager=$otherPager"    
     }
    
     Foreach ($item in $ADupdater){
         if ([string]::IsNullOrEmpty($item))
         {
            $ADupdater  = $ADupdater 
         }
         else {
            $ADupdater  = ';'   $ADupdater
         }
     }

    Write-host "this is ADUpdated: $ADupdater"

CodePudding user response:

This approach won't work, because the -Replace parameter does not actually accept a string value.

Let's have a look at the -Replace parameter:

PS ~> $setADUserCommand = Get-Command Set-ADUser
PS ~> $setADUserCommand.Parameters['Replace']


Name            : Replace
ParameterType   : System.Collections.Hashtable
ParameterSets   : {[Identity, System.Management.Automation.ParameterSetMetadata]}
IsDynamic       : True
Aliases         : {}
Attributes      : {Identity, System.Management.Automation.ValidateNotNullOrEmptyAttribute, Microsoft.ActiveDirectory.Management.Commands.ValidateAttributeValueHashtableAttribute}
SwitchParameter : False

Notice how the ParameterType value is listed as HashTable - so we need to pass a hashtable as an argument to that parameter.

The good thing is that hashtables can be modified programmatically, so we can still construct the argument you want:

# Let's start by creating an empty hashtable
$ADupdater = @{}

$otherMobile = "555-222-1111"
$otherHomePhone ="" ##(empty or null)
$otherPager = "555-555-5555"

# now we just need to populate the hashtable instead of a string
if (![string]::IsNullOrEmpty($otherMobile)){
    $ADupdater["otherMobile"] = $otherMobile
}
if (![string]::IsNullOrEmpty($otherHomePhone)){
    $ADupdater["otherHomePhone"] = $otherHomePhone
}
if (![string]::IsNullOrEmpty($otherPager)){
    $ADupdater["otherPager"] = $otherPager
}

# Now we simply pass the hashtable to Set-ADUser
Set-ADUser -Identity $person -Replace $ADupdater

You might want to make life easier for yourself by also storing the candidate values and attribute names in a hashtable itself - it'll make the code much simpler to add new attributes to (since you don't need a separate if(![string]::...){...} block for each new attribute):

$ADupdater = @{}

$values = @{
  otherMobile = "555-222-1111"
  otherHomePhone ="" ##(empty or null)
  otherPager = "555-555-5555"
}

foreach($entry in $values){
  if(-not [string]::IsNullOrEmpty($entry.Value)){
    $ADupdater[$entry.Name] = $entry.Value
  }
}

CodePudding user response:

For those interested (and thanks @Mathias R. Jessen) What worked for me (although perhaps not clean or efficient)...

$ADupdater = @{}

$values = @{
  otherMobile = $otherMobile
  otherHomePhone = $otherHomePhone
  otherPager = $otherPager
}

if (! [string]::IsNullOrEmpty($otherMobile)) {
    $ADupdater.add('otherMobile',$otherMobile)
}
if (! [string]::IsNullOrEmpty($otherHomePhone)) {
    $ADupdater.add('otherHomePhone',$otherHomePhone)
}
if (! [string]::IsNullOrEmpty($otherPager)) {
    $ADupdater.add('otherPager',$otherPager)
}

Set-ADUser -Identity $($person) -Replace $ADupdater

Since I'm passing the values of otherMobile, otehrHomePhone and otherPager as parameters to my script I couldn't get Mathias's recommendation of below to work:

foreach($entry in $values){
  if(-not [string]::IsNullOrEmpty($entry.Value)){
    $ADupdater[$entry.Name] = $entry.Value
  }
}

Cheers!

  •  Tags:  
  • Related