Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Executenonquery in pipeline

Closed Thread
 
Thread Tools Display Modes
Old 11-15-2007   #1 (permalink)
gurbao
Guest


 

Executenonquery in pipeline

Hi,

It's late in the evening here in Norway and I am struggeling with
something I believe will be "easy match" for one of you gurus out
there.

I have one or more ManagementObjects in a variable after collecting
diskinfo from the server.
I want to insert these into a sql table I have created.

$conn = New-Object system.Data.SqlClient.SqlConnection
$conn.connectionstring = "Data Source=.;Initial Catalog=dba;
Integrated Security=SSPI"
$conn.open()
$cmd = New-Object System.Data.SqlClient.SqlCommand($conn)


$diskinfo=gwmi -computer $server win32_logicaldisk -filter
"drivetype=3"

What comes next? I have tried different variations of

foreach ($disk in $diskinfo) {
$insert = "INSERT INTO dbo.diskusage (servername, deviceid,
volumename, totalsize, freespace) VALUES (`'$($server.servername)`',
`'$($disk.deviceid)`', `'$($disk.volumename)`', $($disk.size),$
($disk.freespace))"
$cmd.executenonquery($insert)
}

but unlucky so far...

Any tips?
Thanks,
Old 11-15-2007   #2 (permalink)
Oisin Grehan
Guest


 

Re: Executenonquery in pipeline

On Nov 15, 3:23 pm, gurbao <aud...@xxxxxx> wrote:
Quote:

> Hi,
>
> It's late in the evening here in Norway and I am struggeling with
> something I believe will be "easy match" for one of you gurus out
> there.
>
> I have one or more ManagementObjects in a variable after collecting
> diskinfo from the server.
> I want to insert these into a sql table I have created.
>
> $conn = New-Object system.Data.SqlClient.SqlConnection
> $conn.connectionstring = "Data Source=.;Initial Catalog=dba;
> Integrated Security=SSPI"
> $conn.open()
> $cmd = New-Object System.Data.SqlClient.SqlCommand($conn)
>
> $diskinfo=gwmi -computer $server win32_logicaldisk -filter
> "drivetype=3"
>
> What comes next? I have tried different variations of
>
> foreach ($disk in $diskinfo) {
> $insert = "INSERT INTO dbo.diskusage (servername, deviceid,
> volumename, totalsize, freespace) VALUES (`'$($server.servername)`',
> `'$($disk.deviceid)`', `'$($disk.volumename)`', $($disk.size),$
> ($disk.freespace))"
> $cmd.executenonquery($insert)
>
> }
>
> but unlucky so far...
>
> Any tips?
> Thanks,
What's the literal value of $insert before execution? can you dump
that here? also, define unlucky: error, no error but no data in db; in
short, elaborate!

- Oisin
Old 11-15-2007   #3 (permalink)
Keith Hill [MVP]
Guest


 

Re: Executenonquery in pipeline

"Oisin Grehan" <oising@xxxxxx> wrote in message
news:3a6f1901-8a30-4b5f-ac3f-bba8a71ec488@xxxxxx
Quote:

> On Nov 15, 3:23 pm, gurbao <aud...@xxxxxx> wrote:
Quote:

>> Hi,
>>
>> It's late in the evening here in Norway and I am struggeling with
>> something I believe will be "easy match" for one of you gurus out
>> there.
>>
>> I have one or more ManagementObjects in a variable after collecting
>> diskinfo from the server.
>> I want to insert these into a sql table I have created.
>>
>> $conn = New-Object system.Data.SqlClient.SqlConnection
>> $conn.connectionstring = "Data Source=.;Initial Catalog=dba;
>> Integrated Security=SSPI"
>> $conn.open()
>> $cmd = New-Object System.Data.SqlClient.SqlCommand($conn)
>>
>> $diskinfo=gwmi -computer $server win32_logicaldisk -filter
>> "drivetype=3"
>>
>> What comes next? I have tried different variations of
>>
>> foreach ($disk in $diskinfo) {
>> $insert = "INSERT INTO dbo.diskusage (servername, deviceid,
>> volumename, totalsize, freespace) VALUES (`'$($server.servername)`',
>> `'$($disk.deviceid)`', `'$($disk.volumename)`', $($disk.size),$
>> ($disk.freespace))"
>> $cmd.executenonquery($insert)
>>
>> }
>>
>> but unlucky so far...
>>
>> Any tips?
>> Thanks,
>
> What's the literal value of $insert before execution? can you dump
> that here? also, define unlucky: error, no error but no data in db; in
> short, elaborate!
I'm no DB guru by any stretch but do you have your DB table type set up to
accept such large integer values? My disk space size is:

250,056,704,000

That's well over the range of a standard 32-bit int.

--
Keith

Old 11-15-2007   #4 (permalink)
Karl Prosser[MVP]
Guest


 

Re: Executenonquery in pipeline

one thing to note, and i'm sorry it doesn't answer your question.. but
the example you made is not using the pipeline, but rather the vbscript
like language foreach loop..

if you wanted to use the pipeline you would do something like

gwmi -computer $server win32_logicaldisk -filter "drivetype=3" |
foreach-object { and something using $_ related to your database stuff }

but i know that doesn't answer your problems..
i would just try isolating to see what the problem is, do a simple
static insert, see if that works etc..
print something in the loop to see if the loop is even being called etc..

Karl
http://www.powershell.com
Old 11-16-2007   #5 (permalink)
gurbao
Guest


 

Re: Executenonquery in pipeline

On Nov 16, 5:40 am, "Karl Prosser[MVP]" <karl@xxxxxx_o_w_e_r_s_h_e_l_l.com>
wrote:
Quote:

> one thing to note, and i'm sorry it doesn't answer your question.. but
> the example you made is not using the pipeline, but rather the vbscript
> like language foreach loop..
>
> if you wanted to use the pipeline you would do something like
>
> gwmi -computer $server win32_logicaldisk -filter "drivetype=3" |
> foreach-object { and something using $_ related to your database stuff }
>
> but i know that doesn't answer your problems..
> i would just try isolating to see what the problem is, do a simple
> static insert, see if that works etc..
> print something in the loop to see if the loop is even being called etc..
>
> Karlhttp://www.powershell.com

To all of you: Thanks for your feedback.
Some sleep and a new day was all I needed :-)

foreach ($disk in $diskinfo) {
$cmd.CommandText = "INSERT INTO dba.dbo.diskusage (servername,
deviceid, volumename, totalsize, freespace) VALUES (`'$
($server.servername)`', `'$($disk.deviceid)`', `'$
($disk.volumename)`', $($disk.size),$($disk.freespace))"
$cmd.executenonquery() | out-null
}

I use bigint for the disksizes, that should be about
(9223372036854775807/1Gb) 8589934592 GB :-)
The pipeline-title of this text is misleading, but I started out doing
this in the pipeline, but before I got to post this I had changed my
mind ;-). Remember; it was laaate last night...

Thanks for helping us noobs out,
Old 11-16-2007   #6 (permalink)
Keith Hill [MVP]
Guest


 

Re: Executenonquery in pipeline

"gurbao" <audunj@xxxxxx> wrote in message
news:2328fb8f-f3ec-4a05-a1ad-59cc1270beaa@xxxxxx
Quote:

> foreach ($disk in $diskinfo) {
> $cmd.CommandText = "INSERT INTO dba.dbo.diskusage (servername,
> deviceid, volumename, totalsize, freespace) VALUES (`'$
> ($server.servername)`', `'$($disk.deviceid)`', `'$
> ($disk.volumename)`', $($disk.size),$($disk.freespace))"
> $cmd.executenonquery() | out-null
> }
>
> I use bigint for the disksizes, that should be about
> (9223372036854775807/1Gb) 8589934592 GB :-)
Yeah that should be plenty big enough. :-) BTW you don't need to escape
single quotes within a double quoted string.

--
Keith


Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pipeline and ScriptBlocks Shane PowerShell 8 01-21-2008 10:27 PM
Brackets in the pipeline Dmitry Sotnikov PowerShell 2 09-06-2007 10:33 AM
Using the $_ pipeline with WMI Larry R PowerShell 2 04-27-2007 09:27 AM
pipeline timeout William Stacey [C# MVP] PowerShell 0 04-09-2007 03:26 PM
A pipeline exercise Roman Kuzmin PowerShell 5 10-19-2006 10:56 AM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50