Check if a certain user is logged on the Terminal Server using Powershell

Just recently I have been developing a Powershell script to determine if a specific user on a Windows Terminal Server is logged in or not. If the user happens to not be logged in, It will e-mail a message to me and another co-worker to log that user in immediately.

It basically uses the quser command to get a list of the currently logged in users on the terminal server and stores that info into the variable $nwt. I then setup a if statement to see if it matches “nwt” which is the username I want to make sure is logged in at all times. If it does not find the string nwt in the $nwt variable, it will then send an e-mail to myself and a co-worker notifying us that he is not logged in. This is possible via the “System.Net.Mail” .net object in the .net framework. Here is a site with more information on the System.Net.Mail object, that helped me add a CC address to the e-mail, here is the link: systemnetmail.com. And that’s really all there is too it!

So far the script has been a success, since it has alerted us as to when this critical user account logs out so we can quickly log him back in. (We have some software that needs this specific user account to run properly.. it’s really crappy software..)

Below is the code:
——————————————————————-

$nwt = invoke-expression -Command "quser nwt"

$date = date

if($nwt -is [Array]){

if($nwt[1] -match "nwt"){

write-host "--> NWT is logged in!"

echo "--> NWT is logged in! [ $date ]" > ./nwt.log

}

}else{

write-host "--> [ Problem ] NWT is not logged in!"

$emailFrom = "administrator@example.com"

$emailTo = "youremail@example.com"

$subject = "NWT is not logged in"

$body = "

The NWT user account is not logged in on Termserver. This was detected at:[ $date ]"$smtpServer = "mail.example.local"

$msg = new-object System.Net.Mail.MailMessage $emailFrom, $emailTo,

$subject, $body

$msg.CC.Add("co-worker@example.com")

$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$smtp.Send($msg)

}


——————————————————————-

  • Share/Bookmark

Windows – No Disk

The other day a client calls up and says she keeps getting a “Windows – No Disk” window, that will not go away no matter how many times she closes it.  I then asked when did this problem appear, in which she replied “after I pulled out the USB Flash Drive”.  So at this point I was thinking that some files she was working on, wanted to save themselves to the usb flash drive and could not since the drive was not present, thus causing the annoying “No Disk” error.  I then instructed her to save her work elsewhere on the hard drive and restart her workstation.  Windows - No Disk ErrorUnfortunately however, after she restarted her workstation and logged in the error continued to pop up!

Since the restart didn’t solve the issue, I knew it was time pull out my best tool in the shed “Google Search”.  After a couple google searches and a couple forums, I found out that the reason for this error is due to a usb driver that became corrupted during installation. The solution is to uninstall the usb drivers and reinstall them.

Once I had uninstalled all the usb drivers in Computer Management and restarted the workstation, the “Windows – No Disk” error appeared no more.  Mere mortals can only guess why the usb driver became corrupted in Windows, but thanks to google and a post on technet this XP annoyance was solved!

References:

http://social.technet.microsoft.com/Forums/en/itprovistahardware/thread/d5f38918-d11b-45c4-861d-b53aa3dbdaed

  • Share/Bookmark

NTBACKUP error – “End of Media encountered while backing up to non-removable media.”

Today I was investigating why some ntbackup jobs were failing on a remote server, and came across this error message in the logs : “End of Media encountered while backup up to non-removable media”. So I googled the error, and came up with a couple forums talking about how backups being saved to tape drives get this error when the tape runs out of space.  In my situation however, I was not using a tape drive for the backups, I was using an external hard drive.

Below is a screenshot of the NTBACKUP log

NTBACKUP log error

As you can see in the NTBACKUP log, when it tried to backup the files to the external hard drive it encountered the “End of Media” error.

So at this point I was hitting my head against the wall wondering why in the *(#$ is this happening!!?  The external hard drive had plenty of free space for the backups, it just didn’t make any sense!!  As a usual Sysadmin response I decided to dig dipper into the issue.  After thinking it though a bit, I had some inspiration to check “Computer Management” and take a look at the partition and filesystem setup of the external hard drive.  Below is a screenshot of what I discovered in Computer Management:

computer management - bestsrv

If you look volume E: in the top right, under filesystem it reads “fat32″! Once I saw that it occurred to me that the backups were failing because they exceeded the 4GB file size limit of the fat32 filesystem.  Sure enough after checking the backup sizes It was just barely over 4GB which would explain why NTBACKUP reported that the disk was out of space.  After discovering the true cause behind the failing backups, I backed-up all the data on the Western Digital MyBook, reformatted it with NTFS, and transferred all the data back onto the external drive.  That effectively solved the issue and allowed the customer to continue to backup the server without buying any additional hardware.

It just comes to show that most problems are simple to fix and easy to overlook.

References:

http://www.experts-exchange.com/Storage/Misc/Q_20710438.html

http://en.wikipedia.org/wiki/File_Allocation_Table

  • Share/Bookmark

Powershell built-in GUI Editor!!

Many of you have probably been on the look for a gui editor for the new windows powershell, well look no more!  Microsoft has included a built-in gui editor for powershell (I didn’t even know until now), and all you need to do to run it is follow one of the 2 ways below.

Method 1

1. Click Start
2. Type “Powershell ise” and hit enter
3.  The Powershell integrated scripting environment will then start!

Method 2

1.  Open powershell
2.  In the console type: “powershell_ise.exe”
3.  Hit enter.

Below are some screenshots for reference:

powershell - run ise

Below is a screenshot of what the Powershell ISE looks like.  Check it out!

powershell editor

The powershell gui editor basically works like this:  The top area is your script editor, the middle area is where you will see the output/results of your powershell script and commands.  The bottom area is where you can enter powershell commands as if you were on the powershell command prompt.

Once you start using it, I am sure you will see a increase in productivity while creating/testing scripts.  This is because it allows you to edit a script, run it, see it’s output, and even run commands as if you were at the command prompt.  It’s streamlined everything you could want or need into one program!  +1 point for Microsoft! (we’ll see how long that lasts lol).

Well what are you waiting for?  Lets get scripting!

Powershell Download Links

1.  Powershell 2.0 CTP

  • Share/Bookmark

Unattended FTP script to download files

Looks like I also found a nice quick way to download files off my webserver using a ftp script.  It’s so simple that I don’t know why I didn’t even think of it before!  Before I had this script, I had to manually download my sysadmin-tools from my website onto the remote servers I was doing maintenance on that particular day.  That took about 3-5 minutes each time I had to download the tools I had in a zip file on my webserver, which as you can imagine get really repetitive and annoying after a while.  So after about the 50th time downloading the sysadmin-tools, I decided to automate it!

This is how I approached creating the script:

1.  It has to work out of the box with Windows (I administer and maintain Windows Server 2003 boxes)

2.  It had to connect to my webserver using my username and password

3.  It had to change directories on the server to the download directory

4.  K.I.S.S (keep it simple stupid!)

Decisions:

After reviewing the script requirements, I decided to write a simple batch script using the built in FTP command in windows.  I didn’t know windows had a built in FTP command to tell you the truth until I starting writing the script.

Unattended FTP Script:

Below are the contents of the batch file:

@echo off

ftp -s:ftpscript.txt

Below are the contents of the ftpscript.txt file:

open
yourserver.com
username
password
cd “/public_html/yourdownloadfolder”
binary
get “sysadmin-tools.zip” “C:\Users\username\Desktop\sysadmin-tools.zip”
quit

Explanation:

It’s pretty straightforward, the batch file using the ftp command to start the built-in ftp program in windows.  After that the batch script calls the “ftpscript.txt” file to supply the ftp command to execute.  In the ftpscript.txt file it basically says to connect to a server, enters a username and password, then changes directories to the directory that contains the file you wish to download, switches the FTP mode to binary so it will download the .zip file correctly, downloads the file to somewhere on my Desktop, and finally quits FTP.

FTP Modes:

Make sure you are downloading in the right FTP mode!! I was getting corrupted files at first when I was making the script because I was downloading the .zip file in the wrong mode.  Once I changed to binary it downloaded successfully without any corruption issues.

Text files (html, php, css, java) — use text mode
Multimedia files (images, audio, video) — use binary mode
Programs (.exe, .class, .jar, .swf) — use binary mode

References:
http://www.robvanderwoude.com/ftp.php
http://www.bluehostforum.com/showthread.php?t=9311

  • Share/Bookmark