Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

powershell - Adding array value in a cmdlet set-transportrule

I need to make a script to add a recipient in the "-From" section of a Exchange Online transport rule.

The script need to store in a variable the actuals recipients in the rule and add one more define by the user.

Here where i am:

#Get the rule and store in $rule variable
$rule = Get-TransportRule  -Identity xxxxx-xxxxx-xxxx-xxxx-xxxxx

#Define the new recipient to add to the rule
$newuser = "[email protected]"

Thi is my problem:

I can get the actual recipient in the value $rule with $rule.from . The result is an array because it already have some recipients.

Command set-transportrule accept the variable $rule.from in the -from option but only take the first recipient in the array and not the others one

Set-TransportRule -Identity xxxxx-xxxxx-xxxx-xxxx-xxxxx -From $rule.from,$newuser

But it work if i use the variable with the index like this:

#Set the rule by re-adding the actual recipient and add $newuser
Set-TransportRule -Identity xxxxx-xxxxx-xxxx-xxxx-xxxxxc -From $rule.from[0],$rule.from[1],$newuser

I know I can find the number of recipients with $rule.from.count but after how can I generate the cmdlet set-transportrule automatically with all the index present in the variable $rule.from ?

EX if i have 8 recipients in the array the command should be:

Set-TransportRule -Identity xxxxx-xxxxx-xxxx-xxxx-xxxxxc -From $rule.from[0],$rule.from[1],$rule.from[2],$rule.from[3],$rule.from[4],$rule.from[5],$rule.from[6],$rule.from[7],$newuser

Or maybe a better way is possible?

Thank you!

question from:https://stackoverflow.com/questions/65866620/adding-array-value-in-a-cmdlet-set-transportrule

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

as you wrote in the comments my "blind guess" worked - so this is the answer:

store the current assigned addresses to a variable $arr

$arr=$rule.from 

add the $newuser to the variable

$arr += $newuser

and assign $arr to the -From parameter

Set-TransportRule -Identity xxxxx-xxxxx-xxxx-xxxx-xxxxx -From $arr

to sum it up:

#Get the rule and store in $rule variable
$rule = Get-TransportRule  -Identity xxxxx-xxxxx-xxxx-xxxx-xxxxx

#Define the new recipient to add to the rule
$newuser = "[email protected]"

#store current addresses in $arr variable
$arr=$rule.from

#append $newuser to $arr variable
$arr += $newuser

#update transport rule with $arr variable
Set-TransportRule -Identity xxxxx-xxxxx-xxxx-xxxx-xxxxx -From $arr

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...