0

I would like to use a variable with variable name in 2 examples

Example 1:

$Myvalue1 = "$AutomateProcessing"

I want to put $false in my variable $AutomateProcessing using

$"$Myvalue1" = $false 

(in place of $AutomateProcessing = $false)

Example 2:

$Myvalue2 = "AutomateProcessing"

Get-EXOMailbox $MyMBX | set-CalendarProcessing -$Myvalue2 $MyConfig

(in place of Get-EXOMailbox $MyMBX | set-CalendarProcessing -AutomateProcessing $MyConfig)

With this, I could create a loop with a lot of parameters I want to modifiy.

Is it possible to do with PowerShell?

Thank you in advance

3
  • Can you rephrase? I am having a hard time understanding what you're after. Commented Jun 10, 2022 at 16:17
  • Re example 1: What you're looking for is variable indirection, where you refer to a variable indirectly, via its name stored in a different variable or provided by an expression. PowerShell enables this via the Get-Variable and Set-Variable cmdlets, but note that there are usually better alternatives. See this post for details. Commented Jun 11, 2022 at 1:39
  • Re example 2: Use splatting - see about_Splatting Commented Jun 11, 2022 at 1:40

4 Answers 4

1

You can use the cmdlet set-variable.

Use switch "Variable" to define your variable (without $ sign) and the switch "Value" for the value.

$AutomateProcessing=$true
$Myvalue1 = "AutomateProcessing"
Set-Variable -Name $Myvalue1 -Value $false
Out-Host -InputObject "Variable $Myvalue1 is now set to $AutomateProcessing"

The result:

Variable AutomateProcessing is now set to False
Sign up to request clarification or add additional context in comments.

3 Comments

Exactly what I searched. simple and efficient! Thank you very much
Happy to help. Don't forget to set an accepted answer.
The problem is that I can only set one accepted answer... You've the good one for the first example and Shabarinat has the good answer for the second example....
0

The first part is not clear.

On the second example you mentioned, You can achieve this if you store the entire commandlet as a string first and use Invoke-Expression to trigger it.

Something like this.

[string] $FullCommandlet = "Get-EXOMailbox $MyMBX | set-CalendarProcessing -$Myvalue2 $MyConfig"

Invoke-Expression -Command $FullCommandlet

Hope that helps !

3 Comments

Invoke-Expression (iex) should generally be avoided and used only as a last resort, due to its inherent security risks. Superior alternatives are usually available. If there truly is no alternative, only ever use it on input you either provided yourself or fully trust - see this answer.
Exactly what I searched. simple and efficient! Thank you very much
Also thank you for the remark about the security risk of such command
0

Try Splatting your parameters in from a hashtable

Param(
[array]$MyMBX = <array>,
[String]$AddOrganizerToSubject = <value>,
[bool]$AutomateProcessing = <value>,
[bool]$AllowRecurringMeetings = <value>
)
Foreach($mbx in $MyMBX){
$Myconfig = @{'Identity' = (Get-EXOMailbox -Identity $mbx)
             'AutomateProcessing' = $AutomateProcessing 
             'AddOrganizerToSubject' = $AddOrganizerToSubject
             'AllowRecurringMeetings' = $AllowRecurringMeetings
              }# etc etc
Set-CalendarProcessing @Myconfig
}

2 Comments

Splatting is a great tip, but your answer would be more helpful if it focused on how to apply it to the OP's second example.
Thank you for your help.... a little more complex to use with a lot of values but to be investigate. Here in my case, I try to compare each value to a default value and change only if needed. In your explanation, we change all the settings each time.
0

Simplified to just Automateprocessing

Param(
[array]$MyMBX = <array>,
[bool]$AutomateProcessing = $false,
)
Foreach($mbx in $MyMBX){
$Myconfig = @{'Identity' = (Get-EXOMailbox -Identity $mbx)
             'AutomateProcessing' = $AutomateProcessing 
              } 
Set-CalendarProcessing @Myconfig
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.