I am a beginner in PowerShell. I have a question about a script that I am working on.
Param(
[string]$SourceServer,
[string]$SourceDatabase,
[string]$InputFile,
[string]$TargetServer,
[string]$TargetDatabase,
[string]$TargetTable
)
$Data = invoke-sqlcmd -ServerInstance $SourceServer -Database $SourceDatabase -InputFile $InputFile
$ConnectionString = "Server=$TargetServer;Database=$TargetDatabase;Integrated Security=True"
$Connection=new-object System.Data.SqlClient.SQLConnection $ConnectionString
$Connection.Open()
$BulkCopy = new-object ("Data.SqlClient.SqlBulkCopy") $Connection
$BulkCopy.DestinationTableName = $TargetTable
$BulkCopy.WriteToServer($Data)
$Connection.Close()
So it is executing a script against a source server and loads the result into a target server. It works well (after some help in this thread http://social.technet.microsoft.com/Forums/windowsserver/en-US/d1dd3608-eb38-44a0-8ec7-668ea9315bb7/powershell-script-to-execute-sql-script).
Now I would like to add some additional columns to the array, before I load the result into the target server.
I would like to add the SourceServer, SourceDatabase, and the current datetime.
I have been testing with Add-Member and Get-Member, but I haven't been able to get it right. All help is greatly appreciated.
Ola Hallengren
http://ola.hallengren.com
Param(
[string]$SourceServer,
[string]$SourceDatabase,
[string]$InputFile,
[string]$TargetServer,
[string]$TargetDatabase,
[string]$TargetTable
)
$Data = invoke-sqlcmd -ServerInstance $SourceServer -Database $SourceDatabase -InputFile $InputFile
$ConnectionString = "Server=$TargetServer;Database=$TargetDatabase;Integrated Security=True"
$Connection=new-object System.Data.SqlClient.SQLConnection $ConnectionString
$Connection.Open()
$BulkCopy = new-object ("Data.SqlClient.SqlBulkCopy") $Connection
$BulkCopy.DestinationTableName = $TargetTable
$BulkCopy.WriteToServer($Data)
$Connection.Close()
So it is executing a script against a source server and loads the result into a target server. It works well (after some help in this thread http://social.technet.microsoft.com/Forums/windowsserver/en-US/d1dd3608-eb38-44a0-8ec7-668ea9315bb7/powershell-script-to-execute-sql-script).
Now I would like to add some additional columns to the array, before I load the result into the target server.
I would like to add the SourceServer, SourceDatabase, and the current datetime.
I have been testing with Add-Member and Get-Member, but I haven't been able to get it right. All help is greatly appreciated.
Ola Hallengren
http://ola.hallengren.com