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

Arrays

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 05-13-2008   #1 (permalink)
Jason Massie
Guest


 

Arrays

I need to access array members. Based on what I read, I thought this would
work but no dice. "Index operation failed; the array index evaluated to
null." What am I doing wrong?

$Events =Get-EventLog Application -newest 2
$Events[$Events.eventid]



My System SpecsSystem Spec
Old 05-13-2008   #2 (permalink)
Jon
Guest


 

Re: Arrays


"Jason Massie" <jason**R3move**@statisticsio.com> wrote in message
news:e%23MeUMUtIHA.2068@xxxxxx
Quote:

>I need to access array members. Based on what I read, I thought this would
>work but no dice. "Index operation failed; the array index evaluated to
>null." What am I doing wrong?
>
> $Events =Get-EventLog Application -newest 2
> $Events[$Events.eventid]
>

Your first line retrieves the last 2 entries in the application log, and the
next line.. well.. I'm not sure what you're trying to do there. Perhaps this
is what you're after, which shows the eventid for each of the 2 array
members ....

$Events =Get-EventLog Application -newest 2
$events | foreach {$_.EventID}

--
Jon


My System SpecsSystem Spec
Old 05-13-2008   #3 (permalink)
Jason Massie
Guest


 

Re: Arrays

I am trying to reference the columns in a SQL 2008 invoke-sqlcmd command. I
tried with your syntax but it looks like it is treating $_.* as literals.

$Events =Get-EventLog Application -newest 2
ForEach-Object {invoke-sqlcmd -query "insert into AppLogger([time], [type],
[source],[eventid], [message]) values ($_.time, $_.type,
$_.source,$_.eventid, $_.message)" -ServerInstance LocalHost}


I get:
Invoke-Sqlcmd : The multi-part identifier ".time" could not be bound.
The multi-part identifier ".type" could not be bound.
The multi-part identifier ".source" could not be bound.
The multi-part identifier ".eventid" could not be bound.
The multi-part identifier ".message" could not be bound.

"Jon" <Email_Address@xxxxxx> wrote in message
news:u7j37SUtIHA.4560@xxxxxx
Quote:

>
> "Jason Massie" <jason**R3move**@statisticsio.com> wrote in message
> news:e%23MeUMUtIHA.2068@xxxxxx
Quote:

>>I need to access array members. Based on what I read, I thought this would
>>work but no dice. "Index operation failed; the array index evaluated to
>>null." What am I doing wrong?
>>
>> $Events =Get-EventLog Application -newest 2
>> $Events[$Events.eventid]
>>
>
>
> Your first line retrieves the last 2 entries in the application log, and
> the next line.. well.. I'm not sure what you're trying to do there.
> Perhaps this is what you're after, which shows the eventid for each of the
> 2 array members ....
>
> $Events =Get-EventLog Application -newest 2
> $events | foreach {$_.EventID}
>
> --
> Jon
>
>

My System SpecsSystem Spec
Old 05-13-2008   #4 (permalink)
Jon
Guest


 

Re: Arrays

"Jason Massie" <jason**R3move**@statisticsio.com> wrote in message
news:%23BAf2aUtIHA.4924@xxxxxx
Quote:

>I am trying to reference the columns in a SQL 2008 invoke-sqlcmd command. I
>tried with your syntax but it looks like it is treating $_.* as literals.
>
> $Events =Get-EventLog Application -newest 2
> ForEach-Object {invoke-sqlcmd -query "insert into AppLogger([time],
> [type], [source],[eventid], [message]) values ($_.time, $_.type,
> $_.source,$_.eventid, $_.message)" -ServerInstance LocalHost}
>
>
> I get:
> Invoke-Sqlcmd : The multi-part identifier ".time" could not be bound.
> The multi-part identifier ".type" could not be bound.
> The multi-part identifier ".source" could not be bound.
> The multi-part identifier ".eventid" could not be bound.
> The multi-part identifier ".message" could not be bound.
>


Well you're the piping '|' in there, by the looks of it. Perhaps try it with

$Events | ForEach-Object {invoke-sqlcmd .........


--
Jon




My System SpecsSystem Spec
Old 05-13-2008   #5 (permalink)
Jon
Guest


 

Re: Arrays

"Jon" <Email_Address@xxxxxx> wrote in message
news:ejb3xmUtIHA.1436@xxxxxx
Quote:

> Well you're the piping '|' in there, by the looks of it. Perhaps try it
> with
>
> $Events | ForEach-Object {invoke-sqlcmd .........
>

The 'missing' was missing. Should have read

"Well you're missing the piping '|' in there, by the looks of it..."

--
Jon



My System SpecsSystem Spec
Old 05-13-2008   #6 (permalink)
jason
Guest


 

Re: Arrays

I think I am getting close. It is like it is trying to insert the full path
to the "column" name instead of the value.

$Events = Get-EventLog Application -newest 1
$Events | foreach-object {invoke-sqlcmd -query "insert into
AppLogger([source]) values ($events.source)" -ServerInstance LocalHost}

It returns:

Invoke-Sqlcmd : The multi-part identifier
"System.Diagnostics.EventLogEntry.source" could not be bound.

Any other ideas?


"Jon" <Email_Address@xxxxxx> wrote in message
news:%23l18joUtIHA.548@xxxxxx
Quote:

> "Jon" <Email_Address@xxxxxx> wrote in message
> news:ejb3xmUtIHA.1436@xxxxxx
Quote:

>> Well you're the piping '|' in there, by the looks of it. Perhaps try it
>> with
>>
>> $Events | ForEach-Object {invoke-sqlcmd .........
>>
>
>
> The 'missing' was missing. Should have read
>
> "Well you're missing the piping '|' in there, by the looks of it..."
>
> --
> Jon
>
>
>
My System SpecsSystem Spec
Old 05-13-2008   #7 (permalink)
jason
Guest


 

Re: Arrays

Hmm, this inserts the right value(MSSQLSERVER) but it is really not the
logic that I want.
$Events = Get-EventLog Application -newest 1
$source = $events.source
$Events | foreach-object {invoke-sqlcmd -query "insert into
demodb..AppLogger([source]) values ('$source')" -ServerInstance LocalHost}

This executes but it inserts this value:
'System.Diagnostics.EventLogEntry.source'
$Events = Get-EventLog Application -newest 1
$Events | foreach-object {invoke-sqlcmd -query "insert into
demodb..AppLogger([source]) values ('$events.source')" -ServerInstance
LocalHost}

What am I missing? Thanks.

"Jason Massie" <jason**R3move**@statisticsio.com> wrote in message
news:e%23MeUMUtIHA.2068@xxxxxx
Quote:

>I need to access array members. Based on what I read, I thought this would
>work but no dice. "Index operation failed; the array index evaluated to
>null." What am I doing wrong?
>
> $Events =Get-EventLog Application -newest 2
> $Events[$Events.eventid]
>
My System SpecsSystem Spec
Old 05-13-2008   #8 (permalink)
Brandon [MVP]
Guest


 

Re: Arrays

two things.

1) when you put a variable in a string and you want to access a property.
You need to wrap in $() so powershell knows to unwrap it
2) I dont really think this is what you expect. $Events is an collection of
Events. If you do $Events.Source it implies that $Events is an element in
the collection which is not. When you pipe a collection through
foreach-object you can reference the current element by using $_.

Try this
$Events = Get-EventLog Application -newest 1
$Events | foreach-object {invoke-sqlcmd -query "insert into
demodb..AppLogger([source]) values ($_.source)" -ServerInstance LocalHost}

"jason" <jason-r3move@xxxxxx> wrote in message
news:7DA87571-E9B8-4098-BB36-9109C9947D96@xxxxxx
Quote:

> Hmm, this inserts the right value(MSSQLSERVER) but it is really not the
> logic that I want.
> $Events = Get-EventLog Application -newest 1
> $source = $events.source
> $Events | foreach-object {invoke-sqlcmd -query "insert into
> demodb..AppLogger([source]) values ('$source')" -ServerInstance LocalHost}
>
> This executes but it inserts this value:
> 'System.Diagnostics.EventLogEntry.source'
> $Events = Get-EventLog Application -newest 1
> $Events | foreach-object {invoke-sqlcmd -query "insert into
> demodb..AppLogger([source]) values ('$events.source')" -ServerInstance
> LocalHost}
>
> What am I missing? Thanks.
>
> "Jason Massie" <jason**R3move**@statisticsio.com> wrote in message
> news:e%23MeUMUtIHA.2068@xxxxxx
Quote:

>>I need to access array members. Based on what I read, I thought this would
>>work but no dice. "Index operation failed; the array index evaluated to
>>null." What am I doing wrong?
>>
>> $Events =Get-EventLog Application -newest 2
>> $Events[$Events.eventid]
>>
>
My System SpecsSystem Spec
Old 05-13-2008   #9 (permalink)
jason
Guest


 

Re: Arrays

It took a little tweak. Powershell in SQL 2008 is really cool. Where should
a powershell newbie get a crash course?

$Events = Get-EventLog Application -newest 100
$Events | foreach-object {invoke-sqlcmd -query "insert into
demodb..AppLogger([source]) values ('$($_.source)')" -ServerInstance
LocalHost}


"Brandon [MVP]" <tshell.mask@xxxxxx> wrote in message
news:CE48DB89-27E0-4AAC-8FFA-09F19037239E@xxxxxx
Quote:

> two things.
>
> 1) when you put a variable in a string and you want to access a property.
> You need to wrap in $() so powershell knows to unwrap it
> 2) I dont really think this is what you expect. $Events is an collection
> of Events. If you do $Events.Source it implies that $Events is an element
> in the collection which is not. When you pipe a collection through
> foreach-object you can reference the current element by using $_.
>
> Try this
> $Events = Get-EventLog Application -newest 1
> $Events | foreach-object {invoke-sqlcmd -query "insert into
> demodb..AppLogger([source]) values ($_.source)" -ServerInstance LocalHost}
>
> "jason" <jason-r3move@xxxxxx> wrote in message
> news:7DA87571-E9B8-4098-BB36-9109C9947D96@xxxxxx
Quote:

>> Hmm, this inserts the right value(MSSQLSERVER) but it is really not the
>> logic that I want.
>> $Events = Get-EventLog Application -newest 1
>> $source = $events.source
>> $Events | foreach-object {invoke-sqlcmd -query "insert into
>> demodb..AppLogger([source]) values ('$source')" -ServerInstance
>> LocalHost}
>>
>> This executes but it inserts this value:
>> 'System.Diagnostics.EventLogEntry.source'
>> $Events = Get-EventLog Application -newest 1
>> $Events | foreach-object {invoke-sqlcmd -query "insert into
>> demodb..AppLogger([source]) values ('$events.source')" -ServerInstance
>> LocalHost}
>>
>> What am I missing? Thanks.
>>
>> "Jason Massie" <jason**R3move**@statisticsio.com> wrote in message
>> news:e%23MeUMUtIHA.2068@xxxxxx
Quote:

>>>I need to access array members. Based on what I read, I thought this
>>>would work but no dice. "Index operation failed; the array index
>>>evaluated to null." What am I doing wrong?
>>>
>>> $Events =Get-EventLog Application -newest 2
>>> $Events[$Events.eventid]
>>>
>>
>
My System SpecsSystem Spec
Old 05-14-2008   #10 (permalink)
Alex K. Angelopoulos
Guest


 

Re: Arrays

Bruce Payette's book is a good start. To get a feel for it, though, the best
thing I've found is to just use it, and check the docs installed with
PowerShell - they cover a _bunch_ of the core of the language, including
useful examples for some scenarios.

And post questions. Live people can always give a tailor-made crash course.
: )

"jason" <jason-r3move@xxxxxx> wrote in message
news:F9AB82D3-9505-4C3B-925F-7FD4D0AD0C18@xxxxxx
Quote:

> It took a little tweak. Powershell in SQL 2008 is really cool. Where
> should a powershell newbie get a crash course?
>
> $Events = Get-EventLog Application -newest 100
> $Events | foreach-object {invoke-sqlcmd -query "insert into
> demodb..AppLogger([source]) values ('$($_.source)')" -ServerInstance
> LocalHost}
>
>
> "Brandon [MVP]" <tshell.mask@xxxxxx> wrote in message
> news:CE48DB89-27E0-4AAC-8FFA-09F19037239E@xxxxxx
Quote:

>> two things.
>>
>> 1) when you put a variable in a string and you want to access a property.
>> You need to wrap in $() so powershell knows to unwrap it
>> 2) I dont really think this is what you expect. $Events is an collection
>> of Events. If you do $Events.Source it implies that $Events is an element
>> in the collection which is not. When you pipe a collection through
>> foreach-object you can reference the current element by using $_.
>>
>> Try this
>> $Events = Get-EventLog Application -newest 1
>> $Events | foreach-object {invoke-sqlcmd -query "insert into
>> demodb..AppLogger([source]) values ($_.source)" -ServerInstance
>> LocalHost}
>>
>> "jason" <jason-r3move@xxxxxx> wrote in message
>> news:7DA87571-E9B8-4098-BB36-9109C9947D96@xxxxxx
Quote:

>>> Hmm, this inserts the right value(MSSQLSERVER) but it is really not the
>>> logic that I want.
>>> $Events = Get-EventLog Application -newest 1
>>> $source = $events.source
>>> $Events | foreach-object {invoke-sqlcmd -query "insert into
>>> demodb..AppLogger([source]) values ('$source')" -ServerInstance
>>> LocalHost}
>>>
>>> This executes but it inserts this value:
>>> 'System.Diagnostics.EventLogEntry.source'
>>> $Events = Get-EventLog Application -newest 1
>>> $Events | foreach-object {invoke-sqlcmd -query "insert into
>>> demodb..AppLogger([source]) values ('$events.source')" -ServerInstance
>>> LocalHost}
>>>
>>> What am I missing? Thanks.
>>>
>>> "Jason Massie" <jason**R3move**@statisticsio.com> wrote in message
>>> news:e%23MeUMUtIHA.2068@xxxxxx
>>>>I need to access array members. Based on what I read, I thought this
>>>>would work but no dice. "Index operation failed; the array index
>>>>evaluated to null." What am I doing wrong?
>>>>
>>>> $Events =Get-EventLog Application -newest 2
>>>> $Events[$Events.eventid]
>>>>
>>>
>>
>
My System SpecsSystem Spec
Closed Thread
Update your Vista Drivers Update Your Drivers Now!!

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Overwriting Arrays DavidLMeissner PowerShell 1 12-27-2007 07:33 PM
Arrays in Powershell Daren Daigle PowerShell 2 12-03-2007 07:37 AM
Multidimensional arrays Jacob Saaby Nielsen PowerShell 6 11-27-2007 10:44 AM
Working with arrays Jobbsy PowerShell 6 11-19-2007 05:15 AM
Bug with multidimensional arrays IJuan PowerShell 1 04-22-2007 02:24 PM


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 51