1

Can I create a SQL login in SQL Server 2008r2 by passing an encrypted password? I am using the following code.

[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)
4

1 回答 1

0

.Create需要一个 SecureString。有关如何在 powershell 中创建 SecureStrings 的信息,请参阅在 Windows PowerShell 中使用密码、安全字符串和凭据。你需要使用ConvertTo-SecureString.

于 2013-05-15T08:59:51.467 回答