Powershell secure string conversion

青春壹個敷衍的年華 提交于 2019-12-11 18:00:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!