<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ukstokes.com &#187; Scripting</title>
	<atom:link href="http://ukstokes.com/blog/category/scripting/feed/" rel="self" type="application/rss+xml" />
	<link>http://ukstokes.com/blog</link>
	<description>tech stuff from a tech bloke</description>
	<lastBuildDate>Fri, 02 Jul 2010 22:58:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Scripts to make en mass changes to profile paths</title>
		<link>http://ukstokes.com/blog/2009/06/11/scripts-to-make-en-mass-changes-to-profile-paths/</link>
		<comments>http://ukstokes.com/blog/2009/06/11/scripts-to-make-en-mass-changes-to-profile-paths/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 21:56:08 +0000</pubDate>
		<dc:creator>ben</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Active Directory]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://ukstokes.com/blog/?p=413</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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 <a href="http://www.codeplex.com/admodify">ADModify.NET</a>, 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.</p>
<p><a href="http://ukstokes.com/scripts/change_profile_paths.vbs.txt">ChangeProfilePaths.vbs.txt</a><br />
<a href="http://ukstokes.com/scripts/change_tsprofile_paths.vbs.txt">ChangeTerminalServerPaths.vbs.txt</a></p>
<p>The scripts are pretty much identical, the only bits you need to change are these lines at the top:</p>
<pre class="brush: vb;">SMTPServer = &quot;mail.ukstokes.ad&quot;
Set domain = GetObject(&quot;LDAP://OU=Company Users,DC=ukstokes,DC=ad&quot;)
NewTSPath = &quot;\\ukstokes.ad\data\profiles$\users\&quot;</pre>
<p>The LDAP string needs to be changed to the starting point in your AD tree. Every OU underneath will be affected.</p>
<p>Also these 2 lines at the bottom:</p>
<pre class="brush: vb;">objEmail.From = &quot;ben@------.ad&quot;
objEmail.To = &quot;ben@------.ad&quot;</pre>
<p>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.</p>
<p>Before making any mass changes like remember the golden rule to test it in an isolated environment first.</p>
]]></content:encoded>
			<wfw:commentRss>http://ukstokes.com/blog/2009/06/11/scripts-to-make-en-mass-changes-to-profile-paths/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get system stats automatically on SSH login</title>
		<link>http://ukstokes.com/blog/2009/04/10/get-system-stats-automatically-on-ssh-login/</link>
		<comments>http://ukstokes.com/blog/2009/04/10/get-system-stats-automatically-on-ssh-login/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 22:22:46 +0000</pubDate>
		<dc:creator>ben</dc:creator>
				<category><![CDATA[Enterprise Linux]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Bash scripting]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://ukstokes.com/blog/?p=126</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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.</p>
<p>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.</p>
<pre><code>
<pre class="brush: bash;">#!/bin/bash
cat /etc/motd | head -n 5 &gt; /tmp/file &amp;&amp; cat /tmp/file &gt; /etc/motd

CPUTIME=$(ps -eo pcpu | awk 'NR&gt;1' | awk '{tot=tot+$1} END {print tot}')
CPUCORES=$(cat /proc/cpuinfo | grep -c processor)

echo &quot;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
&quot; &gt;&gt; /etc/motd</pre>
<p></code></pre>
<p>Now when we log in we get a summary like this:</p>
<p><img src="http://ukstokes.com/images/stats.png" alt="SSH Server Stats" /></p>
]]></content:encoded>
			<wfw:commentRss>http://ukstokes.com/blog/2009/04/10/get-system-stats-automatically-on-ssh-login/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Identify computer type using VB Script</title>
		<link>http://ukstokes.com/blog/2009/04/06/identify-computer-type-using-vb-script/</link>
		<comments>http://ukstokes.com/blog/2009/04/06/identify-computer-type-using-vb-script/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 16:10:10 +0000</pubDate>
		<dc:creator>ben</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[VBScript]]></category>

		<guid isPermaLink="false">http://ukstokes.com/blog/?p=238</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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): </p>
<pre><code>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 &#038; " is a laptop" , vbInformation , "Results ... "
else
 MsgBox ComputerName &#038; " is not a laptop" , vbInformation , "Results ... "
End If </pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://ukstokes.com/blog/2009/04/06/identify-computer-type-using-vb-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mapping printers using a login script</title>
		<link>http://ukstokes.com/blog/2009/02/11/mapping-printers-using-a-login-script/</link>
		<comments>http://ukstokes.com/blog/2009/02/11/mapping-printers-using-a-login-script/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 12:24:05 +0000</pubDate>
		<dc:creator>ben</dc:creator>
				<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://ukstokes.com/blog/?p=148</guid>
		<description><![CDATA[Here's how: [lang='vb']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[/lang] To set the default printer: [lang='vb']  Select [...]]]></description>
			<content:encoded><![CDATA[<p>Here's how:</p>
<p>[lang='vb']Dim objNetwork, strPrintServer<br />
Set objNetwork = WScript.CreateObject("WScript.Network")</p>
<p>strPrintServer = "\\Your_Print_Server"</p>
<p>Private function GetUserObject<br />
On Error Resume Next<br />
Set GetUserObject = GetObject("WinNT://"  objNetwork.UserDomain  "/"  objNetwork.UserName)<br />
End function</p>
<p>set UserObject = GetUserObject</p>
<p>For Each Group in UserObject.Groups<br />
Select case Group.Name<br />
Case "Your_AD_Security_Group"<br />
objNetwork.AddWindowsPrinterConnection strPrintServer  "\Your_Printer_Name"<br />
End Select<br />
Next[/lang]</p>
<p>To set the default printer:</p>
<p>[lang='vb']  Select case Group.Name<br />
Case "Your_AD_Security_Group DEFAULT"<br />
objNetwork.setDefaultPrinter strPrintServer  "\Your_Printer_Name"[/lang]</p>
<p>Job done.</p>
]]></content:encoded>
			<wfw:commentRss>http://ukstokes.com/blog/2009/02/11/mapping-printers-using-a-login-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Run a scheduled task using VBS</title>
		<link>http://ukstokes.com/blog/2008/10/16/run-a-scheduled-task-using-vbs/</link>
		<comments>http://ukstokes.com/blog/2008/10/16/run-a-scheduled-task-using-vbs/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 10:12:30 +0000</pubDate>
		<dc:creator>ben</dc:creator>
				<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://ukstokes.com/blog/?p=107</guid>
		<description><![CDATA[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. [...]]]></description>
			<content:encoded><![CDATA[<p>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.</p>
<p><a href="http://ukstokes.com/scripts/scheduled_tasks.txt">A copy you can download is here</a> - I'm having a problem with character encoding on the site which is ruining code snippets like the one below. </p>
<p>[lang='vb']</p>
<p>strServer = "your_server_name"<br />
strTask = "task_name"</p>
<p>Continue = MsgBox ("Scheduled job " &amp; strTask &amp; " will run on " &amp; strServer &amp; ". Are you sure?",vbYesNo + VBQuestion, "Run scheduled task")</p>
<p>If Continue = vbYes then</p>
<p>Const OpenAsASCII = 0<br />
Const FailIfNotExist = 0<br />
Const ForReading = 1</p>
<p>sExe = "SCHTASKS /Run /S " &amp; strServer &amp; " /TN " &amp; strTask</p>
<p>Set oShell = CreateObject("WScript.Shell")<br />
Set oFSO = CreateObject("Scripting.FileSystemObject")<br />
sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")<br />
sTempFile = oFSO.GetSpecialFolder(2).ShortPath &amp; "\" &amp; oFSO.GetTempName<br />
oShell.Run "%comspec% /c " &amp; sExe &amp; " " &amp; sTempFile, 0 , True<br />
Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, FailIfNotExist, OpenAsASCII)</p>
<p>' capture output and inject into a variable</p>
<p>sResults = fFile.ReadAll<br />
fFile.Close<br />
oFSO.DeleteFile(sTempFile)<br />
MsgBox sResults , vbInformation , "Result"</p>
<p>Else</p>
<p>wscript.quit</p>
<p>End If</p>
<p>[/lang]</p>
]]></content:encoded>
			<wfw:commentRss>http://ukstokes.com/blog/2008/10/16/run-a-scheduled-task-using-vbs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>bash vs vbs (bash wins)</title>
		<link>http://ukstokes.com/blog/2008/08/07/bash-vs-vbs-bash-wins/</link>
		<comments>http://ukstokes.com/blog/2008/08/07/bash-vs-vbs-bash-wins/#comments</comments>
		<pubDate>Thu, 07 Aug 2008 12:18:00 +0000</pubDate>
		<dc:creator>ben</dc:creator>
				<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://ukstokes.com/blog/?p=97</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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:</p>
<p>[lang="vb"]Const ForReading = 1</p>
<p>Dim fso, TempFile, yesterday, strSubject<br />
yesterday = Date - 1</p>
<p>Set objRegEx = CreateObject("VBScript.RegExp")<br />
objRegEx.Pattern = yesterday</p>
<p>Set objFSO = CreateObject("Scripting.FileSystemObject")</p>
<p>If (objFSO.FileExists("C:\temp\output.txt")) Then<br />
objFSO.DeleteFile "C:\temp\output.txt"<br />
End If</p>
<p>Set objFile = objFSO.OpenTextFile("C:\Program Files\GFI\FaxMaker\sendlog.txt", ForReading)<br />
Set TempFile = objFso.CreateTextFile("C:\Temp\output.txt", True)<br />
Set wshShell = WScript.CreateObject ("WSCript.shell")</p>
<p>TempFile.Write "Fax Server sendlog for " &#038; yesterday &#038; vbCRLF &#038; "----------------------------------" &#038; vbCRLF &#038; vbCRLF</p>
<p>Do Until objFile.AtEndOfStream<br />
strSearchString = objFile.ReadLine<br />
Set colMatches = objRegEx.Execute(strSearchString)<br />
If colMatches.Count > 0 Then<br />
For Each strMatch in colMatches<br />
TempFile.Write strSearchString &#038; vbCRLF<br />
Next<br />
End If<br />
Loop</p>
<p>objFile.Close<br />
TempFile.Close</p>
<p>strSubject = "FaxMaker_Sendlog_" &#038; yesterday<br />
wshShell.run "C:\monitoring\blat262\full\blat.exe" &#038; " " &#038; "C:\temp\output.txt" &#038; " -to " &#038; "<a href="mailto:myaddress@mydomain.com">myaddress@mydomain.com</a>" &#038; " -subject " &#038; strSubject[/lang]</p>
<p>Partial credit goes to the MS Scripting Guy who had already <a href="http://www.microsoft.com/technet/scriptcenter/resources/qanda/mar07/hey0329.mspx" target="_self">published part of this</a> for me. Also you'll notice I called <a href="http://www.blat.net/" target="_self">blat</a> 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!</p>
<p>To accomplish the same task using a bash script, you could do it like this:</p>
<p>[lang="bash"]echo "Fax server sendlog for `date +%d/%m/%y`" > /tmp/tempfile.txt<br />
cat /var/log/sendlog.txt | grep `date +%d/%m/%y` >> /tmp/tempfile.txt<br />
mail -s "Fax Server log for `date +%d/%m/%y`" myaddress@mydomain.com < /tmp/tempfile.txt[/lang]</p>
<p>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.</p>
]]></content:encoded>
			<wfw:commentRss>http://ukstokes.com/blog/2008/08/07/bash-vs-vbs-bash-wins/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
