cant_code Posted June 21, 2023 Posted June 21, 2023 The code runs at start up for newly created vms. The code should be changing the host name of the vm based on what they input, the prompt works but it does not physically set the hostname within windows. It then also prompts them to set a password upon successful input, the prompt works but it doesnt actually set the password. Please help. $WMI = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2") $aItems = $WMI.ExecQuery ("Select Name from Win32_ComputerSystem") $iRandom = Random(100, 999, 1) $hostname = InputBox("Rename PC", "Enter your laptop's 4-digit asset number", "", " M") $newName = "PC" & $hostname & "-VM" & $iRandom For $element In $aItems If @error = 1 Then MsgBox(0, "Error - ", "Error renaming PC") Else MsgBox(0,"Baker Group", "New VM hostname will be " & $newName) $action = $element.Rename($newName) $password = InputBox("Enter new password", "Enter a password to set for this account", "", " M") Run("net user Admin" & $password) FileDelete("C:\Users\User\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\hostname.lnk") Shutdown(6) EndIf Next
rsn Posted June 21, 2023 Posted June 21, 2023 (edited) Quick glance: #RequireAdmin Since those are admin level operations. Also, add a space after Admin: Run("net user Admin " & $password) Edited June 21, 2023 by rsn
water Posted June 21, 2023 Posted June 21, 2023 Add error checking to your script. Means to check the return value or @error and @extended. This tells you that something went wrong and gives you more information about the cause. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
cant_code Posted June 23, 2023 Author Posted June 23, 2023 On 6/21/2023 at 10:54 AM, rsn said: Quick glance: #RequireAdmin Since those are admin level operations. Also, add a space after Admin: Run("net user Admin " & $password) Here's another challenge, i need to run this code at start up and im trying to do it with the windows startup folder. Any way to get around the UAC? I've tried doing a task scheduler shortcut. The shortcut will change the host name but wont change the password when in the startup folder, if i run the task through task scheduler it will work just fine.
rsn Posted June 27, 2023 Posted June 27, 2023 With the task scheduler, you're basically pre-approving anything that runs. If you set the user to system, it should be able to do just about anything you want. As far as getting around the UAC: if you're imaging with SCCM or similar, try disabling UAC late in the task sequence before the last reboot/shutdown. Then as one of the last steps in your script (when it's running at startup), re-enable UAC and force the reboot.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now