PowerShell安全字符串转换

本教程将介绍PowerShell安全字符串转换的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。

PowerShell安全字符串转换 教程 第1张

问题描述

您好,我正在尝试这样做

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

好了关于PowerShell安全字符串转换的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。