Jump to content

Setting the Pulseway Computer name to the Locally logged in username


level42
 Share

Recommended Posts

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 by level42
Link to comment
Share on other sites

  • Administrators

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

Link to comment
Share on other sites

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 by level42
Link to comment
Share on other sites

  • Administrators

@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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

  • 3 months later...

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

Link to comment
Share on other sites

  • 3 weeks later...

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
}

 

 

 

 

 

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...