将服务器添加到SQL管理工作室
问题描述:
我想在不同的服务器上向我的SSMS(SQL Managment Studio)注册的服务器添加一堆SQL(混合使用2000-2005)服务器实例,我按照本教程here一次只能添加一台服务器,而不是同时添加一台服务器。将服务器添加到SQL管理工作室
该列表当前在Excel中,但可以导入到记事本或任何其他文档格式,只要它可以被PowerShell命令识别即可。另外,需要注意的另一件事是我可以在哪里更改登录以使用sql身份验证?并指定用户名和密码?
New-Item $(Encode-Sqlname "SERVERNAME\INSTANCENAME") -itemtype registration -Value “server=MyServer;integrated security=true”
TKS
ERROR
New-Object : Cannot convert argument "1", with value: "", for "PSCredential" to
type "System.Security.SecureString": "Cannot convert the "" value of type "Sys
tem.String" to type "System.Security.SecureString"."
At line:4 char:32
+ -Credential (New-Object <<<< System.Management.Automation.PSCredenti
al("sa", "")) }
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodExcept
ion
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.Power
Shell.Commands.NewObjectCommand
答
如果您的Excel电子表格保存为CSV文件,你可以方便地使用Import-Csv cmdlet将它导入到PowerShell和自动注册的服务器列表通过他们的名称。
假设您的CSV文件看起来像这样:
|Name |
|Server1 |
|Server2 |
|Server3 |
以下命令将导入其内容为对象列表,一个在CSV文件中的每一行,其都具有Name
属性,包含实际值。然后,这些名称用于传递给New-Item cmdlet中的字符串内竟做了登记:
Import-Csv ServersToRegister.csv | ForEach-Object { `
New-Item $(Encode-Sqlname $_.Name) -ItemType Registration `
-Value ("server=$($_.Name);integrated security=true") }
您可以指定用户名和密码通过传递PSCredential对象使用连接到SQL Server实例New-Item cmdlet。所以完整的命令是:
Import-Csv ServersToRegister.csv | ForEach-Object { `
New-Item $(Encode-Sqlname $_.Name) -ItemType Registration `
-Value ("server=$($_.Name);integrated security=true") `
-Credential (New-Object System.Management.Automation.PSCredential("username", "password")) }
我觉得你的意思是.csv而不是cvs? – JackyBoi 2012-02-13 14:18:32
@JackyBoi当然。我纠正了我的答案。感谢您指出:) – 2012-02-13 14:24:22
好吧,我收到以下错误,它说,该值不能转换为system.security.securestring,具体错误我粘贴在原来的问题..其他比我猜测它正在处理所有那之前的代码? (只是猜测!)但是男人! – JackyBoi 2012-02-13 14:41:24