命令行是通过参数传递进来的,如何执行它


PowerShell交流中心分类: 变量-函数-脚本-条件-循环-参数命令行是通过参数传递进来的,如何执行它
0
凯哥 asked 3年 ago

$pwd = ConvertTo-SecureString “password” -AsPlainText -Force
Add-Device -RackServerUuid uuid -Username USERID -Password $pwd
这样执行没问题
如果是
$pwd = ConvertTo-SecureString “password” -AsPlainText -Force
$cmd_line = “Add-Device -RackServerUuid uuid -Username USERID -Password $pwd”
invoke-expression $cmd_line
返回错误是 Cannot bind parameter ‘Password’. Cannot convert the “System.Security.SecureString” value of type “System.String”
to type “System.Security.SecureString”

请问这样的情况怎样解决呢?谢谢!

×用微信扫描并分享
1 Answers
1
Best Answer
Mooser Lee 管理员 answered 3年 ago
$cmd_line = {
  param($Password)
  $pwd = ConvertTo-SecureString $Password -AsPlainText -Force
Add-Device -RackServerUuid uuid -Username USERID -Password $pwd
}
& $cmd_line 'abc123'