通过传递加密密码创建新的sql登录

时间:2013-05-15 08:48:11

标签: sql-server powershell

我可以通过传递加密密码在SQL Server 2008r2中创建SQL登录吗?我使用以下代码。

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | Out-Null
#Assign variables
$Instance   = "INHYDDPC120"
$LoginName  = "MyNewLogin"
$Password   = "ggrytyrty"#(this has to be an encrypted password)
$DBName     = "Test_SMO_Database"

#Get the server object
$Server = New-Object ("Microsoft.SqlServer.Management.SMO.Server") $instance

#Get the login object if it exists
$Login = $Server.Logins.Item($LoginName)

IF (!($Login))  #check to see if login already exists
{
    #it doesn't, so instantiate a new login object
    $Login = New-Object ("Microsoft.SqlServer.Management.SMO.Login") ($Server, $LoginName) 

    #make it a SQL Login
    $Login.LoginType = [Microsoft.SqlServer.Management.Smo.LoginType]::SqlLogin

    #Create it on the server with the specified password

  # $Login.PolicyEnforcementenabled = $false
# $Login.PasswordExpirationEnabled = $false
 $Login.DefaultDatabase = $DBName     
 $Login.Create($Password)

1 个答案:

答案 0 :(得分:0)

.Create采用SecureString。有关如何在powershell中创建SecureStrings的信息,请参阅Working with Passwords, Secure Strings and Credentials in Windows PowerShell。您需要使用ConvertTo-SecureString