Powershell grab text position 148-182, each line.

WeTi
WeTi used Ask the Experts™
on
Dear expert.

I need help with this code:
$time = ((Get-Date).Adddays(-100))
$pathlog =  "\\server\log_*"
$Pattern = "^01","^03"
$results = Get-ChildItem $pathlog -Recurse | where-object {$_.LastWriteTime -ge $time} |
	Select-String -Pattern $Pattern | 
	Group-Object Path |
	% { #Foreach-object alias
		$_.Group | Select-Object Line
}
if ($results) {
$results | Out-File D:\wei_test\resulttest.txt
$text = Get-Content -Path D:\wei_test\resulttest.txt
$text.Substring(148, 182)
$results = 1
} 

Open in new window


Now I would like to grab the Out-File result text line position  148 to 182 in each lines and show it in the powershell window.
I got a error:
 
Exception calling "Substring" with "2" argument(s): "startIndex cannot be larger than length of string.
Parameter name: startIndex"
At line:37 char:1
+ $text.Substring(148, 182)

Kinda stuck with this. thx for the help.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
SubString expects int startIndex, int length, not int startIndex, int stopindex
You'll need SubString(148, 35)
Then if you want to export the lines to a file, you need to use -ExpandProperty in the Select-Object (and don't use aliases in scripts; use aliases only in interactive command lines).
$time = ((Get-Date).Adddays(-100))
$pathlog =  "\\server\log_*"
$Pattern = '^(01|03)'
$results = Get-ChildItem $pathlog -Recurse | Where-Object {$_.LastWriteTime -ge $time} |
	Select-String -Pattern $Pattern |
	Group-Object Path |
	Foreach-Object {
		$_.Group | Select-Object -ExpandProperty Line
	}
If ($results) {
	$results | Out-File D:\wei_test\resulttest.txt
	$text = Get-Content -Path D:\wei_test\resulttest.txt
	$text.Substring(148, 35)
	$results = 1
}

Open in new window

Author

Commented:
as great as always, and yes, i will not sue alias in coding. Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial