[英]How to run powershell command with parameter name is variable
I need to run the same command Get-QadUser
and Set-QadUser
to one property and his value but my property is a variable. 我需要将相同的命令
Get-QadUser
和Set-QadUser
到一个属性和他的值,但我的属性是一个变量。
my code (Set doesn't work !) : 我的代码(设置不起作用!):
$property = 'MobilePhone' # OK
$value = ($User | get-QadUser -IncludedProperties $property).$property # OK
$user | Set-QadUser -$PropertyName $NewValue # NOK
You can use PowerShell feature called Splatting in this case as explained by Don Jones himself here: Windows PowerShell: Splatting . 在这种情况下,您可以使用名为Splatting的 PowerShell功能,如Don Jones本人所解释的: Windows PowerShell:Splatting 。
In this you can define the parameters as a dictionary object of Property and Value pairs as below: 在此,您可以将参数定义为Property和Value对的字典对象,如下所示:
$parameters = @{$FirstPropertyName = $FirstValue; $SecondPropertyName = $SecondValue; $ThirdPropertyName = $ThirdValue}
Then you can pass this to your cmdlet using @
operator as shown below: 然后,您可以使用
@
运算符将其传递给cmdlet,如下所示:
Set-QadUser @parameters
Your complete working script will look as below: 您的完整工作脚本如下所示:
$property = 'MobilePhone' # OK
$value = ($User | get-QadUser -IncludedProperties $property).$property # OK
$parameters = @{$PropertyName = $NewValue}
$user | Set-QadUser @parameters # OK
Edit: I failed to notice earlier that PetSerAl already gave the answer in comments. 编辑:我之前没有注意到PetSerAl已在评论中给出答案。 I hope this answer also adds value overall.
我希望这个答案也能增加整体价值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.