问题
Hi I am trying to do this
Import-Csv C:\sl.csv |$pass = convertto-securestring "abc123"-asplaintext -force | ForEach-Object {New-Item $(Encode-Sqlname $_.Name) -ItemType Registration -Value ("server=$($_.Name);integrated security=true") -Credential (New-Object System.Management.Automation.PSCredential("sa", $pass)) }
but getting error like this
Expressions are only allowed as the first element of a pipeline.
At line:1 char:29
+ Import-Csv C:\sl.csv |$pass <<<< = convertto-securestring "abc123"-asplaintext -fo
rce | ForEach-Object {New-Item $(Encode-Sqlname $_.Name) -ItemType Registration -Value ("server=$($_.Name);integrated security=true") -Credential (New-Object System.Management.Automation.PSCredential("sa", $pass)) }
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline
I basically want to be able to add the username and password (use sql authentication) and trying to convert my password to securestring and not doing a good job from the look of this. Pls assist tks I went to this link and the code there was helpful but the problem was when I right click on my registered servers and saw the properties I see that it is in windows authentication mode and not in sql authentication mode
回答1:
Try to assign value at $pass before the piping:
$pass = convertto-securestring "abc123"-asplaintext -force
Import-Csv C:\sl.csv | ForEach-Object {New-Item $(Encode-Sqlname $_.Name) -ItemType Registration -Value ("server=$($_.Name);integrated security=true") -Credential (New-Object System.Management.Automation.PSCredential("sa", $pass)) }
来源:https://stackoverflow.com/questions/9290830/powershell-secure-string-conversion