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
"
}
}