Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Output to SQL

$
0
0

Hi,

I have a script that will query WMI on my servers and return key metrics (disk space, memory use etc) to a SQL database.  I have the basics and can successfully write to the SQL database but I've hit a problem when there are multiple rows being returned for a single item.

For example, I query Win32_Volume for each server in a list.  Each server has at least 3 volumes which PowerShell outputs as seperate "group" of data for each volume:

The problem is when the data is written to the SQL table, it is written as one row, with 3 pieces of information in each column.

I want it in SQL as one row per volume per server.

I have the same issue when a server has multiple occurences of the same item, network adapter, processors etc.

I've tried pipeing the results into each Format- but that does't change what's written into SQL.  I'm guessing I need to put the result into an array and perform a seperate SQL INSERT for each element but I don't know where to start!

The full script (as it stands):

Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100

# get server names from SQL table to loop through
$DataSet = Invoke-Sqlcmd -Database ServerStats -Query "SELECT ServerName FROM [ServerStats].[dbo].[ServerList]" -ServerInstance <SQL>

# get disk stats
foreach ($element in $DataSet)
{
  $element
  $Server = $element.ServerName
 
    # Get metrics for each server in the ServerStats.ServerList
    $Output = Get-WmiObject -Class Win32_Volume -Computer $Server | Select-Object SystemName,Capacity,DriveType,DriveLetter,FreeSpace -ExcludeProperty "_*"

    # Output to SQL Server table
    ##Connect to the SQL server and the Database
    $conn = New-Object System.Data.SqlClient.SqlConnection("Data Source=<SQL>; Initial Catalog=ServerStats; Integrated Security=SSPI")
 
    ## Open DB Connection
    $conn.Open()
 
        #Create SQL Insert Statement with your values
        $insert_stmt = "INSERT INTO Disk(ComputerName,Size,DiskType,DriveLetter,FreeSpace) VALUES ('$Server','$($Output.Capacity)','$($Output.DriveType)','$($Output.DriveLetter)','$($Output.FreeSpace)')"
       
        ## Create your command
        $cmd = $conn.CreateCommand()
        $cmd.CommandText = $insert_stmt
 
        ## Invoke the Insert statement
        $cmd.ExecuteNonQuery()
 
    ## Close DB Connection
    $conn.Close()
}
# end disk stats

# get network adapter stats
foreach ($element in $DataSet)
{
  $element
  $Server = $element.ServerName
 
    # Get metrics for each server in the ServerStats.ServerList
    $Output = Get-WmiObject -Class Win32_NetworkAdapter -Computer $Server | Select-Object PSComputerName,ServiceName,MACAddress,Name,Speed -ExcludeProperty "_*"

    # Output to SQL Server table
    ##Connect to the SQL server and the Database
    $conn = New-Object System.Data.SqlClient.SqlConnection("Data Source=<SQL>; Initial Catalog=ServerStats; Integrated Security=SSPI")
 
    ## Open DB Connection
    $conn.Open()
 
        #Create SQL Insert Statement with your values
        $insert_stmt = "INSERT INTO NetworkAdapter(ComputerName,ServiceName,MACAddress,Name,Speed) VALUES ('$Server','$($Output.ServiceName)','$($Output.MACAddress)','$($Output.Name)','$($Output.Speed)')"
       
        ## Create your command
        $cmd = $conn.CreateCommand()
        $cmd.CommandText = $insert_stmt
 
        ## Invoke the Insert statement
        $cmd.ExecuteNonQuery()
 
    ## Close DB Connection
    $conn.Close()
}
# end network adapter stats

Any help would be appreciated..


Viewing all articles
Browse latest Browse all 6937

Trending Articles