Avatar of Leo Torres
Leo Torres
Flag for United States of America asked on

Powershell SQL insert data into tables

My insert is now working.

   
add-pssnapin sqlserverprovidersnapin100 -ErrorAction SilentlyContinue
add-pssnapin sqlservercmdletsnapin100 -ErrorAction SilentlyContinue

CLS
$Server = "Server"
$Qry = "Select Top 5 Name from NOCTools.dbo.ADComputers Where Left(Name,1) in ('E','A')"
$ServerList = Invoke-Sqlcmd -MaxCharLength 10000000 -ServerInstance $Server -Query $Qry

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


 
foreach ($computer in $ServerList)
#$ServerList | Foreach-Object
{
    Try{
            $computer = $_.
            $Computerobj = "" | select ComputerName, Uptime, LastReboot
            $wmi = Get-WmiObject -ComputerName $computer -Query "SELECT LastBootUpTime FROM Win32_OperatingSystem" -EV Err -EA SilentlyContinue
            $now = Get-Date
            $boottime = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
            $uptime = $now - $boottime
            $d = $uptime.days
            $h = $uptime.hours
            $m = $uptime.Minutes
            $s = $uptime.Seconds
            $Computerobj.ComputerName = $computer
            $Computerobj.Uptime = "$d Days $h Hours $m Min $s Sec"
            $Computerobj.LastReboot = $boottime
            $Computerobj 

$Qry2 = "INSERT INTO NOCTools.dbo.RebootReport(Computer,RebootDate,Uptime,[Action],Reason,ADUser,Process)
         VALUES ('$computer','$boottime','$d Days $h Hours $m Min $s Sec',NULL,NULL,NULL,NULL)"

Write-Host @Qry2
         Invoke-Sqlcmd -MaxCharLength 10000000 -ServerInstance $Server -Query "INSERT INTO NOCTools.dbo.RebootReport(Computer,RebootDate,Uptime,[Action],Reason,ADUser,Process)
         VALUES ('$computer','$boottime','$d Days $h Hours $m Min $s Sec',NULL,NULL,NULL,NULL)"


}
 
        Catch {
                Write-host $computer
           }

 }                 

Open in new window



Output
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
PS C:\Users\usgltorres> 

Open in new window


How do I query the actual elements in the dataset
Powershell.NET ProgrammingC#

Avatar of undefined
Last Comment
Leo Torres

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
DBAduck - Ben Miller

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Leo Torres

ASKER
that worked
Your help has saved me hundreds of hours of internet surfing.
fblack61