I haven’t actually written the full script yet, but the key to achieving this falls into the following code snippets:
$strCurrentUserName = (Get-WmiObject -class win32_process -ComputerName 'localhost' | Where-Object name -Match explorer).getowner().user
This returns the currently logged on user’s name (this may go awry if there is more than one user logged on but it’s rare). It might make sense to check for this scenario and abort the script if it’s detected.
(Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain
This checks if the machine is in the domain. Not really necessary since the Automox agent is only installed on domain-joined computers in my case.
$strDNSDomainName = ((Get-WmiObject -Class Win32_ComputerSystem).Domain).ToUpper()
Returns the DNS name of the domain the computer is joined to. This is just an additional check to ensure the computer the Automox agent is running on is a corporate computer.
(Get-WmiObject -ComputerName localhost Win32_UserProfile) | select localpath
This allows allows me to dynamically generate the user profile path. It’s quite easy to iterate through all the paths returned and figure out which belongs to $strCurrentUserName