Showing posts with label if. Show all posts
Showing posts with label if. Show all posts

Tuesday, June 10, 2014

PowerShell Script : WAN Monitoring Script with Email Alert

 

Wan Monitoring

 

If you are using two WAN connections in your office or in organization, and if you are not monitoring them, it’s a considerable problem.

In my office, I have two internet connections. One is configured as primary connection and the other one is configured as backup internet line.

Once a not so long time ago, on a very  bad day my primary WAN gets down and the firewall router switched to the backup internet line.

I was not monitoring my Internet Line. {bad, very bad habit }.

I was not ware of this.

Internet was working but little bit slow. I was not looking in to it first because with ISP these kind of glitches does happens sometimes.

But in the evening, when things get too slow, I check the static ip of my internet connection and I came to know, router is switched to backup line.

ARRRRRRRRRRRR !!! Irritating.

To solve this issue for me, I wrote a simple PowerShell script.

I scheduled this script to run after every one hour.  It's check the  Static IP  of the current internet line, and if static IP doesn't match with Primary WAN IP, it sent an email alert.

What this script does?

1.    You have to set your primary WAN IP in to the script

2.    Script check your current Static IP

3.    If the Static IP doesn’t match with the WAN1,

4.    It send you an email.

 

Simple and elegant script.

 

You can download the script from TechNet galleryhttp://gallery.technet.microsoft.com/WAN-using-PowerShell-and-bc1bd828 

 

Here is code for the script, and all code is easy to understand.

  

#==================| Satnaam Waheguru Ji |===============================   
#
# Author : Aman Dhally
# E-Mail : amandhally@gmail.com
# website : www.amandhally.net
# twitter : @AmanDhally
# blog : http://newdelhipowershellusergroup.blogspot.in/
# facebook: http://www.facebook.com/groups/254997707860848/
# Linkedin: http://www.linkedin.com/profile/view?id=23651495
#
# Creation Date : 9- June - 2014
# File :
# Purpose : Check the static ip and compare it with WAN 1
# Version : 5
#
#
# My Pet Spider : /^(o.o)^\
#
#========================================================================

#Note :- This script function is PowerShell V2 compatible.
# We can add Advnace Function to it, But let's keep this things simple :) .


function Check-WanStatus {


begin {
try {

# SMTP Variables
$smtp = 'YOUR_SMTP_SERVER'
$to = 'amandhally@gmail.com'
$from = 'POSH@YourDomain.com'
$subject = 'WAN-1 is Down'



# Wan Variables

#WAN1 - Primary IP Address
$wan1 = '182.73.114.40'
#WAN2 - IP Address
$wan2 = '122.154.3.97'
#Getting Static IP from URL
$url = "http://checkip.dyndns.com"
$webclient = New-Object System.Net.WebClient
}
catch {
}
}
process {
try {

$Ip = $webclient.DownloadString($url)
$Ip2 = $Ip.ToString()
$staticIP = $Ip2.Split(" ")[5].Replace("</body>","").Replace("</html>","")

if ( $staticIP -notmatch $wan1 )
{
Write-Output "WAN-1 {$wan1} is Down. The Current Static IP is : $wan2 ."

$body = "WAN-1 {$wan1} is Down."
$body += "`n"
$body += "`n"
$body += "The Current Static IP is : $staticIP"

Send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -Priority 'High' -UseSsl


}

else
{
Write-Output "All is Well"
}
}
catch {
}
}
end {
try {
}
catch {
}
}
}

Check-WanStatus


Below is the screenshot of the warning email.

 

09-06-2014 12-40-00

You can download the script from TechNet galleryhttp://gallery.technet.microsoft.com/WAN-using-PowerShell-and-bc1bd828 


Regards


Aman Dhally.


 



Come and join my journey of : “100 Days of Self Improvement” on


 


Facebook: https://www.facebook.com/100DoSI 


If you like, you can follow me on Twitter and Facebook. You can also check my “You Tube channel for PowerShell video tutorials. You can download all of my scripts from “Microsoft TechNet Gallery”.

Friday, August 31, 2012

Setting Microsoft Office 2012 Trust Center's Trusted Location Paths using Powershell.

Hi Everyone.

If you are working in a Telecom Industry, and if your Analysts and Consultants are using in house built templates then i am sure that you are 98% facing the “Trust Centre warning”.

We are using custom in house built Office Templates for Word,Excel, and PowerPoint.  and the problem was whenever we open our in house templates is Office 2012 we were facing the below error.

We can click on it but it is a quite annoying.

31-08-2012 14-47-12

To resolved this error, the solution is to add our custom built templates directory to the  trusted location in trust center manually on every laptop and desktop.

31-08-2012 15-14-10

Did i mentioned MANUALLY???

Naaaaaa !!!!!!! I thought that we can script this.

And to resolve this is wrote a simple script. you can download the script from this link :
http://gallery.technet.microsoft.com/scriptcenter/Setting-office-2010-31d6a5f4

This script is very simple.,

You only need to provide the path of your templates or the location which you want to add as trusted to  $trustlocation variable.
$trustlocation = 'C:\Mylocation\Templates'
and that’s all , when you run the script , the script will create a location registry  named as “Location99” in to the registry.
and the Registry Keys will be look like this .
31-08-2012 15-33-48
and i have added comments in the script for easy understanding,
Functionality.
  1. First script test the path of the “Location99” registry key
  2. and if the Test path fails that will create a New Registry  Folder “Location99” and the the following registry keys
      • AllowSubfolders this is DWORD registry key and the value is 1 to allow subfolders as a trusted location too.
      • Date  = Date of creation
      • Description = Description for location etc.
      • Path  =  Path to the folder
  3. and if folder found it just write a confirmation message in the console that folder do exists.
  4. The script will do the same process for WORD,EXCEL and Powerpoint.
After running the script the location is added to the Word.
31-08-2012 15-15-24

Thanks
Aman Dhally
join aman on facebook Join aman on Linkedin follow aman on Twitter

Tuesday, August 28, 2012

Birthdays and Bill payments reminder script based on Powershell.

Hi,

My memory is very weak , and sometime i do forget the birthdays of my friends. and sometime even if forget to pay my mobile bills, I do have set up reminders on my mobile and outlook but still sometime i missed the alerts.

On weekends i hardly check my mobile until it is ringing and on Laptop at home i wont use outlook. Due to my laziness i mainly forget these things. But one thing i do most of the days are open my powershell console daily. the profile on my office laptop and home laptop are synced and script between both laptops are synced daily.

So i thought why not to create a simple script which open daily with my powershell console and show me if i need to pay any bill payment today or to wish  birthday to any of my friend.

I liked the thought. the only problem was to manually add everyone’s birthday in to the script, but somehow i able to manage it.

Download script form here http://gallery.technet.microsoft.com/scriptcenter/BirthdaysTasksBill-cfb1d958

Script is very simple and i used SWITCH statement in it.  There are two parts in the script, first is Payment part and second the the Birthday part.

Payment Part:
We have to make payment every month, so in payment part script only check for date and if date matched as per your criteria it shows what you have defined.
Birthday Parts:
Birthdays are specified by months and Date, the birthday part script check for month and the it switch to date.
How to use it.
Add your friends birthday manually and adding birthday and reminders are one time task.
28-08-2012 19-53-47
When you run the script, it checks for Month, and if it matched the current month then it switch again to Day of the Month, in below example if the Month is 1 means January then it switch to day of the Month, and if the day of Month matches toy 27 then that will shows the predefined alert in the console.
28-08-2012 19-50-11
Screenshots.
09-09-2012 19-01-11
24-08-2012 19-00-3720-09-2012 19-02-29
28-08-2012 19-31-57
The best way to use this script to add it to your Powershell $profile.So that whenever you open your Powershell Console you can have alerts.
20-09-2012 19-03-07

Download script form here http://gallery.technet.microsoft.com/scriptcenter/BirthdaysTasksBill-cfb1d958


Thanks
Aman Dhally
join aman on facebook Join aman on Linkedin follow aman on Twitter

Monday, February 13, 2012

Automate "Remote Server Administration Tools (RSAT)" Deployment using PowerShell.


HI,

These days I am working on a script which does few “Active directory” task for us. This was seems simple so for but then I have found a major hurdle comes.

Problem:

The script on which I am working will be run on newly formatted system which is having Windows7 installed. The problem was that this script will is using few “Active Directory” cmdlets. And the new installation of windows doesn’t have “RSAT” installed by default. So until or unless I don’t have “ActiveDirectory” module installed till then I can’t use my
 get-ADComputer ; get-ADComputerServiceAccount” cmdlets.

Then I thought there should be way to sort this out. So finally after spending whole day I found a way to do this within my PowerShell Script.

Solution:

The “Active Directory” module only available after installing the “Windows6.1-KB958830-x86-RefreshPkg.msu” package and then enable the “Active Directory Module for windows Powershell” in optional features.

The “Windows6.1-KB958830-x86-RefreshPkg.msu” is a “Microsoft update Standalone Package” and we can install these packages using wusa.exe {Windows Update Standalone installer}.


So after installation of the “Windows6.1-KB958830-x86-RefreshPkg.msu” we need to enable the “Active Directory Module for windows Powershell” in optional features for this we are using dism.exe { Deployment Image Servicing and Management tool}.

The Script:

·         I am placing “Windows6.1-KB958830-x86-RefreshPkg.msu” in a central location so that script the executable from it.
·         Normally I copied all the scripts in “c:\tools” folder and if that folder doesn’t exists it create it for us and write a message on screen that “C:\tools” is created.
o    $tool = test-Path C:\tools
if ($tool -eq $false ) {new-Item -Name "Tools" -Path c:\ -ItemType Directory; write-Host " folder "C:\Tools" created" -ForegroundColor Green }
·         Now it check if “Windows6.1-KB958830-x86-RefreshPkg.msu” file in exists in “C:\tools” folder and if it doesn’t exists it will copy the file from our central share folder  \\soft-server\SoftRes\$hotfix
o    hotfix = "Windows6.1-KB958830-x86-RefreshPkg.msu"
if ($testpath -eq $false) { Copy-Item \\dc3-del\PublicShare\$hotfix c:\tools\
Write-host "Files has Copied Sucessfully, Now I am going to  install the HOTFIX." -ForegroundColor RED
}
·         After copy the file from server it will install the files using wusa.exe
o    & wusa.exe "c:\tools\$hotfix" /quiet | out-null
·         After the installation finishes it will enable the “Active Directory PowerShell Module” feature in “windows Optional feature” using DISM
o    & dism.exe /Online /Enable-Feature /FeatureName:RemoteServerAdministrationTools  /FeatureName:RemoteServerAdministrationTools-Roles /FeatureName:RemoteServerAdministrationTools-Roles-AD  /FeatureName:RemoteServerAdministrationTools-Roles-AD-Powershell | Out-Null


:::::::::::::::::::::::::::::::;; Start of the script ############
$hotfix = "Windows6.1-KB958830-x86-RefreshPkg.msu"
$testpath =  test-path -path "c:\tools\$hotfix"
$tool = test-Path C:\tools
if ($tool -eq $false ) {new-Item -Name "Tools" -Path c:\ -ItemType Directory; write-Host " folder "C:\Tools" created" -ForegroundColor Green } 
if ($testpath -eq $false) { Copy-Item \\soft-server\SoftRes\$hotfix
c:\tools\
Write-host "Files has Copied Sucessfully, Now I am going to  install the HOTFIX." -ForegroundColor RED
}
Write-Host "Installing Remote Server Administration Tools...... " -ForegroundColor Yellow                  
& wusa.exe "c:\tools\$hotfix" /quiet | out-null
Write-Host "HotFix Installed, lets enable the Active Directory Features in "Optional Features"" -ForegroundColor "Green"
& dism.exe /Online /Enable-Feature /FeatureName:RemoteServerAdministrationTools  /FeatureName:RemoteServerAdministrationTools-Roles /FeatureName:RemoteServerAdministrationTools-Roles-AD  /FeatureName:RemoteServerAdministrationTools-Roles-AD-Powershell | Out-Null
Write-Host "Lets Import the Active directory PowerShell Module Now.." -BackgroundColor Green  -ForegroundColor Blue
Import-Module ActiveDirectory
write-Host "Module successfully Imported" -ForegroundColor Red -BackgroundColor White

########## End of the Script ############## 



Screenshots:
13-02-2012 20-41-20 
Note: I tested this script on Windows 7 machine and it works perfectly for me . you can download installation file of "Windows6.1-KB958830-x86-RefreshPkg.msu" from here http://www.microsoft.com/download/en/details.aspx?id=7887. Also this is a simple static script no error checking is enable in it.
 
Thanks for viewing
Aman Dhally
messenger_freak_adp1 (30)