Add output of Powershell query Multiple WMI Classes to SQL

  • Hi All,

    I'm new to Powershell and I've been playing around with scripts all week but not having a scripting background I'm really not sure what I'm doing. I located the below script in another one of the topics under Powershell. I've chopped and changed bits of it and just can't seem to quite get it. I must say staring at it for a week I feel like I'm starting to understand some of it. I need help expanding on it. It works for retrieving more than 1 value from the same class and inserts the values. What I need to do is to be able to retrieve values from other classes and insert the result in to the appropriate table column. Any help / advice would be so appreciated. I'm not sure quite what the etiquette is for the forum. All I can say is I am a newbie and need some help.

    $servernames = Get-WmiObject -computername Anycompname -class win32_ComputerSystem | Select Name, Manufacturer

    # Open Connection

    $conn = New-Object System.Data.SqlClient.SqlConnection

    $connectionString = "Server=;Database=;user=;pwd=;"

    $conn.ConnectionString = $connectionString

    $conn.Open()

    # Create the Command object to execute the queries

    $cmd = New-Object System.Data.SqlClient.SqlCommand

    $cmd.Connection = $conn

    $cmd.CommandType = [System.Data.CommandType]::Text

    # Write Data

    foreach ($servername in $servernames)

    {

    # Compose Query for this $servername - watch the single quotes for string values!!

    $query = "INSERT INTO dbo.U_Server (ServerName, OSVersion) VALUES ('" + $servername.Name + "', '" + $servername.Manufacturer + "')"

    # Uncomment next line to display query for checking

    $query

    # Setup Command

    $cmd.CommandText = $query

    # Execute Command to insert data for this $drive

    $result = $cmd.ExecuteNonQuery()

    }

    # Tidy Up

    $conn.Close()

  • Hi Nicholas,

    It may be just me but I cannot see exactly what you want from anyone. Where is the problem in the script and what exactly is that problem?

    I'd love to help but I think that your post is too general (although posting the script was great).

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • Hi Gary,

    Sorry for not being clear. The script as is works for a single wmi class and I understand how to pull more than one value from that class and put it in sql.

    I need to pull data from a number of different values across a number of different classes. I just don't know how to go about that and then write those results between two different tables each with 3 columns.

    Again any help would be so greatly appreciated. Just enough to get me going.

  • Did you look at my answer to your question on stackoverflow?

Viewing 4 posts - 1 through 3 (of 3 total)

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