Dries Van den Bergh Dries Van den Bergh - 3 months ago 55
PowerShell Question

Powershell resume script after reboot

I am currently working on a school assignment where i have to make a couple of powershell scripts to perform a basic configuration of a windows server 2012 r2. First script needs to do the following:

o Server name: WDC
o Ip address: 192.168.1.45
o Subnet: 255.255.255.0
o Gateway: 192.168.1.1
o DNS: 192.168.1.1

o Administrator password: Admin2016
o Primaire DNS server 8.8.8.8
o Secundaire DNS server 192.168.1.45
o Promote to dc
o ...

Problem is that the server needs to reboot after the name change and that the script cannot continue after this.

I know I could do this by for example splitting up the script and continuing the second part with runonce in the registry after reboot, but my teacher swears that it is possible to do this in one ps1 file and furthermore he says that it should be possible to autologin after the reboot and then resume the same script file from where it stopped before the reboot.

This script has to be executed on a local machine, so I can't use workflows. I have been searching for a significant amount of time but can't seem to find any suitable solution to do this exactly how the teacher wants it.

Hope someone can help.

Cheers

Answer

Resuming as such wouldn't be possible. But you can determine if the changes that need a reboot have already been done and just don't repeat the steps for those settings. Essentially, check if you need to make an adjustment to a step, and only then you need to reboot. During the second run, your script won't need to re-apply the new settings which need a reboot and should be able to effectively continue where it left off in its previous run.

As for the auto-login and script re-run parts, both are solvable. There is a solution using registry settings to auto-logon a user. You'd need to add the password for that login to the registry, so this may be a security concern. And running a script after logon is possible using the Windows task scheduler. In addition to using fixed time schedules the scheduler also supports running tasks after certain events. There is a delay if you use those events, it can be up to several minutes long. If that isn't acceptable, you could also use a link to the script in the StartUp folder of the user account used to automatically log on.

The autologon works with registry keys. You need a few keys in the HKLM namespace:

Path: SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon
Value Name: AutoAdminLogon
Value Type: REG_SZ
Value data: 1

Path: SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon
Value Name: DefaultUserName
Value Type: REG_SZ
Value data: <name of the user you want to log on>

Path: SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon
Value Name: DefaultPassword
Value Type: REG_SZ
Value data: <password of the user you want to log on>

and if this is a domain user

Path: SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon
Value Name: DefaultDomainName
Value Type: REG_SZ
Value data: <name of the domain the user belongs to>
Comments