Monthly Archives: January 2012

Hiding parts of html page on printing

If you need to create a web page on which some parts should not be visible when it is printed (or even print-previewed) you can use the following CSS:

@media print
{

.notPrint
{

display:none;

}

}

Then in the html you can specify something like this:

<div class=’notPrint’>This is not visible in printing</div>

This is useful if you have things like buttons and other stuff that is not applicable when printing.

DirectorySearcher and filters to search AD

If you want to do a query against AD (active directory) there are several ways to do it – but (only) one proper way like usual.

The DirectorySearcher class has a ‘filter’ property that allows you to filter the returned results – oppose to you having to test each property in code. The syntax of this filter is a bit tricky (ok a lot…)

It took me a while to figure out how to properly set up a filter to do more complex queries – like when you have multiple things to filter by including ‘and’s and ‘or’s. Anyway, I found a good reference here: Search Filter Syntax.

It explains the syntax plus give some examples. It also list some special characters plus explain how to use ‘wildcards’.

Happy searching…

IP address to table sql function

I have a requirement to do some stuff with a table that contains ip addresses like grouping per subnets etc. Doing this with straight tsql is tricky (if possible at all) so I created a sql server function that takes the ip address field and breaks it down to a set (table) of 4 values. It’s not perfect and brings another type of complexity of its own but at least you can compare the separate bits of the ip address as unique parts. Also, it doesn’t really have any error checking built in.

create FUNCTION [dbo].[ufn_IPAddressToTable]
(

@IpAddress VARCHAR(15)

)
RETURNS @IpTable TABLE (part1 int, part2 int, part3 int, part4 int)
AS
BEGIN

DECLARE @part1 int, @part2 int, @part3 int, @part4 int
IF (not @IpAddress is null)
BEGIN

if (CHARINDEX(‘.’, @IpAddress) > 0)
begin

set @part1 = CONVERT(int, substring(@IpAddress, 0, CHARINDEX(‘.’, @IpAddress)))
set @IpAddress = substring(@IpAddress, CHARINDEX(‘.’, @IpAddress) + 1, 100)

if (CHARINDEX(‘.’, @IpAddress) > 0)
begin

set @part2 = CONVERT(int, substring(@IpAddress, 0, CHARINDEX(‘.’, @IpAddress)))
set @IpAddress = substring(@IpAddress, CHARINDEX(‘.’, @IpAddress) + 1, 100)

if (CHARINDEX(‘.’, @IpAddress) > 0)
begin

set @part3 = CONVERT(int, substring(@IpAddress, 0, CHARINDEX(‘.’, @IpAddress)))
set @IpAddress = substring(@IpAddress, CHARINDEX(‘.’, @IpAddress) + 1, 100)
set @part4 = CONVERT(int, @IpAddress)

end

end

end
if (not(@part1 is null or @part2 is null or @part3 is null or @part4 is null))

insert @IpTable(part1, part2, part3, part4)
values (@part1, @part2, @part3, @part4)

END
RETURN

END

As an example you can use it like this:

declare @IpAddress varchar(15)
set @IpAddress = ‘127.0.0.1’
select * from dbo.ufn_IPAddressToTable(@IpAddress)

Or if you have a table with ip addresses

with ComputerIps(Area, IpPart1, IpPart2, IpPart3)
as
(

select  c.Area,
(select top 1 part1 from dbo.[ufn_IPAddressToTable](c.IpAddress)) as Part1,
(select top 1 part2 from dbo.[ufn_IPAddressToTable](c.IpAddress)) as Part2,
(select top 1 part3 from dbo.[ufn_IPAddressToTable](c.IpAddress)) as Part3
from Computers c
where not (c.IpAddress is null)  and LEN(c.IpAddress) > 0

)
select Area, IpPart1, IpPart2, IpPart3, COUNT(*) as [Computers]
from ComputerIps
group by Area, IpPart1, IpPart2, IpPart3
order by Area, IpPart1, IpPart2, IpPart3

Another problem is that it may be slow when the source table becomes large. At least it helps a bit.

Advanced debugging in VS2010 tips

Just a quick link to an article on codeproject –  Advanced Debugging in Visual Studio

Because you are never to old to learn… or too young!

QuickMon 2.5.4

The first update to one of my tool projects on CodePlex is a QuickMon release. It is actually a very small change – bug fix of a silly little UI bug.

Hopefully I’ll have some time this year to improve the tool further – there are several ideas in the pipeline…

Creating daily log files

In the (wild) old days when the only thing we had to work with computers were the command line we use to store output of important processes to a (text)file so it can be viewed and worked on afterwards. Event today in a support and administration environment these skills are very handy.

The process of doing this was/is called ‘output direction’ or ‘pipe output to’ since the standard output ‘device’ was the console (command window). So taking the application output away from the console meant redirecting it to a ‘file’ – since in a sense the ‘console’ could also be seen as a file that gets stuff outputted to (a temporary one).

Anyway, say you want to save the output of a command line utility like ‘dir’ to a file – you would use the following:

dir *.txt > dir.txt

All this does is execute the normal ‘dir *.txt’ command but store the output to the file ‘dir.txt’. This only outputs the standard ‘console output’ to the text file. If the output file already exists it gets overwritten. Actually there are two types of ‘standard outputs’: 1 – the normal successful and 2 – the standard error output. Using only the ‘>’ operator is just a shortcut for ‘1>’ which only outputs the ‘standard successful’ output i.e. the previous example is the same as:

dir *.txt 1> dir.txt

The second type of output is the ‘standard error’ output. To get this output you use the ‘2>’ operator e.g.

Someexe.exe 2> errors.txt

If you want to combine the outputs – that is both normal and error outputs you can use this:

Someexe.exe > errors.txt 2>&1

Lets say you want to keep a history of all previous outputs – then you want to use the ‘append’ operator ‘>>’:

Someexe.exe >> output.txt

If the file ‘output.txt’ does not exist yet it will be created. Otherwise and new output will simply be appended. If you want to combine all of these – that is combine normal and error ouputs plus append to the output file you can use this:

Someexe.exe >> output.txt 2>&1

Now, say you want to use part of the date when creating, listing etc. files. Unfortunately this tip depends on the date-time format settings of the machine you are running it on. For my example I use the ‘yyyy/mm/dd‘ format. To get the year you can use the following:

set year=%date:~0,4%

To get the month you can use ‘%date:~5,2%’ or day ‘%date:~8,2%’ – again this only works for the ‘yyyy/mm/dd’ format. If your date-time format is different you have to change the ‘~x,y‘ parts where x is ‘from’ and y is ‘to’ character positions.

Now using the example above you can use:

dir %year%*.txt

to file all files with a name that start with the current year (e.g. 2012blablabla.txt)

You can use the same technique when creating output files, e.g.

dir *.txt > %year%List.txt

This way you can create daily log files e.g.

set year=%date:~0,4%
set month=%date:~5,2%
set day=%date:~8,2%
Someexe.exe >> %year%%month%%day%.log

Mission accomplished… for now.

Launch apps as Admin without prompt

This is a tip intended to ease the pain of continuously having to click the ‘Yes’ prompt when you want to launch an app in ‘Admin mode’.

The trick is to use the built-in Windows Task scheduler.

1. First you create a new task (not a basic task) and specify a name e.g. ‘CommandLine’ plus check the ‘Run with highest privileges’ check box.

2. On the actions tab you add a ‘Start a program’ action and select the executable e.g.’C:\Windows\System32\cmd.exe

3. Optionally under the Settings tab you can choose ‘Run a new instance in parallel’so you can run multiple instances of the app if required.

4. Click OK to create the task

Next you create a Windows shortcut (say on the desktop by right clicking and choosing new -> Shortcut)

1. Specify the following as the ‘Target’: e.g.  ‘C:\Windows\System32\schtasks.exe /run /tn CommandLine
Pay attention that the last parameter must match the task name.

2. You may want to manually select an icon for the shortcut as it won’t select the app icon by itself.

Now if you click (double-click) the shortcut a brief command line window before opening your app (in admin mode) – without prompting you! One big disadvantage is that you cannot pass command line parameters this way so launching say notepad with a specified txt file this way won’t work.

Of course, it would have been easier if MS built in a way you can choose (at your own risk) which apps you would like to launch in admin mode without prompting. There is a potential security risk in allowing functionality like that but if they wanted to it could be done with a big warning dialog at the start – that tells you you’re doing this at your own risk. It could use something like signing the required exe with a local (only) certificate to mark it trusted only on the local machine or generating a hash that gets stored in an encrypted system location or something, to tell the OS that particular exe can be trusted (when launched). The way to set up this hash and stuff would need to be a secure process in itself (requiring a ‘human’ user input) and letting you jump through a couple of hoops – once off. The idea is to allow only that particular exe on the particular machine as trusted. Copying it to another machine or changing it would require setting up the ‘trust’ again. Just an idea… (throwing a hint… to Microsoft)

Adding an user to group in AD

I was helping a colleague that has no programming background with a little tool he needs to create to add users to an AD group. The ‘tricky’ part is that he started using VB (.net) and my VB skills is a bit (or a lot) rusty…

So I set out to help him but I hit a little snag with the step of actually adding a user to the group.  I got an exception ‘The server is unwilling to process the request‘ when trying to use the following type of code:

de = new DirectoryEntry(groupDn);
de.Properties[“member”].Add(userDn); //<!– breaks here
de.CommitChanges();
I suspected a security issue but even running this code under raised privileges (admin) does not help. The solution is to rather call the COM add method directly:
de = new DirectoryEntry(groupDn);
de.Invoke(“Add”, New Object() {userDn})
Note: groupDn and userDn must be the full LDAP path to the group and user objects.

A new year – 2012

A new year has come and perhaps some things might be different (if you believe all that Mayan calendar stuff etc.) but mostly it probably would be a case of ‘more of the same’ with few sporadic changes here and there. Sounds like a weather forecast…

On the personal front there is at least Mass effect 3 to look forward to and ‘moa’ hitting the big 4 later the year (like in 40).

On the technology front, hopefully this year would be the (start of) year of Microsoft entering the successful tablet market (lets not mention the previous attempts that went nowhere). I’m still not impressed with the Windows 8 metro interface – for traditional PC’s and the fact that they (Microsoft) is again abandoning their own supporters (.Net and specifically Windows applications). Seems it is the one thing you can count on from them – no matter how much they push something they eventually will drop it and the whole community that supports it.

Apple had a few good years but I suspect that will come to an end soon as there are now too many competitors and mr Jobs is not with us anymore. They have been loosing more and more court cases and their products are not that much more ‘better’ than the rest. However, expect them to hang around for a while as they have become so big that quick failure is not that easy anymore.

Google and the Android/Chrome crowd may reaching new heights and Firefox (and IE) will continue to loose market share. That is a bit of a sour point – ever since Firefox started to do things ‘Chrome’ style they have been chasing the ‘others’ in the market instead of leading by example. I’ll still use Firefox for a while though… IE will probably loose even more ground this year as no one takes it seriously anymore. The bad name that IE6 gave it (plus the anti-trust case remnants) will prevent any new market share growth soon.

If by chance the Mayans had it right then at least we don’t have to worry anymore about global warming, corrupt politicians, no good sci-fi, invasion from Mars or anything remotely as bad. But enough of the moaning and groaning…

Or maybe I must just admit I’m getting old…

 

Elder scrolls world

After being playing Skyrim (and Oblivion/Morrowind before it) for a while I googled a bit to find out about the whole world behind it – The bigger Tamriel or Nim, the whole fictional planet. There is such a massive world (size and story wise) that still needs exploring. I really hope/wish they (Bethesda) plans to have the rest of this wonderful world also made available to play and explore. In case you wish to see a simple map (fictional of course) of the whole place look here.

Apparently in the original Elder scrolls game the whole Tamriel was open to be explorable – but that game is ages old and I never got to play it (actually, I didn’t even know about it back then since it wasn’t the kind of game I would have been interested at the time). This got me thinking – wouldn’t it be wonderful if the whole Tamriel/Nim was playable using the Skyrim level of graphics/game engine. Perhaps this is a bit of wishful thinking as such a game would be huge (in about every way possible including disk space, story, quest count, complexity etc.). Skyrim already pushed the envelope for PC specs but a game like this could go even further. Possibly the whole map will have to come in sections that must be loaded separately as you venture from one region to the next. Crossing the oceans might require some kind of ‘fast travel’ since it would be too easy to get lost as ‘sea’. It is not impossible that there could be isolated islands with its own treasures or adventures. Since it is an fictional world there is even the possibility for an totally underground world as well (aka Middle Earth idea). In Skyrim there has already been something similar – the Falmer/Snow Elves and Dwemer.

A game like that could keep you busy for almost a lifetime in itself. You’d need a separate life to play it… This is probably close to impossible or improbable but still, one could wish and dream.

Oh, and happy new year to all… 🙂

Update: I just thought mentioning that since the original post above I’ve come across some other postings by Skyrim/Elder scroll fans that Skyrim (the game) seems to have maps included for the ‘whole of Tamriel’ – check here. Perhaps Bethesda is planning something ‘bigger’ at some point. Lets hope…
Update 2: There has been rumours and speculation about a totally MMORPG game that is based on the Elders Scrolls. Bethesda has now announced a game like that. Due somewhere in 2013. It will apparently cover the whole of Tamriel and be situated something like a 1000 years before Skyrim. Wonder if they’ll ever consider opening up the whole of Nim or what will happen if you play for too long and use up the 1000 years 😉 ? See this.