how to insert out put of powershell script to a sql table?

  • How output of below script can be stored in SQL server table.

    # Check for failed SQL jobs on multiple servers

    [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | out-null

    foreach ($svr in get-content "C:\AllServers.txt")

    {

    write-host $svr

    $srv=New-Object "Microsoft.SqlServer.Management.Smo.Server" "$svr"

    $srv.jobserver.jobs | where-object {$_.lastrunoutcome -eq "Failed" -and $_.isenabled -eq $TRUE} | format-table name,lastrunoutcome,lastrundate -autosize

    }

    Help needed

  • [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | out-null

    foreach ($svr in get-content "C:\AllServers.txt")

    {

    write-host $svr

    $srv = New-Object "Microsoft.SqlServer.Management.Smo.Server" "$svr"

    $jobList = $srv.jobserver.jobs | where-object {$_.lastrunoutcome -eq "Failed" -and $_.isenabled -eq $TRUE}

    foreach($job in $jobList) {

    $sql += "Insert YourTableName (ServerName, JobName, LastRunOutcome, LastRunDate) Select '$svr', '" + $job.Name + "', '" + $job.LastRunOutcome + "', '" + $job.LastRunDate + "'; `n"

    }

    }

    $sql

    Just modify the insert for your table and column names. Then run the $sql into your target server.

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply