首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PowerShell脚本中设置Server凭据或使用凭据进行调用

在PowerShell脚本中设置Server凭据或使用凭据进行调用
EN

Stack Overflow用户
提问于 2016-10-27 03:58:13
回答 1查看 3.5K关注 0票数 2

下面是连接到AWS数据库的工作PowerShell脚本。如何在没有提示的情况下设置脚本中硬编码的凭据,或者如何使用凭据调用脚本?

我尝试了几种选择,从互联网上使用PSCredentialcredentialsGet-WmiObjectSqlConnection,但它们都不起作用。

代码语言:javascript
复制
      $srv = new-object "Microsoft.SqlServer.Management.SMO.Server" "myBox"          

      #set the credentials here
      #This sets the connection to mixed-mode authentication
      $srv.ConnectionContext.LoginSecure=$false; 

      #Prompt for user credentials 
      $credential = Get-Credential  

      #Deal with the extra backslash character 
      $loginName = $credential.UserName -replace("\\","")  

      #This sets the login name  
      $srv.ConnectionContext.set_Login($loginName);  

      #This sets the password  
      $srv.ConnectionContext.set_SecurePassword($credential.Password)

      #test the connection
      $srv.Databases | Select name 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-27 06:05:18

代码语言:javascript
复制
    $SQLInstance = "Instance Name"
    $Database = "Database"
    $ID = "User ID" 
    $Password = "Password"

function Invoke-Sqlcommand 
{ 
    [CmdletBinding()] 
    param( 
    [Parameter(Position=0, Mandatory=$true)] [string]$ServerInstance, 
    [Parameter(Position=1, Mandatory=$false)] [string]$Database, 
    [Parameter(Position=2, Mandatory=$false)] [string]$Query, 
    [Parameter(Position=3, Mandatory=$false)] [string]$Username, 
    [Parameter(Position=4, Mandatory=$false)] [string]$Password, 
    [Parameter(Position=5, Mandatory=$false)] [Int32]$QueryTimeout=600, 
    [Parameter(Position=6, Mandatory=$false)] [Int32]$ConnectionTimeout=15, 
    [Parameter(Position=7, Mandatory=$false)] [ValidateScript({test-path $_})] [string]$InputFile, 
    [Parameter(Position=8, Mandatory=$false)] [ValidateSet("DataSet", "DataTable", "DataRow")] [string]$As="DataRow" 
    ) 

    if ($InputFile) 
    { 
        $filePath = $(resolve-path $InputFile).path 
        $Query =  [System.IO.File]::ReadAllText("$filePath") 
    } 

    $conn=new-object System.Data.SqlClient.SQLConnection 

    if ($Username) 
    { $ConnectionString = "Server={0};Database={1};User ID={2};Password={3};Trusted_Connection=False;Connect Timeout={4}" -f $ServerInstance,$Database,$Username,$Password,$ConnectionTimeout } 
    else 
    { $ConnectionString = "Server={0};Database={1};Integrated Security=True;Connect Timeout={2}" -f $ServerInstance,$Database,$ConnectionTimeout } 

    $conn.ConnectionString=$ConnectionString 

    #Following EventHandler is used for PRINT and RAISERROR T-SQL statements. Executed when -Verbose parameter specified by caller 
    if ($PSBoundParameters.Verbose) 
    { 
        $conn.FireInfoMessageEventOnUserErrors=$true 
        $handler = [System.Data.SqlClient.SqlInfoMessageEventHandler] {Write-Verbose "$($_)"} 
        $conn.add_InfoMessage($handler) 
    } 

    $conn.Open() 
    $cmd=new-object system.Data.SqlClient.SqlCommand($Query,$conn) 
    $cmd.CommandTimeout=$QueryTimeout 
    $ds=New-Object system.Data.DataSet 
    $da=New-Object system.Data.SqlClient.SqlDataAdapter($cmd) 
    [void]$da.fill($ds) 
    $conn.Close() 
    switch ($As) 
    { 
        'DataSet'   { Write-Output ($ds) } 
        'DataTable' { Write-Output ($ds.Tables) } 
        'DataRow'   { Write-Output ($ds.Tables[0]) } 
    } 

} 


Invoke-Sqlcommand -ServerInstance $SQLInstance -Database $Database -Query "Query Goes here" -Username $ID -Password $Password

使用上面的函数,您可以将用户名和密码以纯文本形式传递给Powershell中的SQL调用。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40275872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档