level42 Posted February 4, 2020 Posted February 4, 2020 (edited) So, I have this: Set-ItemProperty -Path "HKLM:\Software\MMSOFT Design\PC Monitor\" -Name ComputerName -Value "$env:UserName" Which works great from the local machine via PowerShell, However, when I try to run this as a script/task, the name just shows up as "HOSTNAME$" Any ideas why? Edited February 4, 2020 by level42
Administrators Paul Posted February 5, 2020 Administrators Posted February 5, 2020 Hi @level42, Pulseway runs these commands as the local system user (as it's a Windows Service). When you run the powershell command locally it runs in the user session which shows correctly your username. Kind in mind that Pulseway runs even if there is no logged in user. -Paul
level42 Posted February 5, 2020 Author Posted February 5, 2020 Is there then another way where I can extract the locally logged in user and assign that to the PulseWay name? The few examples I found were all for domain accounts.
Administrators Paul Posted February 5, 2020 Administrators Posted February 5, 2020 Try looking at the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\LastUsedUsername -Paul
level42 Posted February 5, 2020 Author Posted February 5, 2020 (edited) Hi Paul, I'm still not sure I understand where these commands are being executed. To me, they seem to be executing on the target local machine, because it is able to successfully "SET" the username, however, it can't seem to read the currently logged in user. I also tested this, but the result is, it simply sets the Pulseway name to the Hostname ... $key = 'HKCU:\Volatile Environment' $UserName = (Get-ItemProperty -Path $key -Name USERNAME).USERNAME Set-ItemProperty -Path "HKLM:\Software\MMSOFT Design\PC Monitor\" -Name ComputerName -Value "$UserName" Again, this works from the local workstation, but not as a script. Is there a way to debug the scripts in Pulseway, and get output directly in Pulseway, as opposed to just plugging in the script and running it to see results? Edited February 5, 2020 by level42
Administrators Paul Posted February 5, 2020 Administrators Posted February 5, 2020 @level42, Pulseway runs scripts as the SYSTEM account. HKEY Current User will be different for the SYSTEM account compared with the HKEY Current User for a logged in user. Try running powershell as the SYSTEM account using psexec and you'll be able to run scripts exactly as the Pulseway service runs them. -Paul
level42 Posted February 5, 2020 Author Posted February 5, 2020 Ok, that makes more sense. In that case I needed to take advantage of WMI. My final code is this, which works great! $temp = Get-WmiObject -Class Win32_ComputerSystem | Select-Object -ExpandProperty username $UserName = $temp -replace ".*\\", "" Set-ItemProperty -Path "HKLM:\Software\MMSOFT Design\PC Monitor\" -Name ComputerName -Value "$UserName" Thanks for the help!
eDecisions Posted May 16, 2020 Posted May 16, 2020 Try this one on for size, this give you "currentuser / computername / domain" $lastlogon = (New-Object -ComObject WScript.Shell).RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\LastLoggedOnUser") $hostname = hostname $pos = $lastlogon.IndexOf("\") $leftPart = $lastlogon.Substring(0, $pos) $lastlogonusername = $lastlogon.Substring($pos+1) Set-ItemProperty -Path "HKLM:\Software\MMSOFT Design\PC Monitor\" -Name ComputerName -Value $lastlogonusername , "/" , $hostname , "/" , $leftPart
eDecisions Posted June 2, 2020 Posted June 2, 2020 Ok, after using this script for the last few weeks I realized it was missing just one more thing: Issue: Tom and Roger have a desktop computer at home and at the office, often Tom or Roger will call from home and I can quickly pull up which computer's below to roger and tom because my script shows last logged in user, but in this case tom and roger have 2 computers, how do I tell which is home and which is work, sure I could tag them but hell I don't want to open the both devices to find the one thats at tom's home, I want to skip that step and see it on the main search box, hence: Script 1, any time you have a home user you run this script on their pc Set-Itemproperty -path 'HKLM:\SOFTWARE\MMSOFT Design\PC Monitor' -Name 'Location' -value 'Users Remote Device' Script 2: Oooops I taged the pc in error, this will un-do the first tag. Set-Itemproperty -path 'HKLM:\SOFTWARE\MMSOFT Design\PC Monitor' -Name 'Location' -value '' Script 3: This will add the tag, in my case "Users Remote Device" in the list of computers in the search, note also I took off the last logon name for Servers, usually its just me and I only wanted to see the server name without the user name but I could easly add it back in. $Location = (Get-ItemProperty -path 'HKLM:\SOFTWARE\MMSOFT Design\PC Monitor').Location $lastlogon = (New-Object -ComObject WScript.Shell).RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\LastLoggedOnUser") $hostname = hostname $installtype = (New-Object -ComObject WScript.Shell).RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\InstallationType") $pos = $lastlogon.IndexOf("\") $leftPart = $lastlogon.Substring(0, $pos) $lastlogonusername = $lastlogon.Substring($pos+1) if ($installtype -eq "Server") { Set-ItemProperty -Path "HKLM:\Software\MMSOFT Design\PC Monitor\" -Name ComputerName -Value $hostname , "/" , $leftPart }else { Set-ItemProperty -Path "HKLM:\Software\MMSOFT Design\PC Monitor\" -Name ComputerName -Value $lastlogonusername , "/" , $hostname , "/" , $leftPart , "/" , $Location }
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now