Kaseya Community

Edit a ps1 variable prior to executing script

  • I have powershell script that will change a machine name using the "New Name" variable within the script. Is there a way to script it into Kaseya to edit the variable each time? Rather than manually edit the ps1 and re-upload it every single time? I tried adding all the contents into a "executeshell" and "executepowershell", but it wont save as I assume you must use a ps1 and point the script to that location.

    $ComputerName = "New Name"
    Remove-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" -name "Hostname"
    Remove-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" -name "NV Hostname"
    New-PSDrive -name HKU -PSProvider "Registry" -Root "HKEY_USERS"
    Set-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\Computername\Computername" -name "Computername" -value $ComputerName
    Set-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\Computername\ActiveComputername" -name "Computername" -value $ComputerName
    Set-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" -name "Hostname" -value $ComputerName
    Set-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" -name "NV Hostname" -value $ComputerName
    Set-ItemProperty -path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -name "AltDefaultDomainName" -value $ComputerName
    Set-ItemProperty -path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -name "DefaultDomainName" -value $ComputerName
    #Set-ItemProperty -path "HKU:\.Default\Software\Microsoft\Windows Media\WMSDK\General" -name "Computername" -value $ComputerName
  • Create a ps1 file, such as Rename-Computer.ps1, out of your commands and add the following lines at the beginning:

    [cmdletbinding()]

    Param (

       [String]$ComputerName

    )

    Then call it from a PowerShell prompt like this:

    .\Rename-Computer.ps1 -ComputerName 'NewName'

    From Kaseya, you first step should be getVariable(Prompt User, 'Enter New Computer Name', NewName).

    Then, I would do executeShellCommand(PowerShell.exe -executionpolicy bypass -file .\Rename-Computer.ps1 -ComputerName '#NewName#'

    I would also look into the built-in cmdlet Rename-Computer:

    docs.microsoft.com/.../rename-computer

  • Awesome, I will check this out and see if I can get it to work with your suggestion. Renaming via cmd was working, but only if a user was logged in. It would be nice to be able to run the script, rename the machine (regardless if logged in or not), then reboot if user logged out, or wait until next reboot. This powershell renames the machine without reboot, but I have not been able to test it properly via script.

  • Just don't make a mistake and rename domain joined pc and than wait for reboot because you will not be able to login if pc  is locked. So rename and immediately reboot when client is not active, after hours or so

  • True, I did not think of this.

    Anyone aware of a script that will change a name WITHOUT a user logged in? Something that would allow the name change regardless if the user is logged in, that could later be modified to reboot if not logged in etc? As stated above, the command via cmd was only working on my test machines if an actual user was logged in. at the login screen, it did not change upon reboot.

    My original post was via powershell that will change the name with NO reboot.

    This here is the one via cmd that works only while logged in, even under run as system. it uses variables but you should get the point.

    wmic computersystem where name="#oldname#" call rename name="#newname#"

  •  User don't have to be logged in to change PC name.

  • We use netdom.exe

    Here's the meaty part of the k-procedure:

    netdom renamecomputer #vMachine.ComputerName# /newname:#NewComputerName# /userd:#domain#\#Lgn# /passwordd:#Psw# /force /reboot>>#LFE#

    It's one of our oldest procs, going back to 6.2 ;)

    works well for us.

  • In regards to powershell, we often build the ps1 file (and other batch files) on the fly in the k-procedure as a workaround

    i.e in the K-Proc:

    Execute shell command: echo [Line1 of ps1 file] >> powershell.ps1

    Execute shell command: echo [Line2 of ps1 file] >>>>powershell.ps1

    Execute shell command: echo [Line3 of ps1 file with #var#] >>>>powershell.ps1

    Execute shell command: echo [Line 4 of ps1 file] >>>>powershell.ps1

    Execute powershell: powershell.ps1

    .

  • I use  wmic and it works great for me.

  • hmm. for some reason mine is not working with wmic. it worked on a test machine yesterday, not it wont fire and stick. It was only working with a test user account logged in as well. So I did some research and found that powershell without the need for reboot, except as you stated earlier it could affect domain users if the reboot is not immediate.Been swamped today but going to start to play around with the suggestions here now to see what i can come up with.

    get variable ...

    get variable ...

    executeshell as user wmic computersystem where name="#oldname#" call rename name="#newname#"

    and do you guys have any of these procedures available to upload and share so I can compare or modify for my environment?

  • Have you tried PowerShell's Rename-Computer commandlet? You could do an executeshellcommand like so:

    powershell.exe -NonInteractive -NoProfile -NoLogo -Command 'Rename-Computer -NewName #NEW_NAME#'

    You could then use a getVariable command before this to prompt the tech for the computer name (NEW_NAME), which gets inserted above. No need to download a script, then do cleanup on the local drive.

    Nate

  • Here it is. it works for me every time.

  • nice and clean!

    my netdom version is very lenghty, and would only confuse

  • I copied script (minus the credentials line, since we dont set this for most clients) and it works great whether logged in or out for WORKGROUP machines. Perfect!

    However, is there anyway to get it working for a Domain joined machine without the (use credentials) line? I tried modifying the script with impersonate user if they are not logged in with no luck. WMIC also seems to not take when manually entering on a domain joined machine.

  • I suspect the problem here is that the Kaseya agent runs under SYSTEM credentials and the SYSTEM account (i.e. the domain's computer account) does not have permissions on the domain to change its name. To change the name of a domain-joined computer you must use an account with domain credentials that has been delegated appropriate permissions.

    Off the top of my head, I don't see a way around this without providing credentials one way or another.