-3

I have a lot of articles about this subject but none of it is understandable.

My request is very simple. I have two part of my codes ; First of all Code 1 should be work and windows should be restarted. After reboot completion, Code 2 should be work. This process should be done silently at background. Powershell version is 4.0 ( Win 2012 R2 )

CODE 1 - This code is changing Computer Primary DNS Suffix.

$computerName = $env:computername

$DNSSuffix = "abc.com"

$oldDNSSuffix = (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\" -Name "NV Domain")."NV Domain"

#Update primary DNS Suffix for FQDN
Set-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\" -Name Domain -Value $DNSSuffix
Set-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\" -Name "NV Domain" -Value $DNSSuffix

#Update DNS Suffix Search List - Win8/2012 and above - if needed
#Set-DnsClientGlobalSetting -SuffixSearchList $oldDNSSuffix,$DNSSuffix

#Update AD's SPN records for machine if part of an AD domain
if ((gwmi win32_computersystem).partofdomain -eq $true) {
     $searchAD = new-object System.DirectoryServices.DirectorySearcher
     $searchAD.filter = "(&(objectCategory=computer)(cn=$($computerName)))"
     $searchADItem = $searchAD.FindAll() | select -first 1
     $adObj= [ADSI] $searchADItem.Path
     $oldadObjSPN = $searchADItem.Properties.serviceprincipalname
     $adObj.Put('serviceprincipalname',($oldadObjSPN -replace $oldDNSSuffix, $DNSSuffix))
     $oldadObjDNS = $searchADItem.Properties.dnsHostName
     $adObj.Put('dnsHostName',($oldadObjDNS -replace $oldDNSSuffix, $DNSSuffix))
     $adObj.setinfo()
     #$adObj.Get('serviceprincipalname')
     #$adObj.Get('dnsHostName')
}

CODE 2 - Installing Terminal Services on this computer

Import-Module RemoteDesktop
Add-WindowsFeature -Name RDS-RD-Server  -IncludeAllSubFeature
Add-WindowsFeature -Name RDS-Licensing  -IncludeAllSubFeature
Legioon
  • 21
  • 2
  • 9
  • 2
    What have you tried to solve this yourself? – arco444 Nov 30 '17 at 12:42
  • [This is probably relevant](https://learn.microsoft.com/en-us/powershell/module/psworkflow/about/about_workflows?view=powershell-5.1) – Mathias R. Jessen Nov 30 '17 at 13:09
  • 1
    This seems like a perfect use case for [Desired State Configuration](https://www.red-gate.com/simple-talk/sysadmin/powershell/powershell-desired-state-configuration-the-basics/). – Bacon Bits Nov 30 '17 at 14:09

2 Answers2

0

If you don’t want to roll your own with log files to check where it left off, you can look into powershell workflows which allows recovery after reboot. See https://technet.microsoft.com/en-us/library/jj574130(v=ws.11).aspx

Ryan McVicar
  • 103
  • 7
0

powershell commands are very complicated, i decided to do this with batch file.

Resume batch script after computer restart

Legioon
  • 21
  • 2
  • 9