1

I'm trying to create a script to update Azure AD Privileged Identity Management which requires an object in the format below.

I want to be able to pass variables to the object, e.g. '{"required":$MFA}', where $MFA comes from a variable. I've tried the script block below but it doesn't recognize $MFA as a variable. Any ideas?

# Set MFA Variable
$MFA = "True"

# Create object
$myObject = [PSCustomObject]@{
    RuleIdentifer     = "JustificationRule"
    Setting = '{"required":$MFA}'
}

#Output object
write-host $myObject

The output just writes the $MFA text. It doesn't substitute $MFA for "True"

@{RuleIdentifer=JustificationRule; Setting={"required":$MFA}}

1 Answers1

1

You can specify strings using both single quotes and double quotes in Powershell.

The difference is obviously which characters inside have to be escaped, but also that single-quote strings doesn't do variable substitution, only double-quote strings do.

So a quick rewrite of your code outputs what you want:

# Set MFA Variable
$MFA = "True"

# Create object
$myObject = [PSCustomObject]@{
    RuleIdentifer     = "JustificationRule"
    Setting = "{""required"":$MFA}"
}

#Output object
write-host $myObject

Outputs:

@{RuleIdentifer=JustificationRule; Setting={"required":True}}
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825