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

zett42
  • 25,437
  • 3
  • 35
  • 72
Thomas
  • 3
  • 1
  • Can you rephrase? I am having a hard time understanding what you're after. – Abraham Zinala Jun 10 '22 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`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/get-variable) and [`Set-Variable`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/set-variable) cmdlets, but note that there are usually better alternatives. See [this post](https://stackoverflow.com/q/68213804/45375) for details. – mklement0 Jun 11 '22 at 01:39
  • Re example 2: Use splatting - see [about_Splatting](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Splatting) – mklement0 Jun 11 '22 at 01:40

4 Answers4

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
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 !

Shabarinath
  • 132
  • 8
  • `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](https://stackoverflow.com/a/51252636/45375). – mklement0 Jun 11 '22 at 01:41
  • Exactly what I searched. simple and efficient! Thank you very much – Thomas Jun 12 '22 at 09:49
  • Also thank you for the remark about the security risk of such command – Thomas Jun 12 '22 at 09:56
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
}
  • [Splatting](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_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. – mklement0 Jun 11 '22 at 01:43
  • 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. – Thomas Jun 12 '22 at 09:52
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
}