COM pt 3–More shell
Last time we looked at the Environment parameterized property
If you ran the script in part two the output would have contained things like
windir=%SystemRoot%
We need to be able to resolve the variable %SystemRoot% part to the full path
| 001 002 003 004 005 006 007 008 009 010 011 012 013 014
| $wshell = New-Object -ComObject "WScript.Shell" "System", "User", "Process", "Volatile" | foreach { "`n$($_)" $envars = $wshell.Environment($($_)) foreach ($envar in $envars){ if ($envar -match "%.*%"){ $s = ($envar -split "%")[1] $exp = $wshell.ExpandEnvironmentStrings("%$s%") $envar.Replace("%$s%", $exp) } else {$envar } } } |
We can take the script from part 2 and modify it slightly. Once we have the environmental variables for each area we loop through them and find those that contain the pattern “%.%”. This is a simple regular expression (I only do simple regex. Maybe I’ll get more adventurous after the March UG group when we have a guest speaker on regex) that looks for two % signs separated by some characters.
If we find that we split on the % and take the second element – push it through the ExpandEnvironemtStrings method and plus the result back into the environmental variable.