We've been migrating shares on our main file server to a DFS share to give us some flexibility with our storage. Part of these changes involve migrating Profile Paths and Terminal Server Profile Paths to the new DFS share location. After the data was being synchronised by DFS we needed a way to change the Profile Path and TS Profile Path attribute on all accounts in part of our Active Directory. This cannot be done using AD Users and Computers - if you bulk select users in ADUC and bring up the collective properties, you will notice the TS Profile Path is not available for editing. There are a few free tools that can do this, notibly ADModify.NET, but we needed a way to script it so it could be run overnight, and also only make changes to the paths if one existed already.
ChangeProfilePaths.vbs.txt
ChangeTerminalServerPaths.vbs.txt
The scripts are pretty much identical, the only bits you need to change are these lines at the top:
SMTPServer = "mail.ukstokes.ad"
Set domain = GetObject("LDAP://OU=Company Users,DC=ukstokes,DC=ad")
NewTSPath = "\\ukstokes.ad\data\profiles$\users\"
The LDAP string needs to be changed to the starting point in your AD tree. Every OU underneath will be affected.
Also these 2 lines at the bottom:
objEmail.From = "ben@------.ad"
objEmail.To = "ben@------.ad"
The script will send an email at the end with a summary of the changes so you will need to change the From and To addresses accordingly.
Before making any mass changes like remember the golden rule to test it in an isolated environment first.
I noticed in recent versions of Ubuntu you get some system stats in a banner message when you connect using SSH. I thought this was pretty useful so have implemented my own version on our Red Hat servers at work.
This runs every 5 minutes as a cron job and updates the file /etc/motd (Message Of The Day) which is shown when a user logs in. Our servers already have a 5 line banner message with information including the server name and purpose of the server, hence the first 5 lines being saved and readded into a new /etc/motd each time.
#!/bin/bash
cat /etc/motd | head -n 5 > /tmp/file && cat /tmp/file > /etc/motd
CPUTIME=$(ps -eo pcpu | awk 'NR>1' | awk '{tot=tot+$1} END {print tot}')
CPUCORES=$(cat /proc/cpuinfo | grep -c processor)
echo "System summary (collected `date`)
- CPU Usage (total average) = `echo $CPUTIME / $CPUCORES | bc`%
- Memory free (real) = `free -m | head -n 2 | tail -n 1 | awk {'print $4'}` Mb
- Memory free (cache) = `free -m | head -n 3 | tail -n 1 | awk {'print $3'}` Mb
- Swap in use = `free -m | tail -n 1 | awk {'print $3'}` Mb
" >> /etc/motd
Now when we log in we get a summary like this:

I required some VB which would run as a shutdown script and run some specific commands if the computer was not a laptop. Luckily this was simplified by the naming convention on our corporate network - all laptops have an L in the computer name. Here is one way this can be done - using a regular expression to find the number of L's in the computer name (and then take specific action if it is less than 1):
Option Explicit
Dim objNTInfo, ComputerName, myRegExp, myMatches
Set objNTInfo = CreateObject("WinNTSystemInfo")
ComputerName = lcase(objNTInfo.ComputerName)
'Prepare a regular expression object
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Global = True
myRegExp.Pattern = "l"
Set myMatches = myRegExp.Execute(computerName)
If myMatches.Count > 0 Then
MsgBox ComputerName & " is a laptop" , vbInformation , "Results ... "
else
MsgBox ComputerName & " is not a laptop" , vbInformation , "Results ... "
End If
Here's how:
Dim objNetwork, strPrintServer
Set objNetwork = WScript.CreateObject("WScript.Network")
strPrintServer = "\\Your_Print_Server"
Private function GetUserObject
On Error Resume Next
Set GetUserObject = GetObject("WinNT://" objNetwork.UserDomain "/" objNetwork.UserName)
End function
set UserObject = GetUserObject
For Each Group in UserObject.Groups
Select case Group.Name
Case "Your_AD_Security_Group"
objNetwork.AddWindowsPrinterConnection strPrintServer "\Your_Printer_Name"
End Select
Next
To set the default printer:
Select case Group.Name
Case "Your_AD_Security_Group DEFAULT"
objNetwork.setDefaultPrinter strPrintServer "\Your_Printer_Name"
Job done.
One of our users has an occasional requirement to manually run a scheduled task on a server. I wanted him to do this without logging on via RDP. Scheduled tasks can be run remotely using the SVHTASKS program (on Windows XP and Server 2003) so I started writing a VB Script to call this app. The tricky part was capturing the output of the command and displaying that back to the user, so he would know if it was a success or failure. It turns out this is actually quite difficult to do, so I hacked up various other scripts I found on the web until I eventually got this to work. The working version is below.
A copy you can download is here - I'm having a problem with character encoding on the site which is ruining code snippets like the one below.
strServer = "your_server_name"
strTask = "task_name"
Continue = MsgBox ("Scheduled job " & strTask & " will run on " & strServer & ". Are you sure?",vbYesNo + VBQuestion, "Run scheduled task")
If Continue = vbYes then
Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1
sExe = "SCHTASKS /Run /S " & strServer & " /TN " & strTask
Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
sTempFile = oFSO.GetSpecialFolder(2).ShortPath & "\" & oFSO.GetTempName
oShell.Run "%comspec% /c " & sExe & " " & sTempFile, 0 , True
Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, FailIfNotExist, OpenAsASCII)
' capture output and inject into a variable
sResults = fFile.ReadAll
fFile.Close
oFSO.DeleteFile(sTempFile)
MsgBox sResults , vbInformation , "Result"
Else
wscript.quit
End If
I've been asked by a group of users if they can have a daily report of everything the fax server has sent. Since the fax software we're using doesn't have this feature, I thought I'd write some vb script to pull all of lines containing yesterday's date from the log file (called sendlog.txt) and email them to a public folder. This took an hour or so to create and looks like this:
Const ForReading = 1
Dim fso, TempFile, yesterday, strSubject
yesterday = Date - 1
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = yesterday
Set objFSO = CreateObject("Scripting.FileSystemObject")
If (objFSO.FileExists("C:\temp\output.txt")) Then
objFSO.DeleteFile "C:\temp\output.txt"
End If
Set objFile = objFSO.OpenTextFile("C:\Program Files\GFI\FaxMaker\sendlog.txt", ForReading)
Set TempFile = objFso.CreateTextFile("C:\Temp\output.txt", True)
Set wshShell = WScript.CreateObject ("WSCript.shell")
TempFile.Write "Fax Server sendlog for " & yesterday & vbCRLF & "----------------------------------" & vbCRLF & vbCRLF
Do Until objFile.AtEndOfStream
strSearchString = objFile.ReadLine
Set colMatches = objRegEx.Execute(strSearchString)
If colMatches.Count > 0 Then
For Each strMatch in colMatches
TempFile.Write strSearchString & vbCRLF
Next
End If
Loop
objFile.Close
TempFile.Close
strSubject = "FaxMaker_Sendlog_" & yesterday
wshShell.run "C:\monitoring\blat262\full\blat.exe" & " " & "C:\temp\output.txt" & " -to " & "<a href="mailto:myaddress@mydomain.com">myaddress@mydomain.com</a>" & " -subject " & strSubject
Partial credit goes to the MS Scripting Guy who had already published part of this for me. Also you'll notice I called blat to email the file, which is considerably less complex than sending it in the vbs (or cheating, as some might call it) ... using vbs to send the message would have doubled the amount of code!
To accomplish the same task using a bash script, you could do it like this:
echo "Fax server sendlog for `date +%d/%m/%y`" > /tmp/tempfile.txt
cat /var/log/sendlog.txt | grep `date +%d/%m/%y` >> /tmp/tempfile.txt
mail -s "Fax Server log for `date +%d/%m/%y`" myaddress@mydomain.com < /tmp/tempfile.txt
Much simpler I think - only 3 lines of code!! People say Linux is harder than Windows but I think this proves that is not always the case. Manipulating files and data in Linux can take considerably less effort if you know which tools to use and how to use them.
Recent Comments