Inline expansion of powershell variable as cmdlet parameter?

后端 未结 1 396
悲哀的现实
悲哀的现实 2021-01-21 16:39

When calling a cmdlet, is it possible to expand the value of a powershell variable somehow so it acts as a parameter (with associated values) for the cmdlet?

Here\'s an

相关标签:
1条回答
  • 2021-01-21 17:22

    You can pass parameters like this to a PowerShell command using a hashtable instead e.g.:

    $CREDENTIALED_SECTION = @{Username=$USER_NAME; Password=$PASSWORD}
    
    Invoke-Sqlcmd -ServerInstance $SERVER_NAME -Query $SQL_STATEMENT @CREDENTIALED_SECTION -Database $DATABASE
    

    Note that it isn't necessary in this case to quote the PowerShell variables. You also need to use the splatting syntax in the command invocation e.g. @<hashtable_with_parameter_name_value_pairs>.

    0 讨论(0)
提交回复
热议问题