Powershell – Automatically run PowerShell script at first boot

powershellscvmmwindows-server-2012

We use Hyper-V + SCVMM to host Windows Server 2012 (R2) virtual machines. Currently our deployment procedure includes a lot of PowerShell commands that we have to copy/paste after the VMM template is deployed. For example, some change the NIC names, add a DNS suffix etc.

Is there a way to aggregate these commands into a PowerShell script and let the script run automatically unattended when the VM is booted for the first time? We cannot already set these properties in the template, because for example not all VMs have the same number of NICs.

Best Answer

Have you looked at creating an OnStart scheduled task?

SCHTASKS.EXE /CREATE /F /SC ONSTART /RU Administrator /RP password!!! /TN "First Boot" /RL HIGHEST /TR "Powershell.exe -ExecutionPolicy Bypass -File C:\FirstBoot\FirstBoot.ps1 "

You do need to have a command at the end of the script to remove this task

SCHTASKS.EXE /DELETE /F /TN "First Boot"