24 January 2013

mechita - Remote Desktop application written in C#

If you have 2 or more Windows Servers in your domain, then using Remote Desktop Connection for administering a lot of servers is probably a bit frustrating.

image

That’s why I wrote my own application for easy management of all Windows Servers. The application can be downloaded from http://bit.ly/WWwuGp. It uses .NET Framework 4.0, so you need to install this also if not already installed.

It loads all computer accounts, where operating system contains the word “server” and list them in the list, so you can with one click log on to any server.

image

If your logged on user account is not the account for administering the server, you should use “Run as different user…” for launching the .exe file.

If you would like to save your password for even faster logging, check the password check-box and type your password in pop-up window and press OK. It will be saved to registry for local user [HKCU\Software\mechita] so other users of your computer will not see it (unless they have administrative rights). Clearing the password check box removes the entry from registry.

image

Console check box allows to log on to console session (equivalent to mstsc /console or mstsc /admin). For example Windows Server 2003 desktop session would be a possible use of that. Or server, where terminal licensing is not activated and the trial time is over.

“X servers in domain” text shows, how many server computer accounts are in AD. By clicking that text (yes, I know that’s not a intuitive approach), the servers list will be updated along with the computer count (for example if you have discovered, that you have an old server account and you deleted that account from AD).

“Close session” button closes the active tab and disconnects from that server.

For going into full screen, press Ctrl+Alt+Pause. The same key combination is also used for returning from full screen mode.

I hope you like this application. any suggestions for improvements are welcome. Put them in comment area.

Ideas so far:

  1. Favorite servers list or most recently used servers list
  2. Reconnect connection in active tab page
  3. Resize application window should resize terminal session also
  4. Server’s list should be cached
  5. Remote Desktop Gateway should be configurable (in conjunction with servers list cache this could be awesome). At the moment no RDP Gateway is used.

Shrink all databases on a Microsoft SQL Server instance

When disk free space on your Windows Server goes below your threshold and you discover, that your MSSQL installations DATA directory is the biggest one with lot’s of large log files (.LDF extension), then this solution is for you.

This very simple command:

EXEC sp_MSForEachDB 'select ''?'' as [Database]; ALTER DATABASE [?] SET RECOVERY SIMPLE; DBCC SHRINKDATABASE (''?'' , 0)'

get’s all databases, and for each:

  1. Displays the database name
  2. Changes database recovery model to Simple (Important! Read this information http://technet.microsoft.com/en-us/library/ms189275.aspx for implications when changing Recovery Model)
  3. Shrinks all database files, leaving 0% free space inside database and log files

This SQL Management Studio screenshot shows the output of the command:

image

In my case 14GB DATA directory was reduced to 2GB. Impressive.

22 January 2013

Easily schedule deletion of older files in a particular folder

In a previous post http://raunomagi.blogspot.com/2012/05/how-to-delete-files-older-than-x-days.html I revealed how to delete older files, but some folder keep growing up and need regular cleaning.

Let’s assume, that the folder, that is growing is IIS web logging in folder C:\inetpub\logs\LogFiles\W3SVC1 (this can be any folder, but to make it more realistic I use this one). To check, how big this folder is right now, use this PowerShell command:

dir C:\inetpub\logs\LogFiles\W3SVC1 | measure -sum length

image

It’s not easily readable, but I see 150 files using 29GB of disk space.

For scheduled deletion you need only few lines to paste into administrative command prompt:

  1. echo forfiles /d -50 /p C:\inetpub\logs\LogFiles\W3SVC1 /c "cmd /c del @path" > %windir%\DeleteOldFiles.bat
  2. schtasks /create /RU:"" /SC:DAILY /TR "'%windir%\DeleteOldFiles.bat'" /ST:01:00 /RL:HIGHEST /TN:DeleteOldFiles
  3. schtasks /run /TN:DeleteOldFiles

image

Line 1 creates a file in Windows directory named DeleteOldFiles.bat. If your directory is somewhere else, change it accordingly. If the path to the folder includes spaces, surround it with quotation marks. The .BAT file looks like this:

image

Line 2 creates a scheduled task for running the .BAT file under SYSTEM account daily at 01:00. The task can be modified later on Task Scheduler:

image

Line 3 run’s the scheduled task right away.

After running the task I returned to my PowerShell window and rechecked the result:

image

So it seems, like I got almost 20GB of free space Smile

04 January 2013

How to enable Active Directory Recycle Bin and restore deleted objects (users, groups etc)

In Windows Server 2003 and Windows Server 2008 the best option to restore a deleted user account was the command line utility adrestore.exe from http://live.sysinternals.com/ website. The restore option worked fairly well. It will keep the same security ID (SID) and logon name so user can log on to same profile on Windows. Side-effect is lost password, phone number, group membership etc. And the account is in disabled state, so you need to enable the acoount and assign a new password.

Since Windows Server 2008 R2 you have the option to enable the Active Directory Recycle Bin. For that you need:

  1. All domain controllers must be Windows Server 2008 R2 (or later). So remove older ones by running dcpromo wizard on them. And if you don’t have W2008R2 domain controller, install one first.
  2. Forest and domain functional levels must be Windows Server 2008 R2. To change it, use domain.msc (Active Directory Domains and Trusts). Remember, that this change is irreversible. And be sure that you don’t have older domain controllers.
    imageimage
  3. AD Recycle Bin must be activated by using the following PowerShell command:
    Enable-ADOptionalFeature -Identity 'CN=Recycle Bin Feature,CN=Optional Features,CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration,DC=contoso,DC=com' -Scope ForestOrConfigurationSet -Target 'contoso.com'
    You must type your domain name instead of contoso.com.
  4. Delete a sample user account using your favorite tool. For example “net user test /delete /domain
  5. Restore can be easily done using PowerShell commands.
    1. First you need to find the affected object:
      Get-ADObject -Filter {name -like "*PhraseFromUsername*" -and deleted -eq $true} -IncludeDeletedObjects
    2. Make sure your result includes only the deleted account and use the same command and pipe it to Restore-ADObject cmdlet:
      Get-ADObject -Filter {name -like "*PhraseFromUsername*" -and deleted -eq $true} –IncludeDeletedObjects | Restore-ADObject

And now you can use this user account (password is kept, username, group membership etc. will be retained).

For more information please visit http://technet.microsoft.com/en-us/library/dd392261(WS.10).aspx