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

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

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

Shutdown script did not shutdown the sever! Exit Code 0x4f7

Well recently a shutdown script that I wrote did not shutdown a server successfully for some odd reason.  The script looks like the following:

shutdown -r -t 20 -c “Shutting Down to Install Windows Updates”

So until now this script has been working flawlessly on multiple servers.  I’ve been able to schedule automatic reboots of remote servers easily using this script in combination with windows scheduled tasks as well.  It comes in handy when you need to reboot the server at a specific time, such as 5:25PM or 1:26AM, which is great for me since I do a lot of remote server maintenance and do not have physical access to these servers.

Anyway, after trying to reboot this one server it gave me a exit code of (0x47f) which showed up in the scheduled tasks log.

scheduled tasks 0x4f7

After Looking up the exit code, I found a website that directed me to convert 47f to decimal and enter the decimal value “net helmsg” in the command line.  The command I entered was “net helpmsg 1271″, which gave me the following answer: “The machine is locked and can not be shut down without the force option”.

net helpmsg 1271

Well isn’t that great!  A error code that is easily understandable from Microsoft!

The solution to this problem was to edit my script to include the force option.  Here is the updated script below:

shutdown -r -f -t 20 -c “Shutting Down to Install Windows Updates”

And that should do the trick.

References:

http://www.tech-archive.net/Archive/Windows/microsoft.public.windows.terminal_services/2006-08/msg00331.html

  • Share/Bookmark

Windows Updates restart now or later option appears to all users on the Terminal Server

Just as the title suggests, Windows Update pops up notifications to restart the server now or later to all users on a Terminal Server.  I ran into this yesterday when I had installed a security update and a few miscellaneous ones as well.  I figured that the restart notifications would only be shown on the administrator accounts and not all users on the Terminal Server.  That proved to be a bad assumption on my part, but a logical one.  Why would regular users need to be notified that the server needed to be rebooted to finish installing updates when they don’t even have the security permissions to do so?

Anyway the Chief Financial Officer e-mailed me wondering what was going on later in the afternoon.  She and other users were getting really annoyed by the constant pop up to restart the server ( shoot I would be too!).  So in the end I told her to restart the server and that was the end of the problem.  Why couldn’t Microsoft restrict these updates notifications and restart pop up’s to only the administrator accounts?  Maybe there is a registry setting that I just don’t know about that could do all this for me, but for now I’ll just remember to schedule these updates during off hours.

  • Share/Bookmark