Hi All,
I am new to powershell workflow
I have 3 DB under "ABC" database server.
I am using SQLConnaction, DatatAdapter, DataSet to fetch record. It give me 1 dataset having 3 Datatable.
Can someone help me, to resolve the error and make it run.
workflow Test
{
param(
[parameter(Mandatory=$false)]
[int] $NumberOfDays,
[parameter(Mandatory=$false)]
[string] $SourceDBServer,
[parameter(Mandatory=$false)]
[string] $SourceDatabase ,
[parameter(Mandatory=$false)]
[string] $SourceUserId ,
[parameter(Mandatory=$false)]
[string] $SourcePassword ,
[parameter(Mandatory=$false)]
[string] $DestinationDBServer,
[parameter(Mandatory=$false)]
[string] $DestinationDatabase,
[parameter(Mandatory=$false)]
[string] $DestinationUserId,
[parameter(Mandatory=$false)]
[string] $DestinationPassword
)
Write-Verbose "<--- JOB START --->"
Write-Verbose ("This script Started at: $((Get-Date).ToString())")
Write-Verbose "#Processes in one execution of script $ProcessesInOneRun"
Write-Verbose "#Days in one execution of script $Days"
$dsTablesList = InlineScript
{
# Create connection to Source DB
$SourceDatabaseConnection =
New-Object System.Data.SqlClient.SqlConnection("Data Source = $using:SourceDBServer;Initial Catalog =$using:SourceDatabase;Integrated Security=False;User ID=$using:SourceUserId;Password =$using:SourcePassword;Encrypt=False;TrustServerCertificate=False")
$SourceDatabaseConnection.Open()
# Create command
$SourceDatabaseCommand = New-Object System.Data.SqlClient.SqlCommand
$SourceDatabaseCommand.Connection = $SourceDatabaseConnection
# Create command for Getting source tables for archiving
#Step 1 Get all table record for archiving
$SourceDatabaseCommand.CommandText = "Sproc_NAME"
$SourceDatabaseCommand.CommandType = [System.Data.CommandType]::StoredProcedure
$param1 = $SourceDatabaseCommand.Parameters.Add("@NumberOfDays" , [System.Data.SqlDbType]::INT)
$param1.Value = $using:NumberOfDays
# Execute the query
$dsSourceList = New-Object system.Data.DataSet
$da = New-Object system.Data.SqlClient.SqlDataAdapter($SourceDatabaseCommand)
[void]$da.fill($dsSourceList)
$SourceDatabaseConnection.Close()
$dsSourceList.Tables[0].TableName = "LMN"
$dsSourceList.Tables[1].TableName = "OPQ"
$dsSourceList.Tables[2].TableName = "XYZ"
return $dsSourceList
}
#Step 2 Loop foreach dataset from Step 1
ForEach ($dataset in $dsTablesList)
{
Try
{
ForEach ($table in $dataset.Tables)
{
InlineScript
{
$table = $Using:table
$ArchiveDatabaseConnection = New-Object System.Data.SqlClient.SqlConnection("Data Source = $using:DestinationDBServer;Initial Catalog = $using:DestinationDatabase;Integrated Security=False;User
ID=$using:DestinationUserId;Password = $using:DestinationPassword;Connect Timeout=5400;Encrypt=False;TrustServerCertificate=False")
#Write-Verbose " Archiving connection open"
$ArchiveDatabaseConnection.Open()
Write-Verbose " Inserting data into destinationtable"
#Step 6 INSERT into destination table
$bc = new-object ("System.Data.SqlClient.SqlBulkCopy") $ArchiveDatabaseConnection
$bc.DestinationTableName = $table.TableName
Write-Output $bc.DestinationTableName
if($bc.DestinationTableName -ne 'UsageLogArchive')
{
ForEach($row in $table)
{
$row = $Using:row
$cmd = new-object System.Data.SQLClient.SQLCommand
$cmd.CommandText = "Select 1 FROM "+$bc.DestinationTableName +" WHERE Id= "+$row["Id"]+""
$cmd.Connection = $ArchiveDatabaseConnection
$rowsUpdated = $cmd.ExecuteScalar()
if($rowsUpdated -eq '1')
{
$row.Delete()
}
}
$table.AcceptChanges()
}
$bc.WriteToServer($table)
#Close Connection
$ArchiveDatabaseConnection.Close()
#Write-Verbose " Archive connection closed"
}
}
InlineScript
{
$SourceDatabaseConnection =
New-Object System.Data.SqlClient.SqlConnection("Data Source = $using:SourceDBServer;Initial Catalog =$using:SourceDatabase;Integrated Security=False;User ID=$using:SourceUserId;Password =$using:SourcePassword;Encrypt=False;TrustServerCertificate=False")
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand("SPROC Name", $SourceDatabaseConnection)
$SqlCmd.CommandType = [System.Data.CommandType]::StoredProcedure
$param1 = $sqlcmd.Parameters.Add("@NumberOfDays" , [System.Data.SqlDbType]::INT)
$param1.Value = $using:NumberOfDays
$SourceDatabaseConnection.Open()
$result = $sqlcmd.ExecuteNonQuery()
$SourceDatabaseConnection.Close()
}
}
Catch
{
InlineScript
{
$ErrorMessage = $_.Exception.Message
Write-Verbose ("Failed to archive data")
Write-Output ("Failed to archive data. The error message was: $ErrorMessage")
}#Log Error?
Throw $_.Exception.Message
}
Finally
{
Write-Output ("The archiving for operational database has completed Execution at $((Get-Date).ToString())")
Write-Verbose ("**ARCHIVING OPERATIONAL DATABASE COMPLETED**")
}
}#foreach Process
}