Jump to content

Update Computer Name with Logged on Username


Quenten Grasso

Recommended Posts

Hey Fellow PW users,

I've created a script which allows the system to change its name to %computername%-Current logged on username, which makes life a little easier when trying to remote to their computers on the remote control tool/Webapp.

To get started, create your script in the script editor, in my example,

Name: Update Computer Name with Logged on Username

Windows/Powershell Code: 

$pwqusers = quser | Select-Object -skip 1
$pwquserfmt = $pwqusers.substring(1,22).Trim() -join ","
Set-ItemProperty -Path "HKLM:\Software\MMSOFT Design\PC Monitor\" -Name ComputerName -Value "$env:computername-$pwquserfmt"

Click Save.

Next we create at new task,

Name: Update Computer Name with Logged on Username

Current Scope -> Windows Workstations

Enable Scheduling -> (I've set it to run once a day) Every weekday (Monday to Friday), starting on 12 August 2018 at 09:00 (Australia/Brisbane)

Scripts tab, Select/add your new script you just created and once that's done click save.

You can now run the task on demand as well which will update all of the PC's in your windows workstations scope to Computer Name-Logged on usernames

This works for RDSH hosts as well however it could get a bit of out hand if you have 5-10+ users logged in so I probably wouldn't recommend it to be used on servers.

I Hope this helps you somehow, these scripts/suggestions come with no warranty and I bear no responsibility if this breaks anything, use at your own risk etc..etc..

Cheers,

Quenten

Edited by Quenten Grasso
Link to comment
Share on other sites

  • 1 year later...

So I wanted the full display name so I have updated your script.

$dom = $env:userdomain
$getusr = quser | Select-Object -skip 1
$usr = $getusr.substring(1,22).Trim() -join ","
$display = ([adsi]"WinNT://$dom/$usr,user").fullname
Set-ItemProperty -Path "HKLM:\Software\MMSOFT Design\PC Monitor\" -Name ComputerName -Value "$env:computername - $display"

 

Link to comment
Share on other sites

  • 5 months later...
  • 3 weeks later...

I had written something similar resently and posted, I tested yours and I don't think your pulling in the last logged in user just machine name as I could not get to run, when you run this as a script in session 0 it does not get the current logged in user, it seems to be pulling in session 0 which is blank  

 

Also, If I may, I suggest you run at at each logon event, this way its always current for the current user on the machine 100% of the time.

Link to comment
Share on other sites

  • 10 months later...
  • 10 months later...
On 3/21/2022 at 7:00 PM, JanSnow said:

Is there a tweak to exempt certain machines (servers and VMs) on this script?

Don't run it against those machines. Create a scope in Pulseway and apply the job / script to that scope and don't include those machines.  

If you don't want to do that, then modify the script and using this.  Change the PCNames to the ones you don't want it to run on, and then yea, it will just exit with a success if it sees those names.  If you are on machines that are all PowerShell 3.0 and above, then you can also use $env:computername -in $Exclusions as well.  -contains is PowerShell 2 compatible.

 

 
$Exclusions = @("PCName1", "PCName2", "PCName3")
 
if ($Exclusions -contains $env:COMPUTERNAME) {
    Exit 0
}
else {
    PLACE REST OF CODE HERE
}
Link to comment
Share on other sites

  • 4 weeks later...

Hi,

I have expanded a bit on this small script, both to do some error handeling and do some logging of changes made.

I have this running on my instace multiple times a day. Hope this can help someone. 

#Check if eventlog source is already created, if not then create
if (![System.Diagnostics.EventLog]::SourceExists("Pulseway Scripting")){
    New-EventLog -LogName Application -Source "Pulseway Scripting"
}

#Set username to blank
$username = ''
#Set username to actual username
$username = $(Get-WMIObject -class Win32_ComputerSystem | select username).username 
$usernamev2 = (((query user)[1]).TrimStart()).SubString(0,21)

#Combine username and text
$usertext = "| $username"

#Fall back to alternative user information if first is empty
if($usertext -eq "| ") {

$usertext = "| $usernamev2"

}

#Collect current value
$curvalue = $(Get-ItemProperty "HKLM:\SOFTWARE\MMSOFT Design\PC Monitor\" -Name ComputerName).Computername

#Check that there is a username
If ($usertext -ne "| ") {
    
    #Collect computername
    $computername = $env:COMPUTERNAME 

    #Combine username and text
    $pulsewayname = "$computername $usertext"


    #Check that current value is not the same as the new one
    if ($curvalue -ne $pulsewayname) {
        
        #Set the new value
        Set-ItemProperty "HKLM:\SOFTWARE\MMSOFT Design\PC Monitor\" -Name ComputerName -Value $pulsewayname

        #Write the changes to event log
        Write-EventLog -LogName Application -Source "Pulseway Scripting" -EventId 1000 -EntryType Information -Message `
        "Event: Pulseway Computername Updated
        Current user: $username
        Computername: $computername
        Changed from: $curvalue
        Changed to: $pulsewayname
        "
    }
}

 

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
×
×
  • Create New...