Discussion:
Write to file multiple times from Powershell
(too old to reply)
Andrew Watt [MVP]
2007-01-15 14:26:09 UTC
Permalink
Hi,

Have a look at the set-content and add-content cmdlets.

Andrew Watt MVP

On Mon, 15 Jan 2007 05:08:00 -0800, Kjetil Thorstensen
Hi.
I'm trying to write a script that parses logfiles, both IIS and other apps'
logs and extracts certain information from specific lines ... not all. The
extraction works fine using $var.substring and family, but I can't find out
how to write the info to a file.
I read the file into the variable (array) called $loglines and plan to
foreach ($i in $loglines) {
if ($loglines[$i].Contains("Process file") ) {
$Time=$loglines[$i].substring(0,14)
$Directory=$loglines[$i].substring( $loglines[$i].IndexOf("\"),
($loglines[$i].LastIndexOf("\")-$loglines[$i].IndexOf("\")))
$Filename=$loglines[14].substring($loglines[$i].LastIndexOf("\")+1,($loglines[$i].Length-$loglines[$i].LastIndexOf("\")-1))
# Here I need to get it to file, one line at a time
}
}
Ideas anyone?
Thanks in advance
/Kjetil
Andrew Watt [MVP]
2007-01-15 17:50:21 UTC
Permalink
Hi again,

I'm not sure which of two things you want to do.

If you want to append to an array do something like this:

$a = 1,2,3
$a
$a += 4,5
$a

If you want to loop through an array whose length you don't know adapt
this:

foreach ($element in $array){
do-something
}

I hope that helps.

Andrew Watt MVP
Author Professional Windows PowerShell (Wrox)


On Mon, 15 Jan 2007 06:39:01 -0800, Kjetil Thorstensen
Bingo! Thanks a lot.
Just one small addition - I was at one point trying to do the same with an
array - declaring it up front and then incrementing the index each time I
entered the loop. That did not work. I could create an array, but I had to
fill it up front - I couldn't find a way to "append".
Am I trying something that doesn't work?
/Kjetil
Post by Andrew Watt [MVP]
Hi,
Have a look at the set-content and add-content cmdlets.
Andrew Watt MVP
On Mon, 15 Jan 2007 05:08:00 -0800, Kjetil Thorstensen
Hi.
I'm trying to write a script that parses logfiles, both IIS and other apps'
logs and extracts certain information from specific lines ... not all. The
extraction works fine using $var.substring and family, but I can't find out
how to write the info to a file.
I read the file into the variable (array) called $loglines and plan to
foreach ($i in $loglines) {
if ($loglines[$i].Contains("Process file") ) {
$Time=$loglines[$i].substring(0,14)
$Directory=$loglines[$i].substring( $loglines[$i].IndexOf("\"),
($loglines[$i].LastIndexOf("\")-$loglines[$i].IndexOf("\")))
$Filename=$loglines[14].substring($loglines[$i].LastIndexOf("\")+1,($loglines[$i].Length-$loglines[$i].LastIndexOf("\")-1))
# Here I need to get it to file, one line at a time
}
}
Ideas anyone?
Thanks in advance
/Kjetil
Kjetil Thorstensen
2007-01-16 05:12:00 UTC
Permalink
Yes, it's exactly what I was looking for. I hadn't figured out how to append
to an array. (I've been away from development for some time and the += syntax
is new to me when it comes to arrays.)

Thank you very much.

Kind regards
/Kjetil
Post by Andrew Watt [MVP]
Hi again,
I'm not sure which of two things you want to do.
$a = 1,2,3
$a
$a += 4,5
$a
If you want to loop through an array whose length you don't know adapt
foreach ($element in $array){
do-something
}
I hope that helps.
Andrew Watt MVP
Author Professional Windows PowerShell (Wrox)
On Mon, 15 Jan 2007 06:39:01 -0800, Kjetil Thorstensen
Bingo! Thanks a lot.
Just one small addition - I was at one point trying to do the same with an
array - declaring it up front and then incrementing the index each time I
entered the loop. That did not work. I could create an array, but I had to
fill it up front - I couldn't find a way to "append".
Am I trying something that doesn't work?
/Kjetil
Post by Andrew Watt [MVP]
Hi,
Have a look at the set-content and add-content cmdlets.
Andrew Watt MVP
On Mon, 15 Jan 2007 05:08:00 -0800, Kjetil Thorstensen
Hi.
I'm trying to write a script that parses logfiles, both IIS and other apps'
logs and extracts certain information from specific lines ... not all. The
extraction works fine using $var.substring and family, but I can't find out
how to write the info to a file.
I read the file into the variable (array) called $loglines and plan to
foreach ($i in $loglines) {
if ($loglines[$i].Contains("Process file") ) {
$Time=$loglines[$i].substring(0,14)
$Directory=$loglines[$i].substring( $loglines[$i].IndexOf("\"),
($loglines[$i].LastIndexOf("\")-$loglines[$i].IndexOf("\")))
$Filename=$loglines[14].substring($loglines[$i].LastIndexOf("\")+1,($loglines[$i].Length-$loglines[$i].LastIndexOf("\")-1))
# Here I need to get it to file, one line at a time
}
}
Ideas anyone?
Thanks in advance
/Kjetil
Al Dunbar [MS-MVP]
2007-01-17 07:00:19 UTC
Permalink
Almost elegant, in fact, especially when compared with REDIM PRESERVE!

/Al
Post by Kjetil Thorstensen
Yes, it's exactly what I was looking for. I hadn't figured out how to append
to an array. (I've been away from development for some time and the += syntax
is new to me when it comes to arrays.)
Thank you very much.
Kind regards
/Kjetil
Post by Andrew Watt [MVP]
Hi again,
I'm not sure which of two things you want to do.
$a = 1,2,3
$a
$a += 4,5
$a
If you want to loop through an array whose length you don't know adapt
foreach ($element in $array){
do-something
}
I hope that helps.
Andrew Watt MVP
Author Professional Windows PowerShell (Wrox)
On Mon, 15 Jan 2007 06:39:01 -0800, Kjetil Thorstensen
Bingo! Thanks a lot.
Just one small addition - I was at one point trying to do the same with an
array - declaring it up front and then incrementing the index each time I
entered the loop. That did not work. I could create an array, but I had to
fill it up front - I couldn't find a way to "append".
Am I trying something that doesn't work?
/Kjetil
Post by Andrew Watt [MVP]
Hi,
Have a look at the set-content and add-content cmdlets.
Andrew Watt MVP
On Mon, 15 Jan 2007 05:08:00 -0800, Kjetil Thorstensen
Hi.
I'm trying to write a script that parses logfiles, both IIS and other apps'
logs and extracts certain information from specific lines ... not all. The
extraction works fine using $var.substring and family, but I can't find out
how to write the info to a file.
I read the file into the variable (array) called $loglines and plan to
foreach ($i in $loglines) {
if ($loglines[$i].Contains("Process file") ) {
$Time=$loglines[$i].substring(0,14)
$Directory=$loglines[$i].substring( $loglines[$i].IndexOf("\"),
($loglines[$i].LastIndexOf("\")-$loglines[$i].IndexOf("\")))
$Filename=$loglines[14].substring($loglines[$i].LastIndexOf("\")+1,($logline
s[$i].Length-$loglines[$i].LastIndexOf("\")-1))
Post by Kjetil Thorstensen
Post by Andrew Watt [MVP]
Post by Andrew Watt [MVP]
# Here I need to get it to file, one line at a time
}
}
Ideas anyone?
Thanks in advance
/Kjetil
ThatVaiGuy
2009-12-28 18:29:01 UTC
Permalink
So, in doing this since I'm new to scripting in general, what would the
syntax look like if I wanted to remove all lines from each of my IIS log
files on a remote IIS server that had a certain IP or two say for the
instance of an F5 load balancer that's doing get requests a few times a
second, and then save the remaining results (Without the F5 data) to a log
file on the local computer?

I'm using Webtrends and my license is being charged for all these darn F5
and What's up gold entries in the logs as if they are people hitting the
site. WT told me to use a script to clean up the data before they analyzed
it but could not provide me with one.
Post by Kjetil Thorstensen
Yes, it's exactly what I was looking for. I hadn't figured out how to append
to an array. (I've been away from development for some time and the += syntax
is new to me when it comes to arrays.)
Thank you very much.
Kind regards
/Kjetil
Post by Andrew Watt [MVP]
Hi again,
I'm not sure which of two things you want to do.
$a = 1,2,3
$a
$a += 4,5
$a
If you want to loop through an array whose length you don't know adapt
foreach ($element in $array){
do-something
}
I hope that helps.
Andrew Watt MVP
Author Professional Windows PowerShell (Wrox)
On Mon, 15 Jan 2007 06:39:01 -0800, Kjetil Thorstensen
Bingo! Thanks a lot.
Just one small addition - I was at one point trying to do the same with an
array - declaring it up front and then incrementing the index each time I
entered the loop. That did not work. I could create an array, but I had to
fill it up front - I couldn't find a way to "append".
Am I trying something that doesn't work?
/Kjetil
Post by Andrew Watt [MVP]
Hi,
Have a look at the set-content and add-content cmdlets.
Andrew Watt MVP
On Mon, 15 Jan 2007 05:08:00 -0800, Kjetil Thorstensen
Hi.
I'm trying to write a script that parses logfiles, both IIS and other apps'
logs and extracts certain information from specific lines ... not all. The
extraction works fine using $var.substring and family, but I can't find out
how to write the info to a file.
I read the file into the variable (array) called $loglines and plan to
foreach ($i in $loglines) {
if ($loglines[$i].Contains("Process file") ) {
$Time=$loglines[$i].substring(0,14)
$Directory=$loglines[$i].substring( $loglines[$i].IndexOf("\"),
($loglines[$i].LastIndexOf("\")-$loglines[$i].IndexOf("\")))
$Filename=$loglines[14].substring($loglines[$i].LastIndexOf("\")+1,($loglines[$i].Length-$loglines[$i].LastIndexOf("\")-1))
# Here I need to get it to file, one line at a time
}
}
Ideas anyone?
Thanks in advance
/Kjetil
ThatVaiGuy
2009-12-28 18:29:01 UTC
Permalink
So, in doing this since I'm new to scripting in general, what would the
syntax look like if I wanted to remove all lines from each of my IIS log
files on a remote IIS server that had a certain IP or two say for the
instance of an F5 load balancer that's doing get requests a few times a
second, and then save the remaining results (Without the F5 data) to a log
file on the local computer?

I'm using Webtrends and my license is being charged for all these darn F5
and What's up gold entries in the logs as if they are people hitting the
site. WT told me to use a script to clean up the data before they analyzed
it but could not provide me with one.
Post by Kjetil Thorstensen
Yes, it's exactly what I was looking for. I hadn't figured out how to append
to an array. (I've been away from development for some time and the += syntax
is new to me when it comes to arrays.)
Thank you very much.
Kind regards
/Kjetil
Post by Andrew Watt [MVP]
Hi again,
I'm not sure which of two things you want to do.
$a = 1,2,3
$a
$a += 4,5
$a
If you want to loop through an array whose length you don't know adapt
foreach ($element in $array){
do-something
}
I hope that helps.
Andrew Watt MVP
Author Professional Windows PowerShell (Wrox)
On Mon, 15 Jan 2007 06:39:01 -0800, Kjetil Thorstensen
Bingo! Thanks a lot.
Just one small addition - I was at one point trying to do the same with an
array - declaring it up front and then incrementing the index each time I
entered the loop. That did not work. I could create an array, but I had to
fill it up front - I couldn't find a way to "append".
Am I trying something that doesn't work?
/Kjetil
Post by Andrew Watt [MVP]
Hi,
Have a look at the set-content and add-content cmdlets.
Andrew Watt MVP
On Mon, 15 Jan 2007 05:08:00 -0800, Kjetil Thorstensen
Hi.
I'm trying to write a script that parses logfiles, both IIS and other apps'
logs and extracts certain information from specific lines ... not all. The
extraction works fine using $var.substring and family, but I can't find out
how to write the info to a file.
I read the file into the variable (array) called $loglines and plan to
foreach ($i in $loglines) {
if ($loglines[$i].Contains("Process file") ) {
$Time=$loglines[$i].substring(0,14)
$Directory=$loglines[$i].substring( $loglines[$i].IndexOf("\"),
($loglines[$i].LastIndexOf("\")-$loglines[$i].IndexOf("\")))
$Filename=$loglines[14].substring($loglines[$i].LastIndexOf("\")+1,($loglines[$i].Length-$loglines[$i].LastIndexOf("\")-1))
# Here I need to get it to file, one line at a time
}
}
Ideas anyone?
Thanks in advance
/Kjetil
Loading...