安裝 SqlServer 模組
程式碼
$serverName = “your_server_name”
$databaseName = “your_database_name”
$connectionString = “Server=$serverName;Database=$databaseName;Integrated Security=True”
$connection = New-Object -TypeName System.Data.SqlClient.SqlConnection($connectionString)
$connection.Open()
$query = “SELECT * FROM your_table_name”
$command = $connection.CreateCommand()
$command.CommandText = $query
$result = $command.ExecuteReader()
while ($result.Read()) {
$column1 = $result.GetValue(0)
$column2 = $result.GetValue(1)
# 輸出結果
Write-Host “Column1: $column1, Column2: $column2”
}
$connection.Close()