Reputation: 139
The following PowerShell code to create two dummy accounts works fine for me when ran in the ISE:
$Password = ConvertTo-SecureString –AsPlainText –Force R4ndomT3xt
New-LocalUser -Name User1 -password $Password -PasswordNeverExpires -UserMayNotChangePassword
New-LocalUser -Name User2 -password $Password -PasswordNeverExpires -UserMayNotChangePassword
net user User1 /active:no
net user User2 /active:no
But when used in the following Chef recipe:
powershell_script 'Dummy' do
code <<-EOH
$Password = ConvertTo-SecureString –AsPlainText –Force R4ndomT3xt
New-LocalUser -Name User1 -password $Password -PasswordNeverExpires -UserMayNotChangePassword
New-LocalUser -Name User2 -password $Password -PasswordNeverExpires -UserMayNotChangePassword
net user User1 /active:no
net user User2 /active:no
EOH
End
..throws
"...Cannot bind parameter 'SecureKey'. Cannot convert the "R4ndomT3xt" value
of type "System.String" to type "System.Security.SecureString".
tion
n,chef-script20170918-4004-f15k8h..." etc
Can anyone suggest why the string conversion to SecureString works in the ISE but fails in the Chef recipe?
Thanks in advance.
Upvotes: 0
Views: 338
Reputation: 139
Replace
$Password = ConvertTo-SecureString –AsPlainText –Force R4ndomT3xt
With
$Password = "R4ndomT3xt" | ConvertTo-SecureString -AsPlainText –Force
Happy for someone to expand on why this works.
Upvotes: 0