0

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

  • To use just one script, pass a parameter (isContinuation, for example) to the script. In the script, if isContinuation is false, then run the first part. If it's true, run the second part. – Tony Hinkle Aug 09 '16 at 14:11
  • Effective duplicate of http://stackoverflow.com/questions/15166839/powershell-reboot-and-continue-script – Chris Dent Aug 09 '16 at 14:28

1 Answers1

0

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>
Dirk Trilsbeek
  • 5,873
  • 2
  • 25
  • 23
  • Thx for the help! that solved the first part of my question. I've already tried the Winlogon part and it doesn't seem to be working for me. I need to autologin as admin so i used the Username Administrator but I still need to enter my pw after a reboot. Also the registry doesn't seem to save the edits after a reboot. – Dries Van den Bergh Aug 09 '16 at 14:31
  • if your registry settings aren't there after a reboot, they likely are never saved in the first place - and therefore not present, when it's about time to automatically log on. I'd first try to find out why your registry settings don't stick, that may be the whole problem here. – Dirk Trilsbeek Aug 09 '16 at 14:37
  • @DriesVandenBergh Not sure how you are making the registry changes, but if you are using `regedit /s .reg` and then immediately rebooting it doesn't always save the changes. Try `start /wait regedit /s .reg` or do it earlier in the script. – Patrick Meinecke Aug 10 '16 at 02:36