<?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>Tue, 01 Nov 2011 11:29:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Pretty Gmail notifications for Ubuntu</title>
		<link>http://ukstokes.com/blog/2011/10/22/pretty-gmail-notifications-for-ubuntu/</link>
		<comments>http://ukstokes.com/blog/2011/10/22/pretty-gmail-notifications-for-ubuntu/#comments</comments>
		<pubDate>Sat, 22 Oct 2011 19:25:05 +0000</pubDate>
		<dc:creator>ben</dc:creator>
				<category><![CDATA[Desktop Linux]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[gmail]]></category>
		<category><![CDATA[libnotify]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://ukstokes.com/blog/?p=868</guid>
		<description><![CDATA[I've recently gone back to Ubuntu (10.11), and in my new installation wondered if there was an app I could install which gave me Gmail notifications via the default notification system (libnotify). I was a bit surprised to find that there wasn't, so I wrote a script in bash that does it: gmailpopup  My script [...]]]></description>
			<content:encoded><![CDATA[<p>I've recently gone back to Ubuntu (10.11), and in my new installation wondered if there was an app I could install which gave me Gmail notifications via the default notification system (libnotify). I was a bit surprised to find that there wasn't, so I wrote a script in bash that does it:</p>
<p style="text-align: center;"><a href="http://ukstokes.com/blog/wp-content/uploads/2011/10/gmailpopup.png"><img class="size-full wp-image-869 aligncenter" title="gmailpopup" src="http://ukstokes.com/blog/wp-content/uploads/2011/10/gmailpopup.png" alt="gmalpopup" width="414" height="478" /></a><em>gmailpopup </em></p>
<p>My script also stores your password hashed (unlike some of the alternatives in Software Centre), in a hidden file in your /home.</p>
<p><strong>Installation</strong></p>
<p>To install, run the following:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">
sudo apt-get install curl gnome-gmail
wget -q -O ~/gmailpopup http://ukstokes.com/scripts/gmailpopup
chmod +x ~/gmailpopup
sudo cp ~/gmailpopup /usr/bin
</pre>
<p>To configure it just type <strong>gmailpopup</strong> in a terminal and enter your Gmail address (GApps domains also supported) and password. If you have accidentally fat fingered the password and get an error, you can start again with <strong>gmailpopup --reconfigure</strong>. On subsequent runs you won't get a popup unless your unread email is different from last time.</p>
<p>This doesn't seem to run if inserted into the crontab. But it works fine if this is added as a startup application:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">
while true; do ~/bin/gmailpopup &amp;&amp; sleep 1m; done &amp;
</pre>
<p>Of course, this doesn't just work on Ubuntu. Any distro which uses libnotify will do. By the way the gnome-gmail package is just required for the Gmail icon. There is probably a better way of doing that, which I'll have a think about, but installing gnome-gmail has the added advantage of making Gmail your default mail client for mailto links etc. If that is not what you want, go to the "Default Applications" in "System Info" to set it back.</p>
<p>Thats it. Leave me a comment and let me know what you think!</p>
]]></content:encoded>
			<wfw:commentRss>http://ukstokes.com/blog/2011/10/22/pretty-gmail-notifications-for-ubuntu/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A cunning problem with for loops</title>
		<link>http://ukstokes.com/blog/2011/03/18/a-cunning-problem-with-for-loops/</link>
		<comments>http://ukstokes.com/blog/2011/03/18/a-cunning-problem-with-for-loops/#comments</comments>
		<pubDate>Fri, 18 Mar 2011 07:26:34 +0000</pubDate>
		<dc:creator>ben</dc:creator>
				<category><![CDATA[Desktop Linux]]></category>
		<category><![CDATA[Enterprise Linux]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[Bash scripting]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://ukstokes.com/blog/?p=815</guid>
		<description><![CDATA[I came across a cunning little problem with for loops where the values being parsed contain spaces, and can offer a couple of solutions to it. I'll explain what I mean with some code. If you md5sum a bunch of files, you get this kind of output: I wanted to iterate through a list of [...]]]></description>
			<content:encoded><![CDATA[<p>I came across a cunning little problem with for loops where the values being parsed contain spaces, and can offer a couple of solutions to it. I'll explain what I mean with some code.</p>
<p>If you md5sum a bunch of files, you get this kind of output:</p>
<pre class="brush: bash; title: ; notranslate">[ben@minitron ~]$ md5sum *.txt
d41d8cd98f00b204e9800998ecf8427e  myfile.txt
cdbd2b665e14f8803d6bddc80bff1402  omg.txt
6353f9b2e7c47f73b48eef4544968bcd  otherfile.txt</pre>
<p>I wanted to iterate through a list of files and their md5sums using a for loop, run a test and then take some action based on the result. But my script kept throwing unexpected output. I realised the for loop was parsing each word, rather than each line because of the spaces.</p>
<pre class="brush: bash; title: ; notranslate">[ben@minitron ~]$ for line in $(md5sum *.txt); do
echo $line; done
d41d8cd98f00b204e9800998ecf8427e
myfile.txt
cdbd2b665e14f8803d6bddc80bff1402
omg.txt
6353f9b2e7c47f73b48eef4544968bcd
otherfile.txt</pre>
<p>Sometimes putting spaces around variables can help bash to resolve values in the right way, but here it caused all line breaks to be removed:</p>
<pre class="brush: bash; title: ; notranslate">[ben@minitron ~]$ for line in &quot;$(md5sum *.txt)&quot;; do echo $line; done
d41d8cd98f00b204e9800998ecf8427e myfile.txt cdbd2b665e14f8803d6bddc80bff1402 omg.txt 6353f9b2e7c47f73b48eef4544968bcd otherfile.txt</pre>
<p>I needed to preserve the original line breaks in order for my script to make sense of which md5sum matched up to which file. There are a couple of ways to do this. One possible solution is to use <strong>tr </strong>to translate the spaces into a different character:</p>
<pre class="brush: bash; title: ; notranslate">[ben@minitron ~]$ for line in $(md5sum *.txt | tr &quot; &quot; :); do
echo $line; done
d41d8cd98f00b204e9800998ecf8427e::myfile.txt
cdbd2b665e14f8803d6bddc80bff1402::omg.txt
6353f9b2e7c47f73b48eef4544968bcd::otherfile.txt</pre>
<p>But for some reason md5sum uses 2 spaces for a field separator, if you wanted just one character between the md5sum and the filename, you could go a little more advanced:</p>
<pre class="brush: bash; title: ; notranslate">[ben@minitron ~]$ for file in *.txt; do
echo -n &quot;$file:&quot;;md5sum $file | cut -d &quot; &quot; -f1 ; done
myfile.txt:d41d8cd98f00b204e9800998ecf8427e
omg.txt:cdbd2b665e14f8803d6bddc80bff1402
otherfile.txt:6353f9b2e7c47f73b48eef4544968bcd</pre>
<p>This does give the output in the other way round but was good enough for me to make my script work.</p>
]]></content:encoded>
			<wfw:commentRss>http://ukstokes.com/blog/2011/03/18/a-cunning-problem-with-for-loops/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Changing the &#8220;administrator&#8221; password</title>
		<link>http://ukstokes.com/blog/2010/09/15/changing-the-administrator-password/</link>
		<comments>http://ukstokes.com/blog/2010/09/15/changing-the-administrator-password/#comments</comments>
		<pubDate>Wed, 15 Sep 2010 19:08:31 +0000</pubDate>
		<dc:creator>ben</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Windows Servers]]></category>

		<guid isPermaLink="false">http://ukstokes.com/blog/?p=715</guid>
		<description><![CDATA[Putting my Windows sysadmin hat on again, here's another couple of scripts that may help with changing your domain\administrator password, these will identify places where your administrator account is logging on. Obviously you can change them to look for any domain account. First script will interrogate the event log on all servers listed in "servers.txt", [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://ukstokes.com/blog/wp-content/uploads/2010/09/ms_logo.jpg"><img src="http://ukstokes.com/blog/wp-content/uploads/2010/09/ms_logo.jpg" alt="Microsoft Logo" title="ms_logo" width="101" height="94" class="alignleft size-full wp-image-730" /></a>Putting my Windows sysadmin hat on again, here's another couple of scripts that may help with changing your domain\administrator password, these will identify places where your administrator account is logging on. Obviously you can change them to look for any domain account.  </p>
<p>First script will interrogate the event log on all servers listed in "servers.txt", and create a report of logon times for the administrator account. It uses <a href="http://www.microsoft.com/DownLoads/details.aspx?FamilyID=890cd06b-abf8-4c25-91b2-f8d975cf8c07">Microsoft Log Parser</a> which is a free download.</p>
<div>
<pre class="brush: powershell; title: ; notranslate">@echo off
set LOG=C:\scripts\log_parser.log
set servers=C:\scripts\servers.txt
set logparser=&quot;C:\Program Files\Log Parser 2.2\LogParser.exe&quot;

FOR /F %%i IN (%servers%) DO call :PROC %%i
goto END

:PROC
ping -n 1 -w 10 %1 | find /C &quot;Reply&quot; &gt; ping.txt
set /p time=&lt;ping.txt
if /I %time% GTR 0 (echo Got ping from %1 &gt;&gt; %LOG%) ELSE echo Skipping %1 &amp;&amp; goto END
echo Processing %1 ...
%logparser% &quot;SELECT TimeGenerated, SourceName, EventCategoryName, Message INTO C:\scripts\%1.txt FROM \\%1\Security WHERE EventID = 528 AND SID LIKE '%%dministrator%%'&quot; -resolveSIDs:ON
echo.

:END
del /Q /F ping.txt</pre>
</div>
<p>The 2nd script is to query the services database on all servers in "servers.txt", and identify any services that are logging on as administrator (or matching string "admin*"). Just modify line 13 in the first script and run it again. </p>
<div>
<pre class="brush: powershell; first-line: 13; title: ; notranslate">
echo Processing %1 ...
for /F &quot;usebackq tokens=2 delims=:&quot; %%a in (`sc &quot;\\%1&quot; query ^| find &quot;SERVICE_NAME&quot;`) do @sc \\%1 qc%%a | find /I &quot;admin&quot; &amp;&amp; @echo %%a
echo.</pre>
</div>
<p>After that you just have to identify applications where the password may be typed in, in our instance the remaining place was a backup job for our Sharepoint server. Job done!</p>
]]></content:encoded>
			<wfw:commentRss>http://ukstokes.com/blog/2010/09/15/changing-the-administrator-password/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Script to list all local admins in your domain</title>
		<link>http://ukstokes.com/blog/2010/08/15/script-to-list-all-local-admins-your-domain/</link>
		<comments>http://ukstokes.com/blog/2010/08/15/script-to-list-all-local-admins-your-domain/#comments</comments>
		<pubDate>Sun, 15 Aug 2010 19:39:39 +0000</pubDate>
		<dc:creator>ben</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Windows Servers]]></category>
		<category><![CDATA[Active Directory]]></category>
		<category><![CDATA[Bash scripting]]></category>
		<category><![CDATA[VBScript]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://ukstokes.com/blog/?p=705</guid>
		<description><![CDATA[Here's a script to create a report of all local administrators on your domain. It gets the computer names from AD and sends a ping to each computer, and if it gets a reply it will interrogate the local administrators group using WMI to get the list of members. The Domain Admins group is ignored. [...]]]></description>
			<content:encoded><![CDATA[<p>Here's a script to create a report of all local administrators on your domain. It gets the computer names from AD and sends a ping to each computer, and if it gets a reply it will interrogate the local administrators group using WMI to get the list of members. The Domain Admins group is ignored. Run at a sensible time when most computers will be turned on.</p>
<p>Edit the SMTP server and strSender values to something appropriate. Also edit line 37 with the name of your domain. You will need to create the folder C:\scripts for this to work, or edit line 8 with a new location for the csv file. </p>
<p>The outputted file is a bit messy, but gets the job done. </p>
<div>
<pre class="brush: vb; title: ; notranslate">
SMTPServer = &quot;mail.yourdomain.corp&quot;
strSender = &quot;name@yourdomain.corp&quot;
strRecipient = InputBox(&quot;Enter the email address for report or&quot; &amp; vbcrlf &amp; &quot;press cancel to just generate a local file.&quot;, &quot;Input required&quot;)
Const ForAppending = 8
Set WshNetwork = WScript.CreateObject(&quot;WScript.Network&quot;)
Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set WshShell = CreateObject(&quot;WScript.Shell&quot;)
strFileName = &quot;C:\scripts\LocalAdminsReport.csv&quot;

If objFSO.FileExists(strFileName) Then
	objFSO.DeleteFile(strFileName)
End If		

Set objFile = objFSO.OpenTextFile(strFileName, ForAppending, True)
objFile.WriteLine &quot;ComputerName,Administrators&quot;

GetLocalAdmins 

msgbox Counter &amp; &quot; computers were counted.&quot; &amp; vbcrlf &amp; &quot;See &quot; &amp; strFileName &amp; &quot; for details.&quot;
If strRecipient = False then
	'user didn't enter an email address
	wscript.quit
Else
	SendEmail
End If

Private Function GetLocalAdmins
	Const ADS_SCOPE_SUBTREE = 2

	Set objConnection = CreateObject(&quot;ADODB.Connection&quot;)
	Set objCommand =   CreateObject(&quot;ADODB.Command&quot;)
	objConnection.Provider = &quot;ADsDSOObject&quot;
	objConnection.Open &quot;Active Directory Provider&quot;

	Set objCOmmand.ActiveConnection = objConnection
	objCommand.CommandText = &quot;Select Name from 'LDAP://DC=yourdomain,DC=corp' &quot; &amp; &quot;Where objectClass='computer'&quot;
	objCommand.Properties(&quot;Page Size&quot;) = 1000
	objCommand.Properties(&quot;Searchscope&quot;) = ADS_SCOPE_SUBTREE
	Set objRecordSet = objCommand.Execute
	objRecordSet.MoveFirst

	Do Until objRecordSet.EOF
		name = objRecordSet.Fields(&quot;Name&quot;).Value
		PINGFlag = Not CBool(WshShell.run(&quot;ping -w 500 -n 1 &quot; &amp; name,0,True))
		If PINGFlag = False Then
			objFile.WriteLine name &amp; &quot;,Did Not Ping&quot;
			Else
				'Get the local administrators
				Set objGroup = GetObject(&quot;WinNT://&quot; &amp; name &amp; &quot;/Administrators,group&quot;)
				For Each objMember In objGroup.Members
					If objMember.Name &lt;&gt; &quot;Administrator&quot; and objMember.Name &lt;&gt; &quot;Domain Admins&quot; Then
						objFile.WriteLine name &amp; &quot;,&quot; &amp; (objMember.Name)
					End If
				Next
		End If
		objRecordSet.MoveNext
	Loop
End Function

Private Function SendEmail
	Set objEmail = CreateObject(&quot;CDO.Message&quot;)
	objEmail.From = strSender
	objEmail.To = strRecipient
	objEmail.Subject = &quot;Local Admins Account&quot;
	objEmail.Textbody = Counter &amp; &quot; computers were counted. See attached log file for details.&quot;
	objEmail.AddAttachment(strFileName)
	objEmail.Configuration.Fields.Item (&quot;http://schemas.microsoft.com/cdo/configuration/sendusing&quot;) = 2
	objEmail.Configuration.Fields.Item (&quot;http://schemas.microsoft.com/cdo/configuration/smtpserver&quot;) = SMTPServer
	objEmail.Configuration.Fields.Item (&quot;http://schemas.microsoft.com/cdo/configuration/smtpserverport&quot;) = 25
	objEmail.Configuration.Fields.Update
	objEmail.Send
End Function
</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://ukstokes.com/blog/2010/08/15/script-to-list-all-local-admins-your-domain/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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; title: ; notranslate">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; title: ; notranslate">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; title: ; notranslate">#!/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>

