We help IT Professionals succeed at work.
Get Started

Powershell SQL Insert

Last Modified: 2015-03-13
Instead of doing a select I want to do an insert instead of a select-Object at the bottom

$AdnServer = "MyServer"
$Qry = "Select Top 2 Name from NOCTools.dbo.ADComputers Where Left(Name,1) in ('E','A')"

$ServerList = Invoke-Sqlcmd -MaxCharLength 10000000 -ServerInstance $AdnServer -Query $Qry

Invoke-Sqlcmd -MaxCharLength 10000000 -ServerInstance $AdnServer -Query "Truncate Table NOCTools.dbo.RebootReport"

 $All = @()
foreach ($computer in $ServerList)
Get-WinEvent -ComputerName $computer -FilterHashtable @{logname='System'; id=1074}  | 
ForEach-Object {

    $rv = New-Object PSObject  | Select-Object Computer,Date, User, Action, Process, Reason, ReasonCode, Comment 
    $rv.Date = $_.TimeCreated
    $rv.Process = $_.Properties[0].Value
    $rv.Computer = $_.Properties[1].Value
    $rv.Reason = $_.Properties[2].Value
    $rv.ReasonCode = $_.Properties[3].Value
    $rv.Action = $_.Properties[4].Value
    $rv.Comment = $_.Properties[5].Value
    $rv.User = $_.Properties[6].Value
        }  | Select-Object COmputer,Date, Action, Reason, User |select -first 100 |  Format-Table -auto #r 


Open in new window

Code should look a little like this. not sure how piece it together.
$Qry2 = "INSERT INTO NOCTools.dbo.RebootReport(Computer,RebootDate,Uptime,[Action],Reason,ADUser,Process)
         VALUES ('$rv.Computer','$rv.Date','',$rv.Comment , $rv.ReasonCode ,$rv.User ,$rv.Process)"

Write-Host @Qry2
         Invoke-Sqlcmd -MaxCharLength 10000000 -ServerInstance $AdnServer -Query $Qry2

Open in new window

Watch Question
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
This problem has been solved!
Unlock 2 Answers and 49 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE