I’ve been writing a lot of custom applications lately, many using Powershell and then packaged using Sapien’s Powershell Studio. Something that I’ve had to do a few times lately is provide applications to allow a non-privileged user to manipulate a user account in a different domain. There are a few pieces that go into this, including mounting AD: as a provider and then “cd” to that “drive”. To make all of this context changing work, it requires embedding the RunAs command into the executable to allow the script to run as an admin account from the domain being affected. The problem with that Powershell can not read the current true username from $env:username
. Since it is important to log who is actually making changes to another persons account, this is a problem.
WMI to the rescue. Since Powershell can read WMI, it becomes easy to store the true username in a variable and write it out to a log file.
$trueUserName = $(Get-WMIObject -class Win32_ComputerSystem | Select username).username
Leave a comment