I have a simple problem, but not simple clear example of solving it. I have a PowerShell 2.0 script running on a Windows 2008 R2 server that is taking a list of SQL Server 2008 R2 servers and looping through the $Svr.Db.JobServer.Jobs collection to record the $Job.OwnerLoginName and a few other properties, and writes the data in variables. Now I want to write these back to a MonitorSvr.DbADb.dbo.JobInfo table. I wrote function to call a SQL stored procedure, "usp_Insert_Job_Record", to do the actual insert using input parameters.
My problem is how to invoke the stored procedure, populate the input parameters and execute the stored procedure with no results returned. I looked at the Invoke-SQLCmd, but that didn't seem to do it. I am currently trying to instantiate the stored procedure from $svr.db.StoredProedure.Items("usp_Insert_Job_Record"), but I don't have a clear example on how to populate the parameter collection and execute. I imagine something like this:
$SMOServer = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Server $SQLServerName$db1 = New-Object Microsoft.SqlServer.Management.Smo.Database($SMOServer, "dbaAdmin")
$Jobs = $SMOServer.JobServer.Jobs
foreach ($Job in $Jobs)
{
$spInsertJobRecord = $db1.StoredProcedures.Item("usp_Insert_Job_Record")
$spInsertJobRecord.Param(0).Value = $SMOServer.Name
$spInsertJobRecord.Param(1).Value = $PortNum
$spInsertJobRecord.Param(2).Value = $AgentSvcName
$spInsertJobRecord.Param(3).Value = $Job.Name
$spInsertJobRecord.Param(4).Value = $Job.OwnerLoginName
$spInsertJobRecord.Param(5).Value = $Job.IsEnabled
$spInsertJobRecord.Param(6).Value = $Job.LastRunOutcome
$spInsertJobRecord.Param(7).Value = "ScanDate: " + $Today
$spInsertJobRecord.Execute
}
$SMOServer = $null
A clear and simple example would be appreciated.
Thanks!
Cheers!
Brandon