Spit out FY H-1B Cap Count from USCIS – PowerShell

###########################################################################
#
# NAME: USCIS_H-1B
#
# AUTHOR:  Siva Mulpuru
#
# COMMENT: This script will spit out FY H-1B Cap Count by parsing through 
#           HTML Body
#
# VERSION HISTORY:
# 1.0 4/27/2011 - Initial release
#
###########################################################################


cls
$ie= new-object -com InternetExplorer.Application
$ie.Navigate('http://www.uscis.gov/h-1b_count')
sleep 1 #Time Allowed to fetch the webpage

if(!(Test-Path c:\tmp))
{
    New-Item -type directory -path c:\tmp
}
if(Test-Path c:\tmp\USCISBuffer)
{
    Remove-Item c:\tmp\USCISBuffer
}
#Save the body content to a file
$ie.Document.Body.InnerText > c:\tmp\USCISBuffer 
$ie.Stop()
#Quite IE COM Obj, or else the IE process will still be running after the script exits
$ie.Quit()
#Grab the two lines we are intereted in
$result = Get-Content c:\tmp\USCISBuffer | Select-String "h-1b Regular", "h-1b Master" 
#Make sure we got the two lines of text we need
if($result.Length -eq 2){
$Regular =  $result[0] #H-1B Regular Cap 65,000 8,0004/22/2011
$Masters =  $result[1] #H-1B Master’s Exemption 20,0005,9004/22/2011
}
else
{
Write-Output "Regular and Masters strings not found. Exiting..."
exit
}
#Regular Expression to sperate in to groups <TOTAL Cap> <Filled Cap> <Published Date>
#Regular Expression to sperate in to groups <65,000> <8,000> <4/22/2010>
$regex = "(\d{1,2}[,]?\d{3}\s?)(\d{1,2}[,]?\d{3}\s?)(\d{1,2}/\d{1,2}/\d{2,4})"

function printResult([int] $TotalQuota, [int]$FiledQuota,[DateTime]$PublishedDate)
{
    #Output Format
    Write-Output ("Out of {0:N0}; {1:N0} has been filled on {2:d}" -f $TotalQuota,$FiledQuota,$PublishedDate)
}

if($Regular -match $regex)
{
$TotalQuota = [int] $Matches[1]
$FiledQuota = [int] $Matches[2]
$PublishedDate = [DateTime] $Matches[3]
printResult $TotalQuota $FiledQuota $PublishedDate
}
if($Masters -match $regex)
{
$TotalQuota = [int] $Matches[1]
$FiledQuota = [int] $Matches[2]
$PublishedDate = [DateTime] $Matches[3]
printResult $TotalQuota $FiledQuota $PublishedDate
}


Download Script

USCIS_H-1B.ps1

Sample Output

Out of 65,000; 8,000 has been filled on 4/22/2011
Out of 20,000; 5,900 has been filled on 4/22/2011

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s