Archive for the ‘Powershell’ Category

Download XML files with Powershell script

November 4, 2010

Let’s say we have a scenario where there is a need to download a huge amount of XML files from a central web server for whatever reason. This is an example but I had a situation very similar like this. That’s why I have created a PowerShell script which is able to do the job for me.

The script is created with a function  called Get-WebPageXMLS

function Get-WebPageXMLS($url, $folder)

 

{

Import-Module BitsTransfer

if (-not (Test-Path $folder)) { md $folder }

$ie = New-Object -COMObject InternetExplorer.Application

$ie.Navigate($url)

while ($ie.Busy) { Start-Sleep -Milliseconds 100 }

$xmls = $ie.document.getElementsByTagName(‘a’) | Select-Object -ExpandProperty href |

where { $_ -match “`.xml$” }

$ie.Quit()

$xmls_n = $xmls.Length

if ($xmls_n -gt 0)

 {

$i = 1

$webClient = new-object System.Net.WebClient # .NET class

$proxy = new-object System.Net.WebProxy “Ipaddress:port” # specify your proxy address and port

# replace your credential by your domain, username, and pasword in cause there is authentication

#$proxy.Credentials = New-Object System.Net.NetworkCredential (“Domain\UserName”, “Password”)

$webClient.proxy=$proxy

# specify a header if you want to check info in your logs on your proxy

$webClient.Headers.Add(“user-agent”, “Windows Powershell WebClient”)

#$b=Get-Credential #here you can uncomment this but comment the following

 

 

$webClient.credentials = new-object system.net.networkcredential(domain\username,”password”)

foreach ($lnk in $xmls)

{ 

 

$dst = “$folder\$($lnk.Split(‘/’)[-1])” 

$dn = $lnk.Split(‘/’)[1]

echo “Getting [$i/$xmls_n] $dn” 

  

#$webClient.Credentials = $b #uncomment if you use get-cridential

$webClient.DownloadFile($lnk, $dst)

#Start-BitsTransfer $lnk $dst -Prio High -Display $dn #uncomment if you wish to use bits transfer

$i++ 

}

} else { “Nothing to do – NO xml links found in $url” }

}

cls

Get-WebPageXMLS http://Source                “c:\temp\destination”

 
 

 

 

With $b=Get-Cridential, if the site from where we download the files requires authentication, we can request user input for username and password. This is commented because we can put username and password directly in the script itself.

You can download the sccript here: 

http://techlevel.web.officelive.com/documents.aspx

   

 

 

 

 

 

 

 

  

 

 

Powershell script monitors particular service , tries to restart it and send email if it fails

August 27, 2010

This is a simple script which monitores w32time service. It can monitor avery service but i choose n that particular cause w32time. What is the genral idea:

1.We get an object service in variable $st. $comp is tha variable takes the hostname of the current computer.

2. If the service isn’t starter when the script runs it tries to start it three times implemented with help of a loop.

while (($st.Status -eq “Stopped”) -and ($i -lt 3)). You can get various methods and Properties from the object in variable $st with $st | get-member. One Property is the Status of the service $st.Status.

The Retries of the script is equalt to 3. $i -lt 3. It can be more then three. If for example $i -lt 8 the script will try 8 times to start the service before it fails.(if the service fails to start 8 times).

At last if the service is still in stopped state the script sends an email to notifyyou.

$st=Get-Service w32time
$i=0
$comp=gc env:computername
while (($st.Status -eq “Stopped”) -and ($i -lt 3))
{
Start-Service w32time
$st=Get-Service w32time
$i+=1

}

if($st.Status -eq “Stopped”)
{
$sfrom=”from@domain.com
$sto=”to@domain.com
$smtpclient=New-Object system.Net.Mail.SmtpClient
$mailmessage=New-Object system.Net.Mail.MailMessage
$smtpclient.HOST=”FQDN-SMTP-SERVER”
$mailmessage.from=$sfrom
$mailmessage.To.add($sto)
$mailmessage.Subject=”Please Check the service “+$st.Name+” on machine name “+$comp+” !!!!!!!!!!!”
$mailmessage.Headers.Add(“message-id”, “<3BD50098E401463AA228377848493927-1>”)
$smtpclient.Send($mailmessage)
}

So you can save the file with ps1 extension. For Example: str-service.ps1. After that you can create bat file with the following line Powershell -command “& {C:\Scripts\str-service.ps1 }”. The bat file can be add in Shedule tasks after that with appropriate schedule.