#50LineOfCodePowerShellChallenge > Install #Gateway #NAT #Router #DNS #DHCP on a Windows Server

%3CLINGO-SUB%20id%3D%22lingo-sub-1565150%22%20slang%3D%22de-DE%22%3E%2350LineOfCodePowerShellChallenge%20%26gt%3B%20Install%20%23Gateway%20%23NAT%20%23Router%20%23DNS%20%23DHCP%20on%20a%20Windows%20Server%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1565150%22%20slang%3D%22de-DE%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHello%20guys!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20a%20suffered%20%23PowerShell%20challage%20in%20mind.%20I%20name%20this%20the%20%2350LineOfCodePowerShellChallenge.%3C%2FP%3E%3CP%3EThe%20only%203%20rules%20in%20this%20challenge%20are%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3COL%3E%3CLI%3EOne%20line%20of%20code%20are%20not%20longer%20then%20300%20char%3C%2FLI%3E%3CLI%3EYou%20must%20be%20exactly%20by%2050%20lines%20(without%20garbage)%3C%2FLI%3E%3CLI%3EShare%20your%20code%20with%20others%3C%2FLI%3E%3C%2FOL%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20are%20you%20able%20to%20do%20with%2050%20line%20of%20code%20in%20PowerShell%3F%20I%20think%20it%20would%20be%20a%20good%20learning%20experience%20for%20all%20of%20us%20%3A)%3C%2Fimg%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20will%20start%20with%20my%20submission%20with%20a%20%23PowerShellScript%20to%20install%20a%20fully%20functional%20%23Gateway%20%23NAT%20%23Router%20with%20%23DNS%20and%20%23DHCP%20on%20a%20Windows%20Server%20(%23ServerCore%20or%20%23DesktopExperience).%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3ELook%20forward%20to%20seeing%20what%20you%20guys%20come%20up%20with%20and%20don't%20forget%20to%20share%20this!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EFunction%20Customize.Server%20%7BSet-WinUserLanguageList%20-LanguageList%20%22DE-DE%22%20-Force%3B%20Set-TimeZone%20-Id%20%22W.%20Europe%20Standard%20Time%22%3B%20Set-Volume%20-DriveLetter%20C%20-NewFileSystemLabel%20%22SYSTEM%22%7D%0AFunction%20Autostart.PowerShellServerCore%20%7BSet-ItemProperty%20-Path%20'HKLM%3A%5CSOFTWARE%5CMicrosoft%5CWindows%20NT%5CCurrentVersion%5CWinlogon%5C'%20-Name%20Shell%20-Value%20'powershell.exe'%7D%0AFunction%20Set.Pagefile%20%7B%0A%20%20%20%20Get-WmiObject%20Win32_ComputerSystem%20-EnableAllPrivileges%20%7C%20ForEach-Object%20%7B%24_.AutomaticManagedPagefile%20%3D%20%24False%3B%20%24_.Put()%7D%0A%20%20%20%20Get-WmiObject%20-Query%20%22Select%20*%20From%20Win32_PageFileSetting%20Where%20Name%20like%20'%25pagefile.sys'%22%20%7C%20ForEach-Object%20%7B%24_.InitialSize%20%3D%20%222048%22%3B%20%24_.MaximumSize%20%3D%20%222048%22%3B%20%24_.Put()%7D%7D%0AFunction%20Rename.NIC%20%7B%20%20%20%20%0A%20%20%20%20Get-NetAdapter%20%7C%20Where-Object%20%7B%24_.MacAddress%20-eq%20%2200-00-00-00-00-01%22%7D%20%7C%20Rename-NetAdapter%20-NewName%20%22Intern-NIC%22%0A%20%20%20%20Get-NetAdapter%20%7C%20Where-Object%20%7B%24_.MacAddress%20-eq%20%2200-00-00-00-00-02%22%7D%20%7C%20Rename-NetAdapter%20-NewName%20%22Extern-NIC%22%7D%0AFunction%20Set.IPAddress%20%7B%0A%20%20%20%20New-NetIPAddress%20%E2%80%93InterfaceAlias%20%22Intern-NIC%22-AddressFamily%20IPv4%20-IPAddress%20%2210.0.0.1%22%20-PrefixLength%20%2224%22%0A%20%20%20%20Set-DnsClientServerAddress%20%E2%80%93InterfaceAlias%20%22Intern-NIC%22%20-ServerAddresses%20%2210.0.0.1%22%0A%20%20%20%20Set-DnsClient%20%E2%80%93InterfaceAlias%20%22Intern-NIC%22%20-ConnectionSpecificSuffix%20%22my.corp%22%20-RegisterThisConnectionsAddress%20%24true%20-UseSuffixWhenRegistering%20%24True%7D%0AFunction%20Disable.IPv6%20%7BGet-NetAdapter%20%7C%20ForEach-Object%20%7BDisable-NetAdapterBinding%20%E2%80%93InterfaceAlias%20%24_.Name%20%E2%80%93ComponentID%20ms_tcpip6%7D%7D%0AFunction%20Disable.NetBios%20%7BGet-WmiObject%20Win32_NetworkAdapterConfiguration%20%7C%20ForEach-Object%20%7B%24_.SetTCPIPNetBios(2)%7D%7D%0AFunction%20Disable.WINS%20%7BGet-WmiObject%20-List%20Win32_NetworkAdapterConfiguration%20%7C%20ForEach-Object%20%7B%24_.EnableWins(%24false%2C%24false)%7D%7D%0AFunction%20Update.NTP%20%7BW32tm%20%2FConfig%20%2FSyncFromFlags%3AManual%20%2FManualPeerlist%3A%220.de.pool.ntp.org%2C%201.de.pool.ntp.org%2C%202.de.pool.ntp.org%2C%203.de.pool.ntp.org%22%20%2FReliable%3AYES%20%2FUpdate%7D%0AFunction%20Disable.Telemetry%20%7BNew-ItemProperty%20-Type%20DWord%20-Path%20%22HKLM%3A%5CSOFTWARE%5CPolicies%5CMicrosoft%5CWindows%5CDataCollection%22%20-Name%20%22AllowTelemetry%22%20-value%20%220%22%7D%0AFunction%20Install.ServerFeatures%20%7BInstall-WindowsFeature%20-Name%20DNS%2C%20DHCP%2C%20Routing%20-IncludeAllSubFeature%20-IncludeManagementTools%7D%0AFunction%20Rename.Server%20%7BRename-Computer%20-NewName%20%22Gateway%22%20-LocalCredential%20Administrator%20-PassThru%3B%20Restart-Computer%7D%0AFunction%20Config.DNS%20%7B%0A%20%20%20%20Add-DNSServerPrimaryZone%20-Name%20%22my.corp%22%20-ZoneFile%20%22my.corp.DNS%22%20-DynamicUpdate%20NonSecureAndSecure%0A%20%20%20%20Add-DNSServerPrimaryZone%20-NetworkID%20%2210.0.0.0%2F24%22%20-ZoneFile%20%220.0.10.in-addr.arpa.dns%22%0A%20%20%20%20Add-DNSServerForwarder%20-IPAddress%20%221.1.1.1%22%20-PassThru%0A%20%20%20%20Add-DNSServerResourceRecord%20-ZoneName%20%22my.corp%22%20-A%20-Name%20%22Gateway%22%20-AllowUpdateAny%20-IPv4Address%20%2210.0.0.1%22%20-TimeToLive%20%2201%3A00%3A00%22%20-AgeRecord%0A%20%20%20%20Restart-Service%20dns%0A%20%20%20%20Add-DNSServerResourceRecord%20-Name%20%221%22%20-Ptr%20-ZoneName%20%220.0.10.in-addr.arpa%22%20-AllowUpdateAny%20-PtrDomainName%20%22Gateway.my.corp%22%0A%20%20%20%20Set-DnsClientServerAddress%20%E2%80%93InterfaceAlias%20%22Intern-NIC%22%20-ServerAddresses%20%22Extern-NIC%22%7D%0AFunction%20Config.DHCP%20%7B%0A%20%20%20%20Add-DHCPServerv4Scope%20-Name%20%22MyCorp%22%20-StartRange%20%2210.0.0.100%22%20-EndRange%20%2210.0.0.200%22%20-SubnetMask%20%22255.255.255.0%22%20-State%20Active%0A%20%20%20%20Set-DHCPServerv4Scope%20-ScopeId%20%2210.0.0.0%22%20-LeaseDuration%20%221.00%3A00%3A00%22%0A%20%20%20%20Set-DHCPServerv4OptionValue%20-ScopeID%20%2210.0.0.0%22%20-DNSDomain%20%22my.corp%22%20-DNSServer%20%2210.0.0.1%22%20-Router%20%2210.0.0.1%22%0A%20%20%20%20Set-ItemProperty%20-Path%20%22HKLM%3A%5CSOFTWARE%5CMicrosoft%5CServerManager%5CRoles%5C12%22%20%E2%80%93Name%20ConfigurationState%20%E2%80%93Value%202%0A%20%20%20%20Restart-Service%20dhcpserver%7D%0AFunction%20Install.NATRouting%20%7B%0A%20%20%20%20Install-RemoteAccess%20-VpnType%20RoutingOnly%20-PassThru%0A%20%20%20%20Net%20Stop%20remoteAccess%0A%20%20%20%20Netsh%20Routing%20IP%20NAT%20Install%0A%20%20%20%20Netsh%20Routing%20IP%20NAT%20Add%20Interface%20%22Extern-NIC%22%0A%20%20%20%20Netsh%20Routing%20IP%20NAT%20Add%20Interface%20%22Intern-NIC%22%0A%20%20%20%20Netsh%20Routing%20IP%20NAT%20Set%20Interface%20%22Extern-NIC%22%20Mode%3DFull%0A%20%20%20%20Net%20Start%20RemoteAccess%7D%0A%23%20Prerequisites%3A%20%0A%23%201)%20A%20fresh%20installation%20of%20Windows%20Server%20%20(Core%20or%20DesktopExperience).%20Mind%20your%20Admin%20password.%20%0A%23%202)%20A%20fully%20updated%20Server%0A%23%203)%20Make%20the%20needed%20changes%20in%20this%20script%20%20%20%20%0A%23%20Part%20I%3A%20Load%20lines%201%20to%2019%20in%20PowerShell%20and%20then%20execute%20line%2047%0ACustomize.Server%3B%20Autostart.PowerShellServerCore%3B%20Set.Pagefile%3B%20Rename.NIC%3B%20Set.IPAddress%3B%20Disable.IPv6%3B%20Disable.NetBios%3B%20Disable.WINS%3B%20Update.NTP%3B%20Disable.Telemetry%3B%20Install.ServerFeatures%3B%20Rename.Server%0A%23Wait%20for%20Reboot!%0A%23%20Part%20II%3A%20Load%20lines%2020%20to%2041%20in%20PowerSehll%20and%20then%20execute%20line%2050%20%0AConfig.DNS%3B%20Config.DHCP%3B%20Install.NATRouting%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1565150%22%20slang%3D%22de-DE%22%3E%3CLINGO-LABEL%3EWindows%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20Server%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
New Contributor

 

 

Hello guys!

 

I have a litte #PowerShell challage in mind. I name this the #50LineOfCodePowerShellChallenge.

The only 3 rules in this challenge are:

 

  1. One line of code are not longer then 300 char
  2. You must be exactly by 50 lines (without garbage)
  3. Share your code with others

 

What are you able to do with 50 line of code in PowerShell? I think it would be a good learning experience for all of us :)

 

I will start with my submission with a #PowerShellScript to install a fully functional #Gateway #NAT #Router with #DNS and #DHCP on a Windows Server (#ServerCore or #DesktopExperience).

Look forward to seeing what you guys come up with and don't forget to share this!

 

Function Customize.Server {Set-WinUserLanguageList -LanguageList "DE-DE" -Force; Set-TimeZone -Id "W. Europe Standard Time"; Set-Volume -DriveLetter C -NewFileSystemLabel "SYSTEM"}
Function Autostart.PowerShellServerCore {Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\' -Name Shell -Value 'powershell.exe'}
Function Set.Pagefile {
    Get-WmiObject Win32_ComputerSystem -EnableAllPrivileges | ForEach-Object {$_.AutomaticManagedPagefile = $False; $_.Put()}
    Get-WmiObject -Query "Select * From Win32_PageFileSetting Where Name like '%pagefile.sys'" | ForEach-Object {$_.InitialSize = "2048"; $_.MaximumSize = "2048"; $_.Put()}}
Function Rename.NIC {    
    Get-NetAdapter | Where-Object {$_.MacAddress -eq "00-00-00-00-00-01"} | Rename-NetAdapter -NewName "Intern-NIC"
    Get-NetAdapter | Where-Object {$_.MacAddress -eq "00-00-00-00-00-02"} | Rename-NetAdapter -NewName "Extern-NIC"}
Function Set.IPAddress {
    New-NetIPAddress –InterfaceAlias "Intern-NIC"-AddressFamily IPv4 -IPAddress "10.0.0.1" -PrefixLength "24"
    Set-DnsClientServerAddress –InterfaceAlias "Intern-NIC" -ServerAddresses "10.0.0.1"
    Set-DnsClient –InterfaceAlias "Intern-NIC" -ConnectionSpecificSuffix "my.corp" -RegisterThisConnectionsAddress $true -UseSuffixWhenRegistering $True}
Function Disable.IPv6 {Get-NetAdapter | ForEach-Object {Disable-NetAdapterBinding –InterfaceAlias $_.Name –ComponentID ms_tcpip6}}
Function Disable.NetBios {Get-WmiObject Win32_NetworkAdapterConfiguration | ForEach-Object {$_.SetTCPIPNetBios(2)}}
Function Disable.WINS {Get-WmiObject -List Win32_NetworkAdapterConfiguration | ForEach-Object {$_.EnableWins($false,$false)}}
Function Update.NTP {W32tm /Config /SyncFromFlags:Manual /ManualPeerlist:"0.de.pool.ntp.org, 1.de.pool.ntp.org, 2.de.pool.ntp.org, 3.de.pool.ntp.org" /Reliable:YES /Update}
Function Disable.Telemetry {New-ItemProperty -Type DWord -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\DataCollection" -Name "AllowTelemetry" -value "0"}
Function Install.ServerFeatures {Install-WindowsFeature -Name DNS, DHCP, Routing -IncludeAllSubFeature -IncludeManagementTools}
Function Rename.Server {Rename-Computer -NewName "Gateway" -LocalCredential Administrator -PassThru; Restart-Computer}
Function Config.DNS {
    Add-DNSServerPrimaryZone -Name "my.corp" -ZoneFile "my.corp.DNS" -DynamicUpdate NonSecureAndSecure
    Add-DNSServerPrimaryZone -NetworkID "10.0.0.0/24" -ZoneFile "0.0.10.in-addr.arpa.dns"
    Add-DNSServerForwarder -IPAddress "1.1.1.1" -PassThru
    Add-DNSServerResourceRecord -ZoneName "my.corp" -A -Name "Gateway" -AllowUpdateAny -IPv4Address "10.0.0.1" -TimeToLive "01:00:00" -AgeRecord
    Restart-Service dns
    Add-DNSServerResourceRecord -Name "1" -Ptr -ZoneName "0.0.10.in-addr.arpa" -AllowUpdateAny -PtrDomainName "Gateway.my.corp"
    Set-DnsClientServerAddress –InterfaceAlias "Intern-NIC" -ServerAddresses "Extern-NIC"}
Function Config.DHCP {
    Add-DHCPServerv4Scope -Name "MyCorp" -StartRange "10.0.0.100" -EndRange "10.0.0.200" -SubnetMask "255.255.255.0" -State Active
    Set-DHCPServerv4Scope -ScopeId "10.0.0.0" -LeaseDuration "1.00:00:00"
    Set-DHCPServerv4OptionValue -ScopeID "10.0.0.0" -DNSDomain "my.corp" -DNSServer "10.0.0.1" -Router "10.0.0.1"
    Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\ServerManager\Roles\12" –Name ConfigurationState –Value 2
    Restart-Service dhcpserver}
Function Install.NATRouting {
    Install-RemoteAccess -VpnType RoutingOnly -PassThru
    Net Stop remoteAccess
    Netsh Routing IP NAT Install
    Netsh Routing IP NAT Add Interface "Extern-NIC"
    Netsh Routing IP NAT Add Interface "Intern-NIC"
    Netsh Routing IP NAT Set Interface "Extern-NIC" Mode=Full
    Net Start RemoteAccess}
# Prerequisites: 
# 1) A fresh installation of Windows Server  (Core or DesktopExperience). Mind your Admin password. 
# 2) A fully updated Server
# 3) Make the needed changes in this script    
# Part I: Load lines 1 to 19 in PowerShell and then execute line 47
Customize.Server; Autostart.PowerShellServerCore; Set.Pagefile; Rename.NIC; Set.IPAddress; Disable.IPv6; Disable.NetBios; Disable.WINS; Update.NTP; Disable.Telemetry; Install.ServerFeatures; Rename.Server
#Wait for Reboot!
# Part II: Load lines 20 to 41 in PowerSehll and then execute line 50 
Config.DNS; Config.DHCP; Install.NATRouting

 

0 Replies