2016-07-04 35 views

答えて

0

https://technet.microsoft.com/en-us/magazine/hh855069.aspxにあるTechnetの記事は、PowerShellを使用してSQL Serverデータベースに接続する方法を示しています。それはまた、あなたのスクリプトで使用できる例の機能が含まれます。

function Get-DatabaseData { 
    [CmdletBinding()] 
    param (
     [string]$connectionString, 
     [string]$query, 
     [switch]$isSQLServer 
    ) 
    if ($isSQLServer) { 
     Write-Verbose 'in SQL Server mode' 
     $connection = New-Object-TypeName System.Data.SqlClient.SqlConnection 
    } else { 
     Write-Verbose 'in OleDB mode' 
     $connection = New-Object-TypeName System.Data.OleDb.OleDbConnection 
    } 
    $connection.ConnectionString = $connectionString 
    $command = $connection.CreateCommand() 
    $command.CommandText = $query 
    if ($isSQLServer) { 
     $adapter = New-Object-TypeName System.Data.SqlClient.SqlDataAdapter $command 
    } else { 
     $adapter = New-Object-TypeName System.Data.OleDb.OleDbDataAdapter $command 
    } 
    $dataset = New-Object-TypeName System.Data.DataSet 
    $adapter.Fill($dataset) 
    $dataset.Tables[0] 
} 
function Invoke-DatabaseQuery { 
    [CmdletBinding()] 
    param (
     [string]$connectionString, 
     [string]$query, 
     [switch]$isSQLServer 
    ) 
    if ($isSQLServer) { 
     Write-Verbose 'in SQL Server mode' 
     $connection = New-Object-TypeName System.Data.SqlClient.SqlConnection 
    } else { 
     Write-Verbose 'in OleDB mode' 
     $connection = New-Object-TypeName System.Data.OleDb.OleDbConnection 
    } 
    $connection.ConnectionString = $connectionString 
    $command = $connection.CreateCommand() 
    $command.CommandText = $query 
    $connection.Open() 
    $command.ExecuteNonQuery() 
    $connection.close() 
} 

上記のスクリプトでは、TechNetの記事からの、あなただけの3つのパラメータを提供する必要があります:あなたは信頼関係接続を使用する接続文字列(=真統合セキュリティの場合)、実行するクエリ、およびDB(SQL ServerまたはOleDB)の種類。

0

さらに、Invoke-Sqlcmd2に戻ってこれらのすべてを自動化することもできます。我々は大きな成功を収めてそれを使用し、それは人生をより簡単にします。

関連する問題