My digital notepad RSS 2.0
 Tuesday, March 27, 2007

For some idiotic reason (marketing) several server manufacturers insist on putting a self-promoting default wallpaper on the Windows installation making a remote desktop connection unnessecarily slow for the initial connect. Run the attached .reg file to remove the wallpaper for the default user. If you don't trust files downloaded from the Internet, copy the following text, paste into an empty notepad document, save as "RemoveWallpaper.reg" (or something similar) and run the file (double-click it).

Windows Registry Editor Version 5.00

[HKEY_USERS\.DEFAULT\Control Panel\Desktop]
"Wallpaper"=""

[HKEY_CURRENT_USER\Control Panel\Desktop]
"Wallpaper"=""

RemoveWallpaper.reg (,32 KB)

 
Tuesday, March 27, 2007 12:55:44 PM (Central Europe Standard Time, UTC+01:00)  #    Comments [0] -
Administration
 Thursday, January 11, 2007

Our primary DNS server changed it's IP yesterday and we had a secondary DNS server with several domains that needed to have the IP for the master server updated. Windows' gift and curse is that you don't have a bunch of text files where you can find everything but manually opening the properties of every domain, removing the previous master IP, adding a new IP and moving on to the next domain was just not an option.

That's why we have Windows Management Instrumentation (WMI). For an administrator it's the best thing that's happened since they started slicing bread. Well, not quite, but if you intend to do some serious administration of Windows servers you'd better look into it. If you don't like looking through a bunch of documentation for figure out what to do, use Scriptomatic 2.0 like I did yesterday. It will generate a basic script for you that can easily be modified.

Now, the main event: a script to update the master server IP for all secondary DNS zones on a Windows computer.

  1. ' This script updates the Master server IP for all secondary zones on the
  2. ' specifed computers.
  3.  
  4. ' Constants for WMI
  5. Const wbemFlagReturnWhenComplete = &H0
  6. Const wbemFlagReturnImmediately = &h10
  7. Const wbemFlagForwardOnly = &h20
  8. Const iDnsSecondary = 2
  9.  
  10.  
  11. ' The array to hold the master servers
  12. ' (there can be multiple master servers)
  13. Dim MasterServersArray(0)
  14. ' Add the master servers to the array. Replace with your own IP
  15. MasterServersArray(0) = "192.168.2.10"
  16.  
  17. ' The address of the old master server, replace with your own IP
  18. ' Used to check the zone and only update those that need it.
  19. Dim strOldMasterServer
  20. strOldMasterServer = "10.0.0.10"
  21. ' The script can do this on multiple computers,
  22. ' just add them to the array
  23. Dim arrComputers
  24. arrComputers = Array("COMPUTERNAME")
  25.  
  26. Dim objWMIService
  27. Dim colItems
  28. Dim objItem
  29.  
  30. For Each strComputer In arrComputers
  31.         WScript.Echo
  32.         WScript.Echo "=========================================="
  33.         WScript.Echo "Computer: " & strComputer
  34.         WScript.Echo "=========================================="
  35.        
  36.         WScript.Echo "Fetching zones..."
  37.         ' Connect to the DNS WMI interface on the current computer
  38.         Set objWMIService = GetObject("winmgmts:\\" & strComputer _
  39.                 & "\root\MicrosoftDNS")
  40.  
  41.         ' Get all zones from the DNS server. We use wbemFlagReturnWhenComplete
  42.         ' to make sure the call completes before returning. If not we often get
  43.         ' errors when looping through the returned collection. If you only want 
  44.         ' some zones to be updated you can filter your query just like any SQL query.
  45.         Set colItems = objWMIService.ExecQuery("SELECT * FROM MicrosoftDNS_Zone", _
  46.                 "WQL", wbemFlagReturnWhenComplete + wbemFlagForwardOnly)
  47.         WScript.Echo "Retrieved zones."
  48.  
  49.         ' Used to hold a return object.
  50.         dim objTmp
  51.  
  52.         For Each objItem In colItems
  53.           ' Only secondary zones have master servers
  54.           if objItem.ZoneType = iDnsSecondary then     
  55.                 ' Get the current master IPs 
  56.                         strMastersIPAddressesArray = Join(objItem.MastersIPAddressesArray, ",")
  57.                        
  58.                         ' Some information
  59.                         WScript.Echo "Name: " & objItem.Name
  60.                         WScript.Echo "MastersIPAddressesArray: " & strMastersIPAddressesArray
  61.  
  62.                         ' We'll only do this in the zones with the old master server
  63.                         ' No need to update zones that don't need it.
  64.                         if strMastersIPAddressesArray = strOldMasterServer then
  65.                           ' ResetMasterIpArray is a function on the WMI object
  66.                           ' that replacesthe current array of Master IPs.
  67.                           ' objTemp receives a refrence to the updated zone.
  68.                           objItem.ResetMasterIpArray MasterServersArray, objTemp
  69.                           ' We don't need it so we set it to nothing
  70.                           ' immediatly to save memory
  71.                           set objTemp = nothing
  72.                           WScript.Echo
  73.                         end If
  74.                         ' Let's just make the secondary zone refresh from
  75.                         ' the master server while we're at it
  76.                         objItem.ForceRefreshOfSecondaryZoneFromMaster()
  77.           end if
  78.         Next
  79. Next
UpdateMasterServerIP.vbs (2,82 KB)
 
Thursday, January 11, 2007 9:39:17 AM (Central Europe Standard Time, UTC+01:00)  #    Comments [0] -
Administration | Scripting
 Friday, January 05, 2007

A somewhat akward title, but I had a hard time finding a better one :)

We have a projects folder. In that folder there is one folder for each project and in each project folder there is a documents folder (among many others). Now, I needed to find the size of each documents folder for each project and ignore the other folders (don't ask me why).

Seeing as there are a few hundred projects and each documents folder can contain from five to several thousand files I did not want to check each folder. Since the other folders are even larger, listing the size of everything and take out what I did not need would increase the time to create the listing.

So I created a PowerShell script. Was that the fastest way to do it? Probably not, but I'll have to do this again someday and then I'll save lots of time. Besides, it didn't take all that long to create.

$hashTbl=@{};
Get-ChildItem | where-object {$_.Mode -eq "d----"} | `
 foreach-object  { `
   $_.Name;`
  $hashTbl[$_] = ( get-childitem($_.Name + "\documents")`
-recurse -force| `
   measure-object length -sum )
  };
$output = ($hashTbl.psbase.keys |`
 foreach `
  {"Name,Size,File count"} `
  { $_.Name + "," + $hashTbl[$_].Sum + ","`
+ $hashTbl[$_].Count } `
 );
New-Item c:\temp\DirectorySize.csv -type file -force | out-null;
Add-Content c:\temp\DirectorySize.csv  $output;
import-csv c:\temp\DirectorySize.csv;

Since some might wonder what the heck I'm doing here (like myself, six months from now) I thought I'd go through it line by line.

Instansiate an empty hash table

$hashTbl=@{};

Get a list of all children in the current directory and use where-object to get only the objects that have the directory attibute i.e. just directories and not files.

Get-ChildItem | where-object {$_.Mode -eq "d----"} | `

The result from here is piped into a foreach-object that will do an operation for each object.

 foreach-object  { `

In this operation I display the Name of the folder. This will be outputted while the script is running, giving me feedback on progress. The ; seperates commands.

   $_.Name;`

The next command might need a bit more explaining.

  $hashTbl[$_] = ( get-childitem($_.Name + "\documents")`
-recurse -force| `
   measure-object length -sum )

From the inside and out: First we use get-childitem for the documents folder. $_ is an auto-variable from the foreach and contains the current object in the loop. $_.Name is the name of that folder. Add "\documents" to that to look at the sub-folder I want. -recurse gets the children of all sub folders and -force includes hidden files.

get-childitem($_.Name + "\documents") -recurse -force 

Then we pipe that into measure-object and tells it to sum the length property of all the items returned from get-childitem.

get-childitem($_.Name + "\documents") -recurse -force| `
   measure-object length -sum )

That creates a GenericMeasureInfo object that I put into the hashtable for later.

$hashTbl[$_] = ( get-childitem($_.Name + "\documents")`
-recurse -force| `
   measure-object length -sum )

This loops though all the keys in the hashtable and outputs a string with the values comma-separated. The {"Name,Size,File count"} is the before statement in the foreach that executes before the loop, outputting the headers for my comma-separated list. The ( ) around everything makes it a list that I can save in $output.

$output = ($hashTbl.psbase.keys |`
 foreach `
  {"Name,Size,File count"} `
  { $_.Name + "," + $hashTbl[$_].Sum + ","`
+ $hashTbl[$_].Count } `
 );

Create a new file, overwriting any old file. Output is supressed.

New-Item c:\temp\DirectorySize.csv -type file -force | out-null;

Add the comma-separated list to the file.

Add-Content c:\temp\DirectorySize.csv  $output; 

Read it and display it for information purposes only.

import-csv c:\temp\DirectorySize.csv;

There, all done. Any questions?

subSubDirSize.ps1 (,51 KB)
 
Friday, January 05, 2007 3:00:02 PM (Central Europe Standard Time, UTC+01:00)  #    Comments [3] -
Administration | Powershell | Scripting
Links
Twitter updates
    Archive
    <March 2010>
    SunMonTueWedThuFriSat
    28123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910
    About the author/Disclaimer

    Disclaimer
    The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

    © Copyright 2010
    Glenn F. Henriksen
    Sign In
    Statistics
    Total Posts: 48
    This Year: 0
    This Month: 0
    This Week: 0
    Comments: 31
    All Content © 2010, Glenn F. Henriksen
    DasBlog theme 'Business' created by Christoph De Baene (delarou)