PowerShell安全字符串转换

2022-03-08 00:00:00 powershell sql-server ssms

您好,我正在尝试这样做

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)) }

但是遇到这样的错误

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

我基本上希望能够添加用户名和密码(使用SQL身份验证),并尝试将密码转换为securestring,但从外观上看并不是很好。请协助TKS 我转到此链接,那里的代码很有帮助,但问题是,当我右键单击已注册的服务器并看到属性时,我看到它处于windows身份验证模式而不是sql身份验证模式


解决方案

尝试在管道之前的$PASS处赋值:

$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)) }

相关文章