Saturday 25 February 2012

PowerShell script to launch Netflix in UK / US mode

I'm a Netflix subscriber in the UK and it was brought to my attention that Netflix can be launched in US mode by changing which DNS servers you point at. This then allows you to access different content based on the location. As I use my Media Center PC to display content I figured a nice desktop icon would be a good option.

This caused me to write the following PowerShell script to automate the whole process.
The script gets your existing DNS entries (multiple NICs work) and saves them to a file. It then changes the DNS entries of all IP enabled NICs to those specified in the script (I've used Sky DNS servers for the UK ones). It then launches IE and points to www.netflix.com in a Theater style window. At that point the script waits for IE to be closed then it reverts the DNS entries on the NICs back to their original settings.

I've tested this on two machines so far; one with a single external NIC and one with two external NICs. This now works for me.

I created the Netflix US shortcut on the desktop with the following line:

powershell.exe -command "& 'C:\PATH\netflix.ps1' US"

I created the Netflix UK shortcut on the desktop with the following line:

powershell.exe -command "& 'C:\PATH\netflix.ps1' UK"

Give it a try and let me know how you get on...
---------------------------------------------------------------------------------------------------------------------------
Param
([String]$DNS)
$DNSUK = "90.207.238.97","90.207.238.99"
$DNSUS = "208.122.23.22","208.122.23.23"
$Website = "www.netflix.com"
IF
($DNS -eq "UK")
{$SetDNS = $DNSUK}
ELSEIF
($DNS -eq "US")
{$SetDNS = $DNSUS}
ELSE
{"You need to specify UK or US. i.e. Netflix.ps1 UK"
EXIT
}
#Let's obtain your current DNS settings
$NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled=True"
$File = New-Item -type file "NIC Information.txt" -Force
foreach ($NIC in $NICs)
{
$NICName = $NIC.Description

$OriginalDNS = $NIC.DNSServerSearchOrder
$DHCPSetting = $NIC.DHCPEnabled
#Let's determine if you're a DHCP client
#---------------------------------------
if
($DHCPSetting -eq "True")
{"Your DNS servers are automatically assigned"
add-content $file "$NICName,$DHCPSetting,NULL"}
ELSE
{"Your DNS servers are manually set to: $OriginalDNS"
add-content $file "$NICName,$DHCPSetting,$OriginalDNS"}

#Set DNS Setting to location specified
#-------------------------------------
$NIC.SetDNSServerSearchOrder($SetDNS)
"For this session your DNS has been changed to $SetDNS"
#END of foreach
}
#Open Internet Explorer
#----------------------
$IE=New-Object -com internetexplorer.application
$IE.Navigate2($website)
$IE.visible=$true
$IE.TheaterMode=$true

#Monitor Internet Explorer process
#---------------------------------
"Waiting for Internet Explorer window to close"
(Get-Process -Name iexplore)| Where-Object {$_.MainWindowHandle -eq $ie.HWND}| Wait-Process
#Set DNS back to original setting
#--------------------------------
$list=Get-content ".\NIC Information.txt"
Foreach ($line in $list)
{
 $splitLine = $line.split(",")
 $Desc = $splitLine[0]
 $DHCPStatus = $splitLine[1]
 $DNSFromFile = $splitLine[2]

if
($DHCPStatus -eq "True")
{
"DHCP was enabled on adapter $Desc"
$NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled=True" | where{$_.Description -eq "$Desc"}
$NICs.SetDNSServerSearchOrder()
}
ELSE
{
"DHCP was not enabled on adapter $Desc"
$SetDNS=$DNSFromFile
$NICs.SetDNSServerSearchOrder($SetDNS)
}
#End of foreach
}
del ".\NIC Information.txt"

No comments:

Post a Comment