Solved

Powershell Split String

Posted on 2013-06-09
4
768 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
  • 2
  • 2
4 Comments
 
LVL 69

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 69

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
A brief introduction to what I consider to be the best editor for PowerShell.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

762 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