2
votes

I am using below powershell script to execute

param([string]$Server,[string]$locusername,[string]$locpassword)

$password = '$locpassword' | ConvertTo-SecureString -asPlainText -Force
$username = $locusername 
$cred = New-Object System.Management.Automation.PSCredential($username,$password)

and i am getting error

Cannot bind argument to parameter 'String' because it is null. + CategoryInfo : InvalidData: (:) [ConvertTo-SecureString], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand

5
I don't get that error. Clearly you are using the quotes wrong (just leave them off) but even when you use the literal value of '$locpassword', this is valid input for ConvertTo-SecureString and should not throw an error.user2226112

5 Answers

2
votes

As already mentioned by @Swonkie in the comments

There is no need to quote the parameter. Just use the parameter as provided.

param([String]$Server, [String]$locusername, [String]$locpassword)
process {
    #NOTE: no quotes on parameter $locpassword
    $secure_password = ConvertTo-SecureString -String $locpassword -AsPlainText -Force
    $credential = New-Object System.Management.Automation.PSCredential($locusername, $secure_password)
    #...other code
}

If you are still getting that error then review the value stored in $locpassword as it may have been assigned a $null value.

1
votes

change this line

$password = '$locpassword' | ConvertTo-SecureString -asPlainText -Force

with this:

$password = ($locpassword | ConvertTo-SecureString -asPlainText -Force)
0
votes

it will be work

$password = ConvertTo-SecureString -String "******" -AsPlainText
-1
votes

use $locpassword or "$locpassword" , not '$locpassword' ;)

example here

-2
votes

instead of using single quotes use double quotes , for string expansion

$password = "$locpassword" | ConvertTo-SecureString -asPlainText -Force 

full code:

param([string]$Server,[string]$locusername,[string]$locpassword)

$password = "$locpassword" | ConvertTo-SecureString -asPlainText -Force 
$username = $locusername 
$cred = New-Object System.Management.Automation.PSCredential($username,$password)