On Azure, I am running multiple .sql files from a container in 100s of Azure SQL Databases via Powershell runbook.
I want Powershell to read the server name and the database name to run the scripts from my SQL Server table that looks like this:
Servername
Databasename
Status
Server-01
DB-01
Process
Server-01
DB-02
Skip
Server-02
DB-03
Process
In my current version of the Powershell script, it can read the files in the container and run them in a given server and database:
# Get the blob container
$blobs = Get-AzStorageContainer -Name $containerName -Context $ctx | Get-AzStorageBlob
# Download the blob content to localhost and execute each one
foreach ($blob in $blobs)
{
$file = Get-AzStorageBlobContent -Container $containerName -Blob $blob.Name -Destination "." -Context $ctx
Write-Output ("Processing file :" + $file.Name)
$query = Get-Content -Path $file.Name
Invoke-Sqlcmd -ServerInstance "Server-01.database.windows.net" -Database "DB-01" -Query $query -AccessToken $access_token
Write-Output ("This file is executed :" + $file.Name)
}
I am looking for a method that will read the rows from the table and feed them into the -ServerInstance and -Database fields in the Invoke-Sqlcmd. Ideally it can filter out the Skip rows.
One method is to load the database list into a DataTable and iterate over the list for each query. Change the $databaseListConnectionString in the example code below per your authentication method and set the connection AccessToken if/as needed.
# get database list
$databaseListConnectionString = "Data Source=YourServer;Initial Catalog=YourDatabase"
$databaseListQuery = "SELECT ServerName, DatabaseName FROM dbo.DatabaseList WHERE Status = 'Process';"
$dataAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($databaseListQuery, $databaseListConnectionString)
$dataAdapter.SelectCommand.Connection.AccessToken = $access_token
$databaseList = New-Object System.Data.DataTable
[void]$dataAdapter.Fill($databaseList)
# Get the blob container
$blobs = Get-AzStorageContainer -Name $containerName -Context $ctx | Get-AzStorageBlob
# Download the blob content to localhost and execute each one
foreach ($blob in $blobs) {
{
$file = Get-AzStorageBlobContent -Container $containerName -Blob $blob.Name -Destination "." -Context $ctx
Write-Output ("Processing file :" + $file.Name)
$query = Get-Content -Path $file.Name
foreach($database in $databaseList.Rows) {
Invoke-Sqlcmd -ServerInstance "$($database.ServerName)" -Database "$($database.DatabaseName)" -Query $query -AccessToken $access_token
Write-Output ("This file is executed :" + $file.Name)
}
}
}
Related
I have the following script that grabs an Excel file and loads it to an SQL Server database. It works perfectly fine when executed in Powershell ISE:
Import-Module DBATools
Import-Module ImportExcel
$File = "S:\Shares\Reporting\File.xlsx"
$Instance = "10.24.10.100"
$Password = ConvertTo-SecureString "Password" -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ("User", $Password)
$Database = "ESS_TAB"
$Table = "DB_DATA_AUTO"
$Data = Import-Excel -Path $File | ConvertTo-DbaDataTable
Write-DbaDataTable -SqlInstance $Instance -SqlCredential $Credential -Database $Database -InputObject $Data -Table $Table
If I try to run it from anywhere else I just get a cursor. There is never and error it just runs endlessly.
In one stored procedure, I have 5 columns:
How could I download images from provided URL in SQL Server table. Table structure in attached screenshot.
I am using this PowerShell script:
param (
[string]$DatabaseServer = "codb01d.brandmuscle.local"
)
################## OrderReportExport ################
Import-Module JAMS
$FileServerDestination = "C:\Temp\$(New-Guid)\"
New-Item $FileServerDestination -itemType directory
$OrderReportexport = $FileServerDestination+"OrderReport.csv"
# run reports
#sqlcmd -S $DatabaseServer -E -d "CentivPOS" -Q "Exec [dbo].[SE-OrderReport]" -o $OrderReportexport -W -s ","
function DataResults
{
$sql =#"
exec [dbo].[LoationImagedata]
"#
Invoke-Sqlcmd -ServerInstance $Databaseserver -Database "Centiv" -Query $sql -QueryTimeout "2000"
}
DataResults
First you need to find the final URI that leads to the image.
Like: https://imagerepo.com/some/image.png
Then you can use the Invoke-Webrequest cmdlet to download it:
Invoke-WebRequest -Uri https://imagerepo.com/some/image.png -OutFile C:\image.png
To upload it to SQL you can refer to this:
[Byte[]]$file = get-content -Encoding Byte C:\TEMP\pictures\image1.jpg
$hexString = ($file|ForEach-Object ToString X2) -join ''
$hexString = '0x'+$hexString
$cmd.CommandText ="UPDATE QuoteData SET PII_Image1 = $hexString Where QuoteNumber = '"+$WorkSheet.Range('G7').Text.replace("'","''")+"'"
I am using this power-shell script to fetch the versions of all SQL Servers in a list.
How may I export the result columns (only query output not error messages) into excel and send to email after the script is run?
Can someone help me add the required script please?
Import-Module SQLPS -DisableNameChecking
$ServerInstences = Get-Content "D:\DBA\All_Server_monitoring\ServerList.txt"
$SQLQuery = #"
Select ##Servername 'Server Name' ,##version 'Version'
"#
$DBName = "master"
$ServerInstences |
ForEach-Object {
$ServerObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $_
Invoke-Sqlcmd -ServerInstance $_ -Database $DBName -Query $SQLQuery
}
The easiest way to export data to a csv file is by using Export-CSV which takes an input object (or object array) and a path and can fill out the csv file from that object/array. For you, it would look like this:
$results = Invoke-Sqlcmd -ServerInstance $_ -Database $DBName -Query $SQLQuery
New-Item -Path "MYPATH.csv"
Export-CSV -Path "MYPATH.csv" -InputObject $results -Append
CSV files are versatile and can be opened with the most lightweight text editors. They also can be easily emailed and opened with Excel.
I was trying to import a CSV file from PowerShell over to my SQL Server database. I've already created the database and tables with columns. I got the data on a CSV file that I need to import.
I tried to research and found a bit of code I modified so it should be working, but when I run the code I get the error:
Import-CsvToSql : exeption calling "writetoserver" with "1"
argument(s): "The transaction is iether not associated with the
connection or has been completed"
But I hasn't imported the data to the table, so I don't know what's wrong.
Here is the code I've got so far:
Import-Module csvsqlimport
Import-CsvToSql -Csv C:\Users\Tim\Desktop\POWERSHELL\AutoParts.csv `
-SqlServer DHCP_SERVER -Database FeilAuto4 `
-Table dbo.Reservedele -FirstRowColumns -Delimiter ";" -Truncate
You may want to try the "SqlServer" module as it is being kept up to date by Microsoft and has multiple SQL cmdlets. The only downside is that you will have to separate the script into multiple commands based on their cmdlets.
## Install module if not installed, this is a one time install.
Install-Module SqlServer
## Input Variables
$csvPath = "C:\Users\Tim\Desktop\POWERSHELL\AutoParts.csv"
$csvDelimiter = ";"
$serverName = "DHCP_SERVER"
$databaseName = "FeilAuto4"
$tableSchema = "dbo"
$tableName = "Reservedele"
## Truncate Table
Invoke-Sqlcmd -ServerInstance $serverName -Database $databaseName -Query "TRUNCATE TABLE $tableSchema.$tableName"
## Import CSV into SQL
Import-Csv -Path $csvPath -Delimiter $csvDelimiter | Write-SqlTableData -ServerInstance $serverName -DatabaseName $databaseName -SchemaName $tableSchema -TableName $tableName -Force
Requirement : The below code will provide the instance name and database details & database states from the list of instances or servers available in SQL_Servers.txt from the path C:\PowerSQL\.
For few servers its not able to connect & throwing the error. I would like to keep these failed attempt server names in separate text file as Log.txt.
ForEach ($instance in Get-Content "C:\PowerSQL\SQL_Servers.txt")
{
Import-Module SQLPS -DisableNameChecking
Invoke-SQLcmd -Server $instance -Database master 'select ##servername as InstanceName,name as DatabaseName,state_desc as DBStatus from sys.databases' | Format-Table
}
Help on this is highly appreciated.
the below code is full filled my requirement.
Import-Module SQLPS -DisableNameChecking
ForEach ($instance in Get-Content "C:\PowerSQL\SQL_Servers.txt")
{
try {
Invoke-SQLcmd -Server $instance -Database master 'select ##servername as InstanceName,name as DatabaseName,state_desc as DBStatus from sys.databases'
$results += New-Object PSObject -Property $instanceinfo
} catch {
Add-Content "C:\PowerSQL\ErrorLog.txt" $instance
}
}
$results | export-csv -Path C:\PowerSQL\SQLServerInstance.csv -NoTypeInformation