Wednesday, January 11, 2006

MSH having a specific profile for every machine.

I like to have my profile scripts do custom actions for specific computers and as such I have a profile script on every computer at home/work. They are not always in the same location or drive. My home directory at home is a shared drive and mounted via Active Directory. So Monad loads up the same profile script no matter what computer I am on (the one in my shared home drive). I would like to configure it on a computer-by-computer basis to load up from a different location.


Monad will load your profile from a set of predefined locations but it does not allow you to specify where before hand. The solution I've used to get around this is to have my shared Monad script call into my Machine specific script.


Since this is Monad I've of course scripted this solution. Below is the script




# Read in the information about known computers
function ReadComputerMap
{
[string]$file = combine-path $(parse-path -parent $profile) "Computers.txt";[hashtable]$map = @{};
foreach ( $line in get-content $file)
{
if ( $line -match "(?[a-z_0-9\\-]+)\|(?[a-z_\\:]+)" )
{
$map[$Matches["name"]] = $Matches["path"];
}
}
return $map;}

# Write out the computer map passed infunction
WriteComputerMap([hashtable]$map)
{
[string]$file = combine-path $(parse-path -parent $profile) "Computers.txt";
echo "# Generated File (do not edit)" > $file;
foreach ( $name in $map.Keys )
{
$line = "{0}|{1}" -f $name, $map[$name];
echo $line >> $file;
}
}

$newPath = $args[0]
$sfPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
$targetDirPath = "{0}\msh" -f (get-property $sfPath Personal).Personal
$targetPath = combine-path $targetDirPath "Microsoft.Management.Automation.msh_profile.msh";

if ( -not (test-path $targetDirPath) ) { mkdir $targetDirPath > $null }

# Make sure we've visited this computer before and that the profile has
# not been moved
[hashtable]$map = ReadComputerMap;
[string]$compName = "{0}\{1}" -f ${env:\userdomain}, ${env:\computername};
if ( -not ($map.ContainsKey($compName)) -or $newPath -notlike $map[$compName])
{
$map[$compName] = $winconfig;
WriteComputerMap $map;
}

echo "# Generated File (do not edit!!!)" > $targetPath;
echo "switch (`"`${env:\userdomain}\`${env:\computername}`")" >> $targetPath
echo "{" >> $targetPath;
foreach ( $name in $map.Keys )
{
$line = "`"{0}`" {{ . '{1}';break}}" -f $name,$map[$name];
echo $line >> $targetPath;
}

echo "default { echo `"Unknown computer`" }" >> $targetPath;
echo "}" >> $targetPath;
}

0 Comments:

Post a Comment

<< Home