Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Add output of Powershell query Multiple WMI Classes to SQL Expand / Collapse
Author
Message
Posted Friday, November 22, 2013 7:43 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, December 16, 2013 4:26 AM
Points: 3, Visits: 11
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()
Post #1516785
Posted Friday, November 22, 2013 7:55 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 4:17 AM
Points: 5,600, Visits: 3,450
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!!!
Post #1516796
Posted Friday, November 22, 2013 8:59 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, December 16, 2013 4:26 AM
Points: 3, Visits: 11
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.
Post #1516834
Posted Friday, November 22, 2013 12:36 PM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, October 9, 2014 8:11 AM
Points: 60, Visits: 896
Did you look at my answer to your question on stackoverflow? [url=http://stackoverflow.com/questions/20144768/powershell-how-to-query-multiple-classes-and-write-in-to-sql-table][/url]
Post #1516904
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse