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
Solved

Powershell Split String

Posted on 2013-06-09
4
765 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
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 …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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.

861 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