Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Arrays

Reply
 
Old 05-13-2008   #1 (permalink)
Jason Massie


 
 

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


 
 

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


 
 

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


 
 

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


 
 

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


 
 

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


 
 

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]


 
 

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


 
 

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


 
 

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
Reply

Thread Tools


Similar Threads
Thread Forum
Arrays and TextBoxes VB Script
Comparing arrays PowerShell
Re: Multidimensional Arrays PowerShell
Question About arrays VB Script
Overwriting Arrays PowerShell


Vista Forums 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 Ltd

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