Showing posts with label script. Show all posts
Showing posts with label script. Show all posts

Sunday, January 27, 2013

Windows Media Services – Starting Publishing Points with VB Scripts

Its good to be back after two & a half months break. Probably when you become too active it’s a way to tell yourself to hold back and relax for a while. Well I spent my 2 months on bed rest not in a particularly good way.

But nothing hold you back from learning new things and this time it’s the Windows Media Services. I am using windows media services 9 on windows server 2003(yeah I know it was last used by Pharaohs Smile)

The Problem :

                             I am working on a live webcasting project where I need to configure 6 media servers across the country to push the live video feed from one server to other. The drawback is that if the 1st media server is down or encoder is down it starts a chain reaction and turn off all the media servers. The problem is compounded with the cumbersome process of restarting the multicast publishing points. We need to run the Multicast announcement wizard all over again for each publishing point and for each server.

The solution

                           I came up with a couple of VB scripts.

First script is the one responsible for starting all the publishing points on the server.

   1: set obj = createobject("WMSServer.server")
   2: for each pubpt in obj.PublishingPoints
   3: ' check for broadcast type
   4:   if pubpt.Type = 2 then
   5:      pubpt.Start
   6:   end if
   7: next
   8:  

Save the above script as start.vbs on C Drive of  WMS server. Running the script will start all the publishing points.


Now I need some mechanism to run this script (located on WMS server) from my local machine.


Below is the script which I save on the local machine as *.vbs and running this script will run the above script on each server.



   1: 'Turning on WMS1
   2: strComputer = "<IP of the WMS1>"
   3: strCommand = "cscript c:\start.vbs"
   4:  
   5: Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
   6: Set objProcess = objWMIService.Get("Win32_Process")
   7:  
   8: errReturn = objProcess.Create(strCommand, null, null, intProcessID)
   9:  
  10: If errReturn = 0 Then
  11: Wscript.Echo "All Publishing Point started at WMS1  with a process ID: " & intProcessID
  12: Else
  13: Wscript.Echo "Publishing Points at WMS1 could not be started due to error: " & errReturn
  14: End If
  15:  
  16: 'Turning on WMS2
  17: strComputer = "<IP of the WMS1>"
  18: strCommand = "cscript c:\start.vbs"
  19:  
  20: Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  21: Set objProcess = objWMIService.Get("Win32_Process")
  22:  
  23: errReturn = objProcess.Create(strCommand, null, null, intProcessID)
  24:  
  25: If errReturn = 0 Then
  26: Wscript.Echo "All Publishing Point started at WMS2  with a process ID: " & intProcessID
  27: Else
  28: Wscript.Echo "Publishing Points at WMS2 could not be started due to error: " & errReturn
  29: End If

Points to remember with this process-


1. Sequence of the servers on the second script should be considered based on the webcasting feed layout. It means WMS1 to be started before WMS2 so that WMS should have source of WMS1 publishing points.


2. User who is executing 2nd script should have admin rights on all the WMS servers.


-Cheers Smile

Wednesday, September 26, 2012

Implementing GTD on Outlook–Part 1–UPDATE

Link to original post

 

I have updated to code to add below items --

1. Start date – date of receiving email

2. Due date – start date + 3

3. Reminder to turn on

4. Reminder date – Due date

   1:  Option Compare Text
   2:   
   3:  Sub MakeWaitingForTaskWithAttachmentFromCurrentMessage(MyMail As Outlook.MailItem)
   4:      Dim strID As String
   5:      Dim olNS As Outlook.NameSpace
   6:      Dim olMail As Outlook.MailItem
   7:      Dim objTask As Outlook.TaskItem
   8:      Dim categories As String
   9:      Dim addRecipient As Boolean
  10:      Dim regex
  11:      Dim matches, customSubject, subject
  12:      
  13:      ' Configuration options
  14:      categories = "@WAITING FOR"
  15:      addRecipient = True
  16:      
  17:      strID = MyMail.EntryID
  18:      Set olNS = Application.GetNamespace("MAPI")
  19:      Set olMail = olNS.GetItemFromID(strID)
  20:      Set objTask = Application.CreateItem(olTaskItem)
  21:      objTask.Attachments.Add MyMail
  22:      Set regex = CreateObject("vbscript.regexp")
  23:      regex.Pattern = "/wf (.*)"
  24:      regex.IgnoreCase = True
  25:      regex.Global = True
  26:      Set matches = regex.Execute(olMail.Body)
  27:      If matches.Count <> 0 Then
  28:          customSubject = matches(0).submatches(0)
  29:      Else
  30:          customSubject = ""
  31:      End If
  32:      If customSubject <> "" Then
  33:          subject = customSubject
  34:      Else
  35:          subject = olMail.subject
  36:      End If
  37:      
  38:      With objTask
  39:          If addRecipient Then
  40:              .subject = olMail.Recipients.item(1) & ": " & subject
  41:          Else
  42:              .subject = subject
  43:          End If
  44:          .categories = categories
  45:          .Body = olMail.Body
  46:          .StartDate = olMail.ReceivedTime
  47:          .DueDate = olMail.ReceivedTime + 3
  48:          .ReminderSet = True
  49:          .ReminderTime = olMail.ReceivedTime + 3
  50:   
  51:      End With
  52:      objTask.Save
  53:       
  54:      Set objTask = Nothing
  55:      Set olMail = Nothing
  56:      Set olNS = Nothing
  57:  End Sub
  58:   
  59:  ' Wrapper that gets the current item and calls the previous function, to use as a macro
  60:  Sub MakeWaitingForTaskWithAttachmentFromCurrentMessageMacro()
  61:      Dim curMail As Outlook.MailItem
  62:      Set curMail = GetCurrentItem()
  63:      Call MakeWaitingForTaskWithAttachmentFromCurrentMessage(curMail)
  64:  End Sub
  65:   
  66:      
  67:   
  68:  Function GetCurrentItem() As Object
  69:      Dim objApp As Outlook.Application
  70:           
  71:      Set objApp = CreateObject("Outlook.Application")
  72:      On Error Resume Next
  73:      Select Case TypeName(objApp.ActiveWindow)
  74:          Case "Explorer"
  75:              Set GetCurrentItem = objApp.ActiveExplorer.Selection.item(1)
  76:          Case "Inspector"
  77:              Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
  78:          Case Else
  79:              ' anything else will result in an error, which is
  80:              ' why we have the error handler above
  81:      End Select
  82:       
  83:      Set objApp = Nothing
  84:  End Function



 


This code is for waiting for category.

Saturday, September 22, 2012

My ISP really sucks !

[Update] Attaching real files. Add your login id and password in login.vbs. Download


I have been using this service (hathway.com) for part 2 years and for last few months I was shocked to see that in the name of security my ISP has scheduled internet connection reset twice – thrice a day. To start the service again I need to login to the ISP web portal. For your information my plan so called “unlimited plan”.
In simple words if you download a lot, specially through torrents, your downloading will tend to go on entire night  or may be days – UNMONITORED. Since this resetting business started, my internet connection resets somewhere between 11:00 PM to 3:30 AM and there is no way I can keep monitoring the connection so that I can login as soon as it disconnects to ensure my downloading.
I had to do something about it. Programmer inside me woke up again. I pulled up my sleeves – got a cup of coffee, and went back to see for solution.
Quite easily an algorithm was designed-
1.  Loop
2. Keep monitoring the internet connection
3. If internet connection is working go to step 1 else step 3
3. as soon as internet connection breaks open internet explorer. Enter username and password.
4. Go to step 1
So first I need to write a script for the step 3. I used VB Script. Below is the code. What it does is open internet explorer then navigate to the ISP web portal and then try to figure out the element ID where I need to enter the username and password. Just save the script as (lets say) “login.vbs”
   1: DIM IE

   2: DIM ipf

   3:  

   4: Set IE = CreateObject("internetexplorer.Application")

   5: IE.navigate "http://203.212.193.59/bsp/startportal.do?CPURL=http://203.212.193.59/"

   6: IE.Visible = True

   7:  

   8: While IE.Busy

   9:      WScript.Sleep 50

  10: Wend

  11:  

  12: Set ipf1 = IE.document.getElementByID("username")

  13:  ipf1.Value = "my username" 'fill in the text box

  14: Set ipf2 = IE.document.getElementByID("password")

  15:  ipf2.Value = "My password" '
fill in the text box
  16: Set ipf3 = IE.document.all.Submit

  17:  ipf3.Click    'click the submit button

  18: '
IE.Quit


If you run this VB script while you are already logged in then you will get an error that element id not found.

                                                            image

Why ?

             Because you ISP portal will redirect the script to the welcome page where the element id is not present.

Hence if I schedule this script to run over and over then I will get the error message. To solve this I need to write a separate batch file which contains logic steps 1,2, &3 of algorithm.

Below is the batch file.



   1: :begin

   2: @ECHO OFF

   3: setlocal

   4:  

   5: SET UNKN="Unknown host"

   6: SET FAIL="Lost = 4"

   7: SET RECD="Received = 4"

   8:  

   9: FOR /F "tokens=1,2,3,4 delims=/ " %%I IN ('DATE /T') DO SET date1=%%J/%%K/%%L

  10: FOR /F "tokens=1,2 delims=: " %%I IN ('TIME /T') DO SET time1=%%I:%%J

  11:  

  12: ping google.com | FIND /I %UNKN% >NUL

  13: IF NOT ERRORLEVEL 1 ECHO [%date1% %time%]  unknown >>.\pingstat.log & GOTO :END

  14:  

  15: ping google.com | FIND /I %FAIL% >NUL

  16: IF NOT ERRORLEVEL 1 ECHO [%date1% %time%]  failure >>.\pingstat.log & cscript .\login.vbs & GOTO :END

  17:  

  18: ping google.com | FIND /I %RECD% >NUL

  19: IF NOT ERRORLEVEL 1 ECHO [%date1% %time%]  success >>pingstat.log & GOTO :END

  20:  

  21: endlocal

  22: :END



Lets save this file as login.bat

What it does is – It pings google.com. If it pings then end of file and if not then call the login.vbs file (saved in same folder). It also save a entry with “Success” or “Failure” in a text file with the name “Pingstat.txt” (I used it for testing)

Now all my steps are completed from the algorithm I devised. Only thing left is scheduling this script. All I need to do is schedule the batch file thorough windows task scheduler.

And YAY ! I am done.

Now my downloading goes uninterrupted and unmonitored.