Archive

Archive for the ‘Scripts’ Category

Use Excel to create easy batch jobs

August 25th, 2009 No comments

Do you have to run a command on several servers remotely?  You can enter each command separately, but every good admin should know how to make life easier for him/her self.  For example, I wanted to use the psloggedon.exe tool to find out who was logged in to all the servers I manage (about 150).  Naturally, I could log into each server, check the Terminal Server Manager, and go on the next, but who wants to do that? 

The PSTool – psloggedon.exe is a nice little utility that I can run from my command prompt, and have it query a remote computer, returning who is logged on to that server.  So, starting in Excel (I already had a list of all the servers), I pasted the list of servers in the C column.

Next, in Column A, I entered "psloggedon.exe” – the name of the executable I want to run.  in Column B, I put a “ \\” – note the space in front of the \\.  In column E, I entered my first switch, “ –l” – and in column F, a second switch “ –x” again, note the spaces before the switches.

 image image

Now, to bring the whole thing together.  I LOVE the command Concatenate. 

image

In column G, I entered =concatenate(A1,B1,C1,D1,E1)

image

This will combine all the columns together (now you know why the spaces were above) for your final command.

image

You can now select column G, and copy and paste it into notepad, and save it as a .bat file.  You now have an easily made bat file, using Excel.

Share and Enjoy:
  • email
  • Facebook
  • Twitter
  • Digg
  • StumbleUpon
  • LinkedIn
  • Print

Categories: How To, Microsoft, Scripts, Tip

Export Outlook Contact Pictures

August 21st, 2009 2 comments

I recently made a mistake, and deleted the folder where I kept all the original headshots of all my contacts.  Don’t ask me why I keep them, I just do… I know I have them in Outlook, but I like to have the originals also.  Anyway, I wanted a way to export the picture from Outlook, back into the file system.  Well, I ran across this script that will do just that.  I could not get it to run correctly running Vista or Windows 7, running Office 2007, or Office 2010, but was able to get it to run on XP with Office 2003.  Guess the MAPI calls are different or something.  If anyone knows how to make this work on Windows Vista/7, using Office 2007/2010 let me know!~

Here’s the script:

Public Const CdoDefaultFolderContacts = 5
snServername = wscript.arguments(0)
mbMailboxName = wscript.arguments(1)
set csCDOSession = CreateObject("MAPI.Session")
pfProfile = snServername & vbLf & mbMailboxName
csCDOSession.Logon "","",False,True,0,True, pfProfile
set cfContactsFolder = csCDOSession.getdefaultfolder(CdoDefaultFolderContacts)
set cfContactscol = cfContactsFolder.messages
set ofConFilter = cfContactscol.Filter
Set cfContFltFld1 = ofConFilter.Fields.Add("0x8015",vbBoolean,true,"0420060000000000C000000000000046")
For Each ctContact In cfContactscol
	Set collAttachments = ctContact.Attachments
	For Each atAttachment In collAttachments
		If atAttachment.name = "ContactPicture.jpg" Then
			fname = replace(replace(replace(replace(replace((ctContact.subject & "-" & atAttachment.name),":","-"),"\",""),"/",""),"?",""),chr(34),"")
			fname = replace(replace(replace(replace(replace(replace(fname,"<",""),">",""),chr(11),""),"*",""),"|",""),"(","")
			fname = replace(replace(replace(fname,")",""),chr(12),""),chr(15),"")
			atAttachment.WriteToFile("c:\contactpictures\" & fname)
			wscript.echo "Exported Picture to : " &  fname
		End if
	next
Next

Save the above code as a .vbs file, and run it from the command line:

exportOutlookPic.vbs mailboxservername exchangeAlias

Make sure that you have created the folder referenced in the script, or you will get an error (c:\contactpictures\).  Works like a charm!  I know have all my Outlook Contact pictures, in my folder system. :)

Share and Enjoy:
  • email
  • Facebook
  • Twitter
  • Digg
  • StumbleUpon
  • LinkedIn
  • Print

Save Password in .RDP File

January 23rd, 2009 5 comments

I shared with you how to create multiple .rdp files with Powershell, and now, I want to show  you how to make your job even easier!  I  ran across this blog, and downloaded his RDP password Hash program.  It got me thinking, I could make my job easier by saving the password in a hashed format in each RDP file that my Powershell script creates!

** Note:  Please see my original post about this if you have questions of the files needed, and folder structure.

First, I modified the .CSV file, adding a new column “PASS” – in that column, add the hashed password created from remkoweijnen.nl’s password hash program [DOWNLOAD]

Then I modified my Powershell script, adding in the password to script, so it’s added to the .RDP file.

No more typing in passwords to connect to servers.  Also, the nice thing is, if you have to change the password, just modify the hash file in the .CSV file, and re-run the Powershell script!

Here is the modified Powershell Script:

$List = Import-CSV RDPLIST.CSV
$resolutions = ("Console","Cullscreen","Widescreen","Docked")
ForEach($Entry in $List) {
  ForEach($resolution in $resolutions) {
    # Prepend the destination directory info for RDP files
    $Dir = "..\" + $resolution + "\" + $Entry.Directory

    # Create new folder
    New-Item -Path $Dir -ItemType Directory -Force

    # Build the file name
    $FileName = $Dir + "\" + $Entry.ServerName + ".RDP"

    # Remove the old file
    Remove-Item $FileName -Force

    # Begin building RDP file
    $temp = "`nfull address:s:" + $Entry.IP 

    switch ($resolution) {

      "Console" {
        $temp += "`nscreen mode id:i:1"
        $temp += "`ndesktopwidth:i:1024"
        $temp += "`ndesktopheight:i:768"
        $temp += "`nusername:s:" + $Entry.USER
        $temp += "`npassword 51:b:" + $Entry.PASS
        $temp += "`nadministrative session:i:1"
      }

      "Fullscreen" {
        $temp += "`nscreen mode id:i:2"
        $temp += "`nusername:s:" + $Entry.USER
        $temp += "`npassword 51:b:" + $Entry.PASS
      }

      "Widescreen" {
        $temp += "`nscreen mode id:i:1"
        $temp += "`ndesktopwidth:i:1152"
        $temp += "`ndesktopheight:i:720"
        $temp += "`nusername:s:" + $Entry.USER
        $temp += "`npassword 51:b:" + $Entry.PASS
      }

      "Docked" {
        $temp += "`nscreen mode id:i:1"
        $temp += "`ndesktopwidth:i:1152"
        $temp += "`ndesktopheight:i:864"
        $temp += "`nusername:s:" + $Entry.USER
        $temp += "`npassword 51:b:" + $Entry.PASS
      }
    }
    $temp | out-file $FileName
    write-host $temp
    get-content template_bottom.txt >> $FileName
  }
}
Share and Enjoy:
  • email
  • Facebook
  • Twitter
  • Digg
  • StumbleUpon
  • LinkedIn
  • Print
Categories: Microsoft, PowerShell, Scripts

Create multiple RDP files with Powershell

September 13th, 2008 9 comments

image

RDP – Remote Desktop Connection, or what we called back in the day, Terminal Services.  Every system administrator uses it daily.  At work, the team I work on has 145 servers that we are responsible for, and only a handful of them are in the same building as us (although we still remote into them as well).

This being said, I was getting tired of typing in the server’s name or IP address every time I needed to connect to it.  I know Microsoft has an mmc for Remote Desktops, but I just don’t like the clunky way it has to be setup, plus, did I mention I had 145 to put in?  Well, call me lazy, but I knew there had to be a way to script it – after all, a RDP file is nothing but some text (open a .rdp file with notepad – you’ll see!).

So, enough background, lets do some scripting!  There are 3 files here:  Powershell script, a CSV file, and a Text file.

  • First, the CSV File:  It has 4 columns with the first row being the headers.  Do not change row.  Name this file RDPLIST.csv
  • ServerName,IP,Directory,USER
    FRIENDLYNAME,FQDNorIP,FOLDERNAME\SUBFOLDER,DOMAIN\USERNAME
  • Next, is the Text File.  This contains other parameters that are going to be the same between every connection – such as enabling shared clipboard, 16bit colors, disable themes, etc.  You can customize this to your liking (to get it just the way you want it, create an .rdp file, and edit it with notepad, and you will see a list similar to below). Name this file template_bottom.txt
  • audiomode:i:2
    authentication level:i:0
    autoreconnection enabled:i:1
    bitmapcachepersistenable:i:1
    compression:i:1
    disable cursor setting:i:0
    disable full window drag:i:1
    disable menu anims:i:1
    disable themes:i:1
    disable wallpaper:i:1
    displayconnectionbar:i:1
    keyboardhook:i:2
    redirectclipboard:i:1
    redirectcomports:i:0
    redirectdrives:i:0
    redirectprinters:i:0
    redirectsmartcards:i:0
    session bpp:i:16
    prompt for credentials:i:0
    promptcredentialonce:i:1
  • Lastly, the Powershell script: Name this file CreateRDP.ps1
  • $List = Import-CSV RDPLIST.CSV
    $resolutions = ("Console","fullscreen","1024x768","1152x864")
    ForEach($Entry in $List) {
      ForEach($resolution in $resolutions) {
        # Prepend the destination directory info for RDP files
        $Dir = "..\" + $resolution + "\" + $Entry.Directory
    
        # Create new folder
        New-Item -Path $Dir -ItemType Directory -Force
    
        # Build the file name
        $FileName = $Dir + "\" + $Entry.ServerName + ".RDP"
    
        # Remove the old file
        Remove-Item $FileName -Force
    
        # Begin building RDP file
        $temp = "`nfull address:s:" + $Entry.IP 
    
        switch ($resolution) {
    
          "Console" {
            $temp = $temp + "`nscreen mode id:i:1"
        $temp = $temp + "`ndesktopwidth:i:1024"
            $temp = $temp + "`ndesktopheight:i:768"
        $temp = $temp + "`nusername:s:" + $Entry.USER
        $temp = $temp + "`nadministrative session:i:1"
          }
    
          "Fullscreen" {
            $temp = $temp + "`nscreen mode id:i:2"
        $temp = $temp + "`nusername:s:" + $Entry.USER
          }
    
          "1024x768" {
            $temp = $temp + "`nscreen mode id:i:1"
            $temp = $temp + "`ndesktopwidth:i:1024"
            $temp = $temp + "`ndesktopheight:i:768"
        $temp = $temp + "`nusername:s:" + $Entry.USER
          }
    
          "1152x864" {
            $temp = $temp + "`nscreen mode id:i:1"
            $temp = $temp + "`ndesktopwidth:i:1152"
            $temp = $temp + "`ndesktopheight:i:864"
        $temp = $temp + "`nusername:s:" + $Entry.USER
          }
        }
        $temp | out-file $FileName
        write-host $temp
        get-content template_bottom.txt >> $FileName
      }
    }

Now, you need a folder structure:

  • Powershell_RDP
    • rdp
      • _script

      image

    Put all three files in the _script folder.

And just because I’m a nice guy, here’s a zip of the files and the correct folder structure.  Just unzip, make your edits to the csv file, save and close it, and open PowerShell (as Administrator if using Vista) and navigate to the _Script folder.  Once there, run the CreateRDP.ps1 (put a ./ in front of the filename)

image

Feel free to customize this script to your own liking!  It’s a simple script, so it shouldn’t be too hard to mess with.  It works like a CHARM!!!  I created 290 RDP files in less than 30 seconds!  Great thing is, it creates a separate folder structure for the different resolutions (I used 1024×768 & 1152×864 because my laptop is a widescreen, but when I’m docked, it’s a standard monitor).

One more tip, I made two new toolbars, and pushed them all the way over to the right of the taskbar, making a nifty little launcher!

image

Hope you enjoy this – I know I did!  It took me longer to write this post than it did to get all my .RDP files! :)

[ UPDATE:  Original Script was written by Aaron Dodd, just tweaked by me]

Share and Enjoy:
  • email
  • Facebook
  • Twitter
  • Digg
  • StumbleUpon
  • LinkedIn
  • Print
Categories: Microsoft, PowerShell, Scripts

VBS to EXE

August 6th, 2007 3 comments

I use a LOT of scripts.  I sometimes will roll out a script to users, or other people on the IT staff.  There are times that I would rather they not see the source of the script for various reasons.  Answer to this?  VBS to EXE!

VbsToExe is a command line application that converts VB-Scripts into an executable.  This utility hides the source of your scripts and protects them optionally with a password.

Share and Enjoy:
  • email
  • Facebook
  • Twitter
  • Digg
  • StumbleUpon
  • LinkedIn
  • Print
Categories: Scripts