Jump to content
level42

Setting the Pulseway Computer name to the Locally logged in username

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Try looking at the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\LastUsedUsername

-Paul

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

@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

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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
}

 

 

 

 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...