Advertisement
Guest User

LABServerConfig.ps1

a guest
Aug 31st, 2016
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [cmdletbinding()]param()
  2.  
  3. Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
  4.  
  5. $DSCResourceModules = @(
  6.     'xPsDesiredStateConfiguration'
  7.     'xCertificate'
  8.     'xActiveDirectory'
  9.     'xAdcsDeployment'
  10.     'xComputerManagement'
  11.     'xDhcpServer'
  12.     'xNetworking'
  13. )
  14.  
  15. foreach($DSCResourceModule in $DSCResourceModules){
  16.     Install-Module $DSCResourceModule -Force
  17. }
  18.  
  19. foreach($DSCResourceModule in $DSCResourceModules){
  20.     Import-Module $DSCResourceModule
  21. }
  22.  
  23.  
  24. New-Item -Path "$env:ProgramFiles\WindowsPowerShell\DscService\Modules" -ItemType Directory | out-null
  25. New-Item -Path "C:\dsctemp" -ItemType Directory | out-null
  26. Set-location "C:\dsctemp"
  27. Publish-DSCModuleAndMof -Source "c:\dsctemp" -ModuleNameList $DSCResrouceModules
  28.    
  29.  
  30. [DSCLocalConfigurationManager()]
  31. configuration LCMConfig
  32. {
  33.     Node localhost
  34.     {
  35.         settings
  36.         {
  37.             ActionAfterReboot = 'ContinueConfiguration'
  38.             ConfigurationMode = 'ApplyOnly'
  39.             RebootNodeIfNeeded = $true
  40.         }
  41.     }
  42. }
  43. LCMConfig
  44. Set-DscLocalConfigurationManager -ComputerName localhost -Force -Verbose -path .\LCMConfig
  45.  
  46.  
  47. configuration DSCLabServer
  48. {
  49.  
  50.    
  51.     Import-DscResource –ModuleName PSDesiredStateConfiguration
  52.     Import-DSCResource -ModuleName xPSDesiredStateConfiguration
  53.     Import-DSCResource -ModuleName xCertificate
  54.     Import-DSCResource -ModuleName xActiveDirectory
  55.     Import-DSCResource -ModuleName xAdcsDeployment
  56.     Import-DscResource -ModuleName xComputerManagement
  57.     Import-DSCResource -ModuleName xDhcpServer
  58.     Import-DSCResource -ModuleName xNetworking
  59.  
  60.     Node $AllNodes.Where{$_.Role -eq "PrimaryServer"}.Nodename
  61.     {
  62.        
  63.         User Administrator
  64.         {
  65.             UserName = "Administrator"
  66.             Password = $Node.DomainCred
  67.             Ensure = 'Present'
  68.         }
  69.         WindowsFeature DSCServiceFeature
  70.         {
  71.             Ensure = 'Present'
  72.             Name   = 'DSC-Service'            
  73.         }
  74.         WindowsFeature ServerGuiMgmtInfra
  75.         {
  76.             Ensure = 'Present'
  77.             Name   = 'Server-Gui-Mgmt-Infra'            
  78.         }
  79.         WindowsFeature ServerGuiShell
  80.         {
  81.             Ensure = 'Present'
  82.             Name   = 'Server-Gui-Shell'
  83.             IncludeAllSubFeature = $true            
  84.         }      
  85.         $IPIndex = 0
  86.         foreach($IP in $Node.IPs){
  87.             $IPIndex++
  88.             xIPAddress "NewIPAddress$IPindex"
  89.             {
  90.                 IPAddress      = $IP.IPAddress
  91.                 InterfaceAlias = $IP.InterfaceAlias
  92.                 SubnetMask     = $IP.SubnetMask
  93.                 AddressFamily  = $IP.AddressFamily
  94.                
  95.             }
  96.             xDefaultGatewayAddress "DefaultGatewayAddress$IPIndex"
  97.             {
  98.                 Address = $IP.DefaultGateway
  99.                 AddressFamily  = $IP.AddressFamily
  100.                 InterfaceAlias = $IP.InterfaceAlias
  101.  
  102.             }
  103.             xDnsServerAddress "DnsServerAddress$IPIndex"
  104.             {
  105.                 Address        = $IP.DNSServer
  106.                 InterfaceAlias = $IP.InterfaceAlias
  107.                 AddressFamily  = $IP.AddressFamily
  108.             }            
  109.         }
  110.         xComputer NewComputerName
  111.         {
  112.             Name = $Node.ComputerName
  113.             DependsOn = "[xIPAddress]NewIPAddress$IPindex"
  114.         }
  115.         File ADDatabasePath          
  116.         {            
  117.             DestinationPath = $Node.ADDatabasePath            
  118.             Type = 'Directory'            
  119.             Ensure = 'Present'            
  120.         }
  121.         File ADLogPath
  122.         {            
  123.             DestinationPath = $Node.ADLogPath          
  124.             Type = 'Directory'            
  125.             Ensure = 'Present'            
  126.         }
  127.         WindowsFeature ADDSInstall            
  128.         {            
  129.             Ensure = "Present"            
  130.             Name = "AD-Domain-Services"            
  131.         }            
  132.         WindowsFeature RSATRoleTools            
  133.         {            
  134.             Ensure = "Present"
  135.             Name = "RSAT-Role-Tools"
  136.             IncludeAllSubFeature = $true
  137.         }
  138.         xADDomain PromoteDC          
  139.         {            
  140.             DomainName = $Node.DomainName
  141.             DomainNetBIOSName= $Node.DomainNetBIOSName
  142.             DomainAdministratorCredential = $Node.DomainCred
  143.             SafemodeAdministratorPassword = $Node.SafemodeAdministratorPassword          
  144.             DatabasePath = $Node.ADDatabasePath            
  145.             LogPath = $Node.ADLogPath            
  146.             DependsOn = "[WindowsFeature]ADDSInstall","[File]ADDatabasePath","[File]ADLogPath","[xComputer]NewComputerName","[User]Administrator"
  147.         }
  148.         xDscWebService PSDSCPullServer
  149.         {
  150.             Ensure = 'Present'
  151.             EndpointName = 'PSDSCPullServer'
  152.             Port = 8080
  153.             PhysicalPath = "$env:SystemDrive\inetpub\PSDSCPullServer"
  154.             CertificateThumbPrint = 'AllowUnencryptedTraffic'
  155.             ModulePath = "$env:PROGRAMFILES\WindowsPowerShell\DscService\Modules"
  156.             ConfigurationPath = "$env:PROGRAMFILES\WindowsPowerShell\DscService\Configuration"
  157.             State = 'Started'
  158.             DependsOn = '[WindowsFeature]DSCServiceFeature'                        
  159.         }
  160.         File RegistrationKeyFile
  161.         {
  162.             Ensure          = 'Present'
  163.             Type            = 'File'
  164.             DestinationPath = "$env:ProgramFiles\WindowsPowerShell\DscService\RegistrationKeys.txt"
  165.             Contents        = $Node.DSCRegistrationKey
  166.         }
  167.         WindowsFeature DHCP {
  168.             DependsOn = "[xComputer]NewComputerName"
  169.             Name = 'DHCP'
  170.             Ensure = 'PRESENT'
  171.             IncludeAllSubFeature = $true
  172.         }
  173.         xDhcpServerAuthorization LocalServerActivation
  174.         {
  175.             Ensure = 'Present'
  176.             DependsOn = '[xADDomain]PromoteDC','[WindowsFeature]DHCP'
  177.         }
  178.         xDhcpServerScope Scope
  179.         {
  180.             DependsOn = '[xDhcpServerAuthorization]LocalServerActivation'
  181.             Ensure = 'Present'
  182.             IPEndRange = $Node.DHCPIPEndRange
  183.             IPStartRange = $Node.DHCPIPStartRange
  184.             Name = $Node.DHCPName
  185.             SubnetMask = $Node.DHCPSubnetMask
  186.             LeaseDuration = $Node.DHCPLeaseDuration
  187.             State = $Node.DHCPState
  188.             AddressFamily = $Node.DHCPAddressFamily
  189.         }        
  190.         xDhcpServerOption Option
  191.         {
  192.             Ensure = 'Present'
  193.             ScopeID = $Node.DHCPScopeID
  194.             DnsDomain = $Node.DomainName
  195.             DnsServerIPAddress = $Node.DHCPDnsServerIPAddress
  196.             AddressFamily = $Node.DHCPAddressFamily
  197.             Router = $Node.DHCPRouter
  198.             DependsOn = '[xDhcpServerScope]Scope'
  199.         }
  200.         WindowsFeature ADCSCertAuthority
  201.         {
  202.                Ensure = 'Present'
  203.                Name = 'ADCS-Cert-Authority'
  204.         }        
  205.         xADCSCertificationAuthority ADCS
  206.         {
  207.             Ensure = 'Present'
  208.             Credential = $Node.DomainCred
  209.             CAType = 'EnterpriseRootCA'
  210.             DependsOn = '[WindowsFeature]ADCSCertAuthority','[xADDomain]PromoteDC'
  211.             CACommonName = "$($Node.DomainNetBIOSName) Root CA"
  212.             HashAlgorithmName = "SHA256"
  213.             KeyLength = 4096
  214.             ValidityPeriod = "Years"
  215.             ValidityPeriodUnits = 20
  216.  
  217.         }
  218.         WindowsFeature ADCSWebEnrollment
  219.         {
  220.             Ensure = 'Present'
  221.             Name = 'ADCS-Web-Enrollment'
  222.             DependsOn = '[WindowsFeature]ADCSCertAuthority'
  223.         }
  224.         xADCSWebEnrollment CertSrv
  225.         {
  226.             Ensure = 'Present'
  227.             IsSingleInstance = 'Yes'
  228.             Credential = $Node.DomainCred
  229.             DependsOn = '[WindowsFeature]ADCSWebEnrollment','[xADCSCertificationAuthority]ADCS'
  230.         }
  231.     }
  232. }
  233.  
  234. $password = "Test@123!" | ConvertTo-SecureString -asPlainText -Force
  235. $DomainCred = New-Object System.Management.Automation.PSCredential("ADATUM\administrator",$password)
  236. $SafemodeAdministratorPassword = New-Object System.Management.Automation.PSCredential("administrator",$password)
  237.  
  238. $ConfigData = 'a'
  239.  
  240. $ConfigData = @{            
  241.     AllNodes = @(            
  242.         @{            
  243.             Nodename = "localhost"
  244.             PSDscAllowDomainUser = $true
  245.             PSDscAllowPlainTextPassword = $true
  246.             ComputerName = 'LABDC01'
  247.             Role = "PrimaryServer"
  248.             DomainName = "adatum.com"
  249.             DomainNetBIOSName = "ADATUM"
  250.             ADDatabasePath = "C:\NTDS"
  251.             ADLogPath = "C:\NTDS\LOG"
  252.             DomainCred = $DomainCred
  253.             SafemodeAdministratorPassword = $SafemodeAdministratorPassword
  254.             IPs = @(
  255.                 @{            
  256.                     IPAddress = '192.168.0.2'
  257.                     SubnetMask = 24
  258.                     AddressFamily = 'IPv4'
  259.                     InterfaceAlias = 'Ethernet'
  260.                     DNSServer = '192.168.0.2'
  261.                     DefaultGateway = '192.168.0.1'
  262.                 }
  263.             )            
  264.             DHCPScopeID = '192.168.0.0'
  265.             DHCPIPStartRange = '192.168.0.3'
  266.             DHCPIPEndRange = '192.168.0.254'            
  267.             DHCPName = '192.168.0.0'
  268.             DHCPSubnetMask = '255.255.255.0'
  269.             DHCPLeaseDuration = '00:08:00'
  270.             DHCPState = 'Active'
  271.             DHCPAddressFamily = 'IPv4'
  272.             DHCPDnsServerIPAddress = '192.168.0.2'
  273.             DHCPRouter = '192.168.0.1'
  274.             RetryCount = 20              
  275.             RetryIntervalSec = 30
  276.             DSCRegistrationKey = "$([guid]::NewGuid())"          
  277.         }            
  278.     )            
  279. }
  280.  
  281. DSCLabServer -ConfigurationData $ConfigData
  282. Start-DscConfiguration -ComputerName localhost -Wait -Force -Verbose -path .\DSCLabServer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement