Showing posts with label windows7. Show all posts
Showing posts with label windows7. Show all posts

Sunday, October 25, 2009

Windows 7 Clean Install using Upgrade Media

Just a slightly different spin to the post by Paul Thurrott about performing a clean install of Windows 7 using “upgrade” media without having to install XP or Vista first.  This just wraps the steps inside a .BAT script.  Make sure you read Paul’s article first or you’ll be lost.  Be sure to run this from a CMD console that was launched via “Run as Administrator” or it won’t work…

@echo off
rem the following line between the divider lines should NOT wrap!
rem --------------------------------
REG ADD "HKLM/Software/Microsoft/Windows/CurrentVersion/Setup/OOBE/" /v MediaBootInstall /d 1 /t REG_DWORD /f
rem --------------------------------
echo Registry key value has been set.
pause
slmgr /rearm
echo Activation skip-rearm has been set.
echo Press ENTER to reboot your computer now...
pause
shutdown -r -f -t 2

Saturday, October 17, 2009

Using Twitter to Control Your Computer

I’ve blogged about this before, but not with a lot of detail.  So here goes.

In my previous post, I explained how to setup a “system” that has your computer sending a DM (Direct Message) Tweet to your Twitter account.  But in this scenario I’m turning it around, so that you will Tweet your computer and have it perform actions based on what you Tweet.

What you do with this is up to you.  Anything you can do with a script is fair game.  Anything.  The script will reside on your computer.  You will send a DM to a Twitter account, which your computer will monitor on a scheduled recurrence.  Your script will read and parse the DM container and look for specific phrases that it is set to recognize and will execute a task accordingly.  Simple enough?  Good.  I thought so.

Ingredients:

  1. A Twitter Account
  2. Another Twitter Account
  3. A List of Things You Intend to Ask Your Computer To Do
  4. A Script
  5. A Scheduled Task

A Twitter Account

This is YOUR Twitter account actually.  You will need this in order to send a DM (Direct Message) Tweet to your computer, which requires…

Another Twitter Account

This is the account your computer will monitor.  Remember to lock it down so only your “other” Twitter account can access it.

A List of Things for Your Computer To Do

You need to sit down and make a list of what specific tasks you want to be able to request of your computer.  Yes, you COULD make it so you Tweet any shell operation and have it blindly execute that, but trust me: THAT IS DANGEROUSLY STUPID.  In fact, so stupid, you should have your balls stomped with ice climbing boots in a rowdy Irish pub just for thinking it.  Need some pointers? Here’s a simple list to get you started:

  • Reboot or Shutdown a computer
  • Disable a User Account
  • Add/Remove A User to/from a Group
  • Start/Stop a Service
  • Start a Batch or Backup Job

For the first example, I’m going to simply have the computer tweet me back.  That way I know the plumbing is working and the toilets flush properly.  To do this, I’m going to make a script that reads my DM list and parses it for a specific phrase.  When that phrase is found, it will simply tweet me back (by DM) to let me know it got it.

A Script

The script I’m using for this example is written in VBscript, but any language/platform will work fine as long as it can invoke the Twitter API.  This script will access my Computer Twitter account, fetch the DM queue, iterate through it for messages containing a specific phrase of “TWEET_ME_BACK @userid” and then simply turn around and send a DM to the “@userid” that sent it (which, in this situation, is me).

'----------------------------------------------
' filename: twitter_monitor.vbs
' author: skatterbrainz
' http://scriptzilla.blogspot.com
' give props, don't rip me off please?
'----------------------------------------------
Const username = "Computer_TwitterName"
Const password = "Computer_TwitterPassword"
Const replyTo = "My_Twitter_Account"

Function Twitter_Get_Direct(strUser,strPass)
wscript.echo "querying for direct messages..."
Dim oXml, strTwitterURL : strTwitterURL = "http://twitter.com/direct_messages.xml"
Set oXml = CreateObject("MSXML2.ServerXMLHTTP.3.0")
oXml.Open "GET", strTwitterURL, False, strUser, strPass
oXml.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
oXml.Send()
Twitter_Get_Direct = oXml.responseText
Set oXml = Nothing
End Function

result = Twitter_Get_Direct(username, password)

'----------------------------------------------
' if direct messages were found, iterate them
'----------------------------------------------

If Trim(result) <> "" Then
wscript.echo "direct messages were found!"
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = "false"
xmlDoc.loadXML(result)
For each x in xmlDoc.documentElement.childNodes
msg = x.Text
If InStr(1, Ucase(msg), "TWEET ME BACK") <> 0 Then
id = Left(msg, 9)
wscript.echo id, ": ", Mid(Msg, 20)
wscript.echo "sending tweet-dm..."
TweetBack()
DeleteDM id
End If
Next
End If

'----------------------------------------------
' send tweet via direct-message
'----------------------------------------------

Sub TweetBack()
Dim retval, strMsg : strMsg = "I got your message!"
Dim oXml, strTwitterURL : strTwitterURL = "http://twitter.com/direct_messages/new.xml"
Set oXml = CreateObject("MSXML2.ServerXMLHTTP.3.0")
oXml.Open "POST", strTwitterURL, False, username, password
oXml.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
oXml.Send "text=" & strMsg & "&user=" & replyTo
retval = oXml.responseText
Set oXml = Nothing
End Sub

'----------------------------------------------
' delete a direct-message using the id number
'----------------------------------------------

Sub DeleteDM(id)
Dim retval
Dim oXml, strTwitterURL : strTwitterURL = "http://twitter.com/direct_messages/destroy/" & id & ".xml"
Set oXml = CreateObject("MSXML2.ServerXMLHTTP.3.0")
oXml.Open "POST", strTwitterURL, False, username, password
oXml.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
oXml.Send()
retval = oXml.responseText
Set oXml = Nothing
End Sub


Note that in the iteration section of code above I invoke the XMLDOM object to parse the direct_message result.  This is because it returns a single XML stream that contains all of the direct messages.



A Scheduled Task



Because your computer needs to read the Twitter account DM queue, it needs to “poll” for it using a recurrent task.  The easiest way to do this is by creating a scheduled task.  Each time the task is initiated, it will execute the script and go back to sleep until it’s scheduled to run again.  I’m using Windows 7 for this example, so here goes…




  1. Click on the Windows button (formerly “Start” button)


  2. Type in SCHED and as soon as “Task Scheduler” appears at the top, press Enter.


  3. Click on Task Scheduler Library in the left-hand panel


  4. Right-click on Task Scheduler Library and select “Create Task


  5. In the Name box, enter “Monitor Twitter Account”


  6. In the Description box, enter “Monitor Twitter account ___ for Direct Messages to parse” (fill-in your computer Twitter account name).


  7. Select “Run whether user is logged on or not”


  8. Click “Change User or Group” button, enter “SYSTEM” and click “Check Names”


  9. Once the name is underlined, click OK.  You should see “NT AUTHORITY\SYSTEM” in the user account name box. (see Fig. 1)


  10. Click the Triggers tab.


  11. Change the “Repeat task every:” to “15 Minutes”


  12. Check “Stop task if it runs longer than:” and set it to “14 Minutes” (see Fig. 2)


  13. Click OK, to return to the Create Task form.


  14. Click the Actions tab, click the New button


  15. Enter “cscript” for the program.  Enter “/nologo c:\scripts\monitor_twitter.vbs” in the arguments box (see Fig. 3, and 4)


  16. Click OK to save the new Task



Figure 1 Fig. 1



Figure 2 Fig. 2



Figure 3 Fig. 3



Figure 4 Fig. 4

Making Your Computer Tweet You When Bad Things Happen

Twitter has a decent, if not minimalist API with which you can tap into with pretty much anything that can execute a process.  Scripting is a natural “fit” for this, but so is mainstream/classic-style/meat-and-potatos “programming”.  Pick your language, it doesn’t really matter.  After all, anyone that says you *have* to use a particular language for general tasks is a complete idiot.  (pssst: it’s the same as saying all you need is a hammer, it works on everything, including hose clamps).

I fell off the trail a bit, I’ll get back on.  Where was I?  Oh yeah…

So, let’s JUST SAY you have a Twitter account and a Windows computer (XP or later).  And maybe you’d like to find a way to leverage that (sorry, I know “leverage” was voted one of the top 100 most overused and annoying words for 2009, at least it didn’t make the top 10), but the path from A to Q (forget Z) is a bit fuzzy?  Hmmm?  Fear not.

Ingredients

  1. A Twitter account
  2. A Script
  3. An Internet connection
  4. A Scheduled Task
  5. Something to trigger this contraption to work

A Twitter Account

You probably don’t want your computer tweeting you, but instead should have it DM (that’s Direct Message) you.  That way your computer’s messages aren’t laid bare to the public, but are instead restricted to just YOU.  You might think you can just use your own Twitter account to DM yourself, but you can’t.  Twitter doesn’t allow that.  So you need to create another Twitter account just for your computer to use. Or do you?  Actually, from the web or most Twitter clients (TweetDeck, UberTwitter, etc.) that is true.  However, with just the API and a script, you can DM yourself.  Weird, but true (for how long is anyone’s guess).

A Script

The pieces for this part are posted over on my other blog here and another example of reading DM folders here.  But for the sake of sparing your poor overworked fingers from more torturous mouse clicks, I’ll drop it below.  For this example, I’m using a VBScript file.  You can use PowerShell, KiXtart, BAT or whatever the **** you prefer, I really don’t give a ****.

Const username  = "Computer_TwitterName"
Const password = "Computer_TwitterPassword"
Const recipient = "My_Twitter_Name"

Function Twitter_Send_Direct(strMsg, recipient)
Dim oXml, strTwitterURL
strTwitterURL = "http://twitter.com/direct_messages/new.xml"
Set oXml = CreateObject("MSXML2.ServerXMLHTTP.3.0")
oXml.Open "POST", strTwitterURL, False, username, password
oXml.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
oXml.Send "text=" & strMsg & "&user=" & recipient
Twitter_Send_Direct = oXml.responseText
Set oXml = Nothing
End Function

msg = "Warning Event 129 NTP Synch Discovery Failure (computername)"
result = Twitter_Send_Direct(msg, recipient)
wscript.echo result


Save the script as “twitter_event_129.vbs” in a folder called C:\scripts.



An Internet Connection



Not to be a smartass or anything, but if you’re reading this, there’s a pretty good chance you already have this.  If not, well then…



A Scheduled Task



I’m going to use Windows Vista and Windows 7 for this exercise.  Why? Because I absolutely hate XP.  I hate it more than OSX or Linux.  But, I absolutely LOVE my Windows 7 and Windows Server 2008.  And one reason why is that this makes it MUCH easier to combine steps 4 and 5, and overall it just looks better on the eyes.  I’ll get back to this in a minute.



Something to Trigger this Contraption to Work



For this exercise, I’ll make this engine fire up whenever the Windows Event system logs an “Warning” Event of type 129 (that’s a NTP time synch discovery problem).  If you can’t find any such events, pick whatever suits you.  Just replace the ID number with the appropriate number below.



Since I’m using Windows 7, I’m going to combine steps 4 and 5 as follows:




  1. Open the Event Viewer console


  2. Navigate to Windows Logs / System


  3. In the Actions panel, click “Filter Current Log…”


  4. Check “Warnings”


  5. In the box with “<All Event IDs>” type in “129” and click OK


  6. You should see just those events now.


  7. Right-click on one of the events, and click “Attach Task to this Event…”


  8. The default title will be highlighted.  You can change it if you want or leave it as-is, but I would suggest you enter something in the Description box to explain what this “task” is for and what it does.  For my example, I’m going to replace the default title “System_Microsoft-Windows-Time-Service_129” with “Send Twitter DM for Event 129 NTP Warnings” and in the Description box enter “Call a script to send a Twitter DM when event ID 129 (warning) for NTP synch discovery is logged” and click Next >


  9. Click Next > on the “When an Event is Logged” panel.


  10. Select “Start a Program” and click Next >


  11. Click the Browse… button to locate my .VBS script


  12. Click Next >


  13. Click Finish



You will see a confirmation message appear…



image (yes, you can just click OK)



But, we’re not done yet…




  1. Open the Windows Task Manager


  2. Under Task Scheduler Library, click Event Viewer Tasks


  3. Right-click on the task you just created and select “Properties”


  4. Click the “General tab, click the “Change User or Group…” button


  5. Type in “SYSTEM” and click “Check Names” and the name should become underlined (validated).  Click OK.  The user account box should now display “NT AUTHORITY\SYSTEM”.


  6. Just below that, change the option to “Run whether a user is logged on or not”


  7. Click the “Actions” tab.


  8. Click the “Edit” button


  9. Change the Program/Script box to “cscript


  10. In the “Add arguments” box, enter /nologo C:\scripts\twitter_event_129.vbs and click OK


  11. Click the “Settings” tab.


  12. Change the “Stop the task if it runs longer than:” value to 1 hour.


  13. Click OK to save the changes



Right-click on the Task and click “Run” to force it to run.  Go to your Twitter client to verify you received a DM.  If not, go back and verify your settings and the account and password settings within your script.



Cheers!