Solved

Powershell Split String

Posted on 2013-06-09
4
770 Views
Last Modified: 2013-06-09
This code was working last year I think something changed with the Data string parse portion and its causing the whole thing to fail.. any sugustions

# ==============================================================================================
# Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 2011
# NAME: GetNFLSchedule.ps1
# DATE  : 11/13/2011
# ==============================================================================================

## - Download webpage to a PSObject variable
$source = "http://espn.go.com/nfl/schedule";
$wc = New-Object System.Net.WebClient;
[string] $content = $wc.DownloadString($Source);
[Array] $arrtxt = $content.Split("`n`r");
$x = 0; $arrtxt2 = $null; $rec = 0;





[Array] $NFLScheduleTable = foreach($i in $arrtxt)
{
	## - Get Game Week:
	if (($i -like '*<table class="tablehead"*') -and ($i -like "*</a>Week*")) 
	{
		#Write-Host "Found Week" -ForegroundColor Yellow;
		$getWeekNumber = ""; $WeekNumber = "";
		$s = $i.Substring($i.IndexOf('</a>Week'));
		$s2 = $s.Substring($s.IndexOf('>')+1);
		$y = $s2.Substring($s2.IndexOf('<span><a href'));
		$getWeekNumber = ($s2.Substring(0,($s2.length)-($y.length))).Split(" ");
		$WeekNumber = $getWeekNumber[1].ToString();	
		$getDateText = $true;
	}
	
	## - Get Game Date:
	if (($getDateText -eq $true) -and ($i -like '*<td width="240">*'))
	{
		$DateText = ""; 
		$s = $i.Substring($i.IndexOf('<td width="240">'));
		$s2 = $s.Substring($s.IndexOf('>')+1);
		$y = $s2.Substring($s2.IndexOf('</'));
		$DateText = ($s2.Substring(0,($s2.length)-($y.length)));
		$getDateText = $false;
	}	
	
	## - Get Teams:
	 if($i.ToString() -Match "<td><a href=""http://espn.go.com/nfl/team/_/name/")
	 {
		 $found = $true;
		 $s = $i.substring($i.Indexof("/name/"),$i.IndexOf("</a>")-$i.Indexof("/name/"));
		 $TeamA = $s.Substring($s.indexof(">")+1);
		  
		 $s = $i.substring($i.Indexof("</a> at <a"),$i.IndexOf("</a></td>")-$i.Indexof("</a> at <a"));
		 $AtTeamB = $s.Substring($s.indexof(""">")+2)
		
		## - Get Game Time:
		if($arrtxt[$rec+1].ToString() -Match " PM</td>") 
		 {
				$s = $arrtxt[$rec+1].Substring($arrtxt[$rec+1].IndexOf('<td>'));
				$s2 = $s.Substring($s.IndexOf('>')+1);
				$y = $s2.Substring($s2.IndexOf('</'));
				$TimeText = ($s2.Substring(0,($s2.length)-($y.length)));
		 }
	 };

	## - Build the Game Schedule PSobject:
	if($found)
	{ 
		$found = $false; $x++;		
		$GetNFLScheduleLine = @{
			seq				= ([int32] $x);
			Week			= ([int32] $WeekNumber);
			GameDate		= $DateText.ToString();
			GameTime		= $TimeText;
		    TeamA			= $TeamA.ToString();
		    AtTeamB			= $AtTeamB.ToString();	    
		}
		
		$NFLScheduleRecord = New-Object PSObject -Property $GetNFLScheduleLine;
		$NFLScheduleRecord;
	};
    

    
	$rec++;
}

## - Display on screen:
$NFLScheduleTable | sort seq | select seq, Week, GameDate, GameTime, TeamA, AtTeamB | ft -AutoSize

Open in new window

0
Comment
Question by:Leo Torres
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39233340
Bad way to parse a Web page ... It depends on table column widths, which is not reliable. Any change of the page will result in errors.
In this case, replace this part (lines 33 to 42):
	## - Get Game Date:
	if (($getDateText -eq $true) -and ($i -like '*<td width="170">*'))
	{
		$DateText = ""; 
		$s = $i.Substring($i.IndexOf('<td width="170">'));
		$s2 = $s.Substring($s.IndexOf('>')+1);
		$y = $s2.Substring($s2.IndexOf('</'));
		$DateText = ($s2.Substring(0,($s2.length)-($y.length)));
		$getDateText = $false;
	}	

Open in new window

0
 
LVL 8

Author Comment

by:Leo Torres
ID: 39233387
Great !! worked..
Any suggestion if this is not best method.. How might have you done different
0
 
LVL 8

Author Closing Comment

by:Leo Torres
ID: 39233388
Thanks!
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39233453
Without analyzing the site in depth, I would parse it as XML, or use GetElementByTagName and the like. This allows for selecting the third column in the second table and such things.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question