{"id":2009,"date":"2014-02-14T00:01:00","date_gmt":"2014-02-14T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/02\/14\/set-up-a-lab-with-windows-powershell-and-free-microsoft-software-part-5\/"},"modified":"2014-02-14T00:01:00","modified_gmt":"2014-02-14T00:01:00","slug":"set-up-a-lab-with-windows-powershell-and-free-microsoft-software-part-5","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/set-up-a-lab-with-windows-powershell-and-free-microsoft-software-part-5\/","title":{"rendered":"Set Up a Lab with Windows PowerShell and Free Microsoft Software: Part 5"},"content":{"rendered":"<p><b>Summary<\/b>: Preconfigure a virtual machine as a domain controller including Windows PowerShell Desired State Configuration and DHCP.\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\">&nbsp;Hey, Scripting Guy!\nI saw how we could build the virtual machines from a template. Were you teasing us about creating a new domain controller in this lab environment? I&rsquo;d really love to see the script that would configure a domain controller in a lab virtual machine!\n&mdash;RR\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\">&nbsp;Hello RR,\nHonorary Scripting Guy, Sean Kearney, about to release the Kraken of Knowledge. Yes my good friend, with our current setup, we can set up that virtual machine to autostart to the point that we have a domain controller automatically!\n<b>&nbsp; &nbsp; &nbsp;Note<\/b> This post is the last in a five-part series. To catch up, read:&nbsp;<\/p>\n<ul>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2014\/02\/10\/set-up-a-lab-with-windows-powershell-and-free-microsoft-software-part-1.aspx\" target=\"_blank\">Set Up a Lab with Windows PowerShell and Free Microsoft Software: Part 1<\/a><\/li>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2014\/02\/11\/set-up-a-lab-with-windows-powershell-and-free-microsoft-software-part-2.aspx\" target=\"_blank\">Set Up a Lab with Windows PowerShell and Free Microsoft Software: Part 2<\/a><\/li>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2014\/02\/12\/set-up-a-lab-with-windows-powershell-and-free-microsoft-software-part-3.aspx\" target=\"_blank\">Set Up a Lab with Windows PowerShell and Free Microsoft Software: Part 3<\/a><\/li>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2014\/02\/13\/set-up-a-lab-with-windows-powershell-and-free-microsoft-software-part-4.aspx\" target=\"_blank\">Set Up a Lab with Windows PowerShell and Free Microsoft Software: Part 4<\/a><\/li>\n<\/ul>\n<p>At this point, we are creating a Windows PowerShell script. If you remember last time, for part of our set up, we have a Windows PowerShell script with the same name as a virtual machine for automatic setup.\nIn this case, I&rsquo;m going to create a script for a virtual machine called EOT-DC01 for a future domain controller. We&rsquo;re not simply going to spin up the Windows PowerShell cmdlet and create a new Active Directory; we are actually going to prepopulate some server features.\nFirst, our server will need to have a static IP address configured. We&rsquo;re also going to leverage all the new built-in cmdlets in Windows Server 2012 R2 to make this easier. For our lab, we&rsquo;ll use 192.168.1.5, a 24-bit subnet mask (also known as 255.255.255.0), and a gateway of 192.168.1.1.\nWe are using two variables for the subnet mask because:<\/p>\n<ul>\n<li>I&rsquo;m lazy and I don&rsquo;t want to write a cool function to convert the subnet.<\/li>\n<li>We need two types of values for two types of cmdlets (the second are the DHCP cmdlets)<\/li>\n<\/ul>\n<p style=\"margin-left:30px\">$DCIPAddress=&#8221;192.168.1.5&#8243;<\/p>\n<p style=\"margin-left:30px\">$DCGateway=&#8221;192.168.1.1&#8243;<\/p>\n<p style=\"margin-left:30px\">$DCPrefix=24<\/p>\n<p style=\"margin-left:30px\">$DCSubnet=&#8221;255.255.255.0&#8243;\nNext on the list, we&rsquo;ll define our domain name, folders for Active Directory, and a password for the safe-mode recovery for our domain. I fully recognize the password is sitting in clear text, and by all rights, this breaks every security rule! But remember, this is intended as use for lab environment. There are many great techniques for storing the password in a more secure manner if you need to, and you could easily adapt this script to it if you like it.\nWe are going to call our domain <b>Contoso.local<\/b> with an older NetBIOS name of <b>CONTOSO<\/b>. We&rsquo;ll add a super secure password of <b>P@ssw0rd<\/b>.<\/p>\n<p style=\"margin-left:30px\">$DomainName=&#8221;Contoso.local&#8221;<\/p>\n<p style=\"margin-left:30px\">$Netbios=&#8221;CONTOSO&#8221;<\/p>\n<p style=\"margin-left:30px\">$DB=&#8221;C:WindowsNTDS&#8221;<\/p>\n<p style=\"margin-left:30px\">$Log=&#8221;C:WindowsNTDS&#8221;<\/p>\n<p style=\"margin-left:30px\">$Sysvol=&#8221;C:WindowsSysvol&#8221;<\/p>\n<p style=\"margin-left:30px\">$Password=&#8217;P@ssw0rd&#8217;<\/p>\n<p style=\"margin-left:30px\">$SecurePassword=CONVERTTO-SecureString $Password -asplaintext -force\nComing up next is to assign an IP address. To configure an IP address in Windows Server 2012 R2, leverage the <b>Get-NetAdapter<\/b> and <b>New-NetIPAddress<\/b> cmdlets. Because this is a very simple server configuration, we do not need to filter out additional adapters.<\/p>\n<p style=\"margin-left:30px\">Get-NetAdapter | NEW-NetIPAddress -IPAddress $DCIPAddress -Defaultgateway $DCGateway -Prefixlength $DCPrefix\nTo continue on with our domain controller, we need to add in some features. We need the Active Directory Domain Services binaries. I will also preload the bits for a DHCP server and Windows PowerShell Desired State Configuration.<\/p>\n<p style=\"margin-left:30px\">Install-Windowsfeature AD-Domain-Services -includeallsubfeature -IncludeManagementTools<\/p>\n<p style=\"margin-left:30px\">Install-WindowsFeature DHCP -IncludeAllSubFeature -IncludeManagementTools<\/p>\n<p style=\"margin-left:30px\">Install-WindowsFeature DSC-Service -IncludeAllSubFeature -IncludeManagementTools\nNow we&rsquo;re going to predefine the scope for our DHCP server. This will be a simple scope of 100 potential addresses, starting at 192.168.1.100 and ending at 192.168.1.200. We are going to name it <b>Contoso Lab DHCP Scope<\/b>.\nBecause our domain controller will also be the primary DNS server in our lab, we can reuse the server IP address in our DHCP scope configuration:<\/p>\n<p style=\"margin-left:30px\">$ScopeStart=&#8221;192.168.1.100&#8243;<\/p>\n<p style=\"margin-left:30px\">$ScopeEnd=&#8221;192.168.1.200&#8243;<\/p>\n<p style=\"margin-left:30px\">$ScopeSubnet=&#8221;255.255.255.0&#8243;<\/p>\n<p style=\"margin-left:30px\">$ScopeName=&#8221;Contoso Lab DHCP Scope&#8221;\nThen we need only add the cmdlets to create a DHCP scope:<\/p>\n<p style=\"margin-left:30px\">ADD-DhcpserverV4Scope -StartRange $Scopestart -EndRange $ScopeEnd -SubnetMask $ScopeSubnet -Name $ScopeName<\/p>\n<p style=\"margin-left:30px\">SET-DHCPServerV4Optionvalue -OptionID 6 -value $DCIPAddress\nThe following part is taken directly from the Windows Server 2012 R2 wizard for creating a domain controller. I have the variables at the top of script earlier so you can tweak them for your own needs and desires.<\/p>\n<p style=\"margin-left:30px\">Import-Module ADDSDeployment<\/p>\n<p style=\"margin-left:30px\">Install-ADDSForest -CreateDnsDelegation:$false -DatabasePath $DB `<\/p>\n<p style=\"margin-left:30px\">-DomainMode &#8220;Win2012R2&#8221; -DomainName $DomainName -DomainNetbiosName $NETBIOS `<\/p>\n<p style=\"margin-left:30px\">-ForestMode &#8220;Win2012R2&#8221; -InstallDns:$true -LogPath $Log `<\/p>\n<p style=\"margin-left:30px\">-NoRebootOnCompletion:$false -SysvolPath $Sysvol -Force:$true `<\/p>\n<p style=\"margin-left:30px\">-SafeModeAdministratorPassword $SecurePassword\nLet&rsquo;s keep in mind you need to authorize your DHCP server for it to be functional. But the Catch 22 is that you can&rsquo;t authorize it in Active Directory until you have an Active Directory. This won&rsquo;t happen until the Windows Server reboots. To ensure that this happens, we&rsquo;ll populate an entry under the <b>RunOnce<\/b> key of our server to authorize the DHCP server after the reboot.&nbsp;<\/p>\n<p style=\"margin-left:30px\">NEW-ITEMPROPERTY &#8220;HKLM:SoftwareMicrosoftWindowsCurrentVersionRunOnce&#8221; -Name &#8220;PoshStart&#8221; -Value &#8220;PowerShell -command {ADD-DHCPServerInDC -DNSName EOT-DC01.contoso.local -IPAddress $DCIPAddress}&#8221;\nSave this script in the C:ISO folder as EOT-DC01.PS1, and then run the previous script for making a virtual machine from a template file like this:<\/p>\n<p style=\"margin-left:30px\">NEW-VMFromTemplate &ndash;VMName EOT-DC01\nIn about 10 minutes (depending on your hard-drive speed and CPU), you should have a fully live and active domain controller for a domain called <b>Contoso.local<\/b>.\nAs a safety feature, the DHCP server is authorized, but the scope is not enabled should this inadvertently spin up on a production LAN.\nAt this point if you needed to add any newly created virtual machines to the domain, you can create a Windows PowerShell script with the <b>Add-Computer<\/b> cmdlet.\nThe cool part is that this method will work on the free Hyper-V Server 2012 R2 in addition to the licensed version and Windows&nbsp;8.1.\nIf you&rsquo;re feeling adventurous, you could try modifying it for earlier versions of Hyper-V, for example, Windows Server<span>&nbsp;<\/span>2008<span>&nbsp;<\/span>R2 and Hyper-V Server<span>&nbsp;<\/span>2008<span>&nbsp;<\/span>R2. You may have to parse DiskPart for the drive letters, or leverage a utility such as Virtual CloneDrive to mount the CDs.\nEnjoy the Power, and let me know how it works out for you! If you&rsquo;d like to save yourself from headaches from typing the scripts, you can download the main structure from the Script Center Repository:&nbsp;<a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/Deploy-PreConfigured-4a2ec2e0\" target=\"_blank\">Deploy PreConfigured Virtual Machines in Hyper-V Server 2012 R2<\/a>. Remember that to get this all running, you need to download <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/Convert-WindowsImageps1-0fe23a8f\" target=\"_blank\">Pronichkin&rsquo;s Convert-WindowsImage.ps1<\/a> and the <a href=\"http:\/\/technet.microsoft.com\/en-us\/evalcenter\/dn205286.aspx\" target=\"_blank\">Windows Server 2012 R2 Evaluation Media<\/a>.\nI invite you to follow The Scripting Guys on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send an email to The Scripting Guys at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then remember eat your Cmdlets each and every day with a taste dash of Creativity.\n<b>Sean Kearney, <\/b>Windows PowerShell MVP and Honorary Scripting Guy&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Preconfigure a virtual machine as a domain controller including Windows PowerShell Desired State Configuration and DHCP. &nbsp;Hey, Scripting Guy! I saw how we could build the virtual machines from a template. Were you teasing us about creating a new domain controller in this lab environment? I&rsquo;d really love to see the script that would [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,154,45],"class_list":["post-2009","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Preconfigure a virtual machine as a domain controller including Windows PowerShell Desired State Configuration and DHCP. &nbsp;Hey, Scripting Guy! I saw how we could build the virtual machines from a template. Were you teasing us about creating a new domain controller in this lab environment? I&rsquo;d really love to see the script that would [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2009","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=2009"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2009\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=2009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}