« Previous Next »

Thread: Script to create a new IIS Site & DNS Entries

Last post 07-20-2009 7:06 PM by robol. 1 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (2 items)

Sort Posts:

  • 07-16-2009, 11:10 AM

    Script to create a new IIS Site & DNS Entries

    I spent a few days doing this, and since i didn't find anything when i was searching that did something quite like this, i thought I would share my script.

     The goal was to create a single script that would create a primary DNS zone on main nameserver, secondary dns zone on second nameserver, and two A entries and MX entry on the primary nameserver. Along with that, i wanted to create an IIS Site, & it's AppPool, and the physical location for the site to reside on the hard drive.

     Nearly everything i found was mostly designed to read a list of zones in from a file and create or update them. This script is designed to do one site at a time, asking the user for the site name, and then just creating it all. *one thing that may cause issue if you happen to copy/paste this out for your own use is where the line breaks are. Some of my lines are fairly long- mostly just the lines that actually call for the creation of a dns zone or a/mx record. so check those places if you have an issue.

    Here's the script that i came up with:

    #Script writen by Daniel Ceola
    #Parts of this script were taken or derived from http://www.highorbit.co.uk/?p=636
    ################################
    #Primary & Secondary NameServers; Mail Server; WebServer that site exists on.
    #These are all defined here, however the script could prompt the user for it simply by changing a line to read:
    # $servername = Read-Host "Enter DNS Server Name or Ip Address"
    #
    $servername="MainNameServer"
    $Server2name="SecondaryNameServer"
    $MailExchange = "MailServerIP"
    $IPAddress = "WebServer"

    ################################
    ###Create DNS Zone on Primary NameServer
    #Asks for domain name & sets it to $Name
    $Name = Read-Host "Enter Domain Name (ie test.com)"
    Write-Host -ForegroundColor green "Creating DNS Zone on Primary Nameserver"

    #0 for primary, 1 for secondary, 2 for Stub zone, 3 for Zone Forwarder
    $Type = 0

    # AD Integration (only valid on Active Directory Domain Controllers)
    $IsDSIntegrated = $False

    # FileName (Optional and only valid for zones with AD integrated set to $False)
    # File must exist if specified and have size greater than 0b.
    $Filename = $Null

    # Master IP (Optional and only valid for Secondary, Stub and Forwarder zones)
    $MasterIP = $Null

    # AdminEmail (Optional and only valid for Primary zones, writes into SOA record)
    $AdminEmail = $Null

    #Creates the actual zone
    $NewZone = ([WMIClass]"\\$ServerName\root\MicrosoftDNS:MicrosoftDNS_Zone").CreateZone($Name, $Type, $IsDSIntegrated, $FileName, $MasterIP, $AdminEmail)
    Write-Host -ForegroundColor yellow "Done"

    #Sets the containerName variable which is used below to be the same as the $Name variable, could have
    #simply removed where $Containername is used and changed it to $name
    $ContainerName=$Name

    ################################
    ###Create DNS Zone on Secondary NameServer

    Write-Host -ForegroundColor green "Creating DNS Zone on Secondary Nameserver"

    #0 for primary, 1 for secondary, 2 for Stub zone, 3 for Zone Forwarder
    $Type = 1

    # AD Integration (only valid on Active Directory Domain Controllers)
    $IsDSIntegrated = $False

    # FileName (Optional and only valid for zones with AD integrated set to $False)
    # File must exist if specified and have size greater than 0b.
    $Filename = $Null

    # Master IP (Optional and only valid for Secondary, Stub and Forwarder zones)
    #$MasterIP is set to address of the primary nameserver
    $MasterIP = $ServerName

    # AdminEmail (Optional and only valid for Primary zones, writes into SOA record)
    $AdminEmail = $Null

    #Creates the actual zone
    $NewZone = ([WMIClass]"\\$Server2Name\root\MicrosoftDNS:MicrosoftDNS_Zone").CreateZone($Name, $Type, $IsDSIntegrated, $FileName, $MasterIP, $AdminEmail)
    Write-Host -ForegroundColor yellow "Done"


    ###############################
    #Create A Record
    # Record Name (Owner Name). Should include full suffix to prevent the method throwing an error.
    Write-Host -ForegroundColor green "Creating A Record"

    ##creates A record for name www
    $OwnerName = "www.$ContainerName"

    ##creates A record for name 'same as parent'
    $OwnerName2="$ContainerName"

    ## Class, as in IN, CS, CH or HS. Normally only care about IN (Internet: 1) which
    ## is the default. (Optional)
    $RecordClass = $Null

    # Time To Live in seconds (Optional), $null sets it to default
    $TTL = $Null

    $NewATypeClass = [WMIClass]"\\$ServerName\root\MicrosoftDNS:MicrosoftDNS_AType"
    $NewARecord = $NewATypeClass.CreateInstanceFromPropertyData($ServerName, $ContainerName, $OwnerName, $RecordClass, $TTL, $IPAddress)
    $NewARecord2 = $NewATypeClass.CreateInstanceFromPropertyData($ServerName, $ContainerName, $OwnerName2, $RecordClass, $TTL, $IPAddress)
    Write-Host -ForegroundColor yellow "Done"

    ##################################
    #Create MX Records
    Write-Host -ForegroundColor green "Creating MX Record"
    # Record Name (Owner Name). Normally the SMTP domain, in this case it matches
    # ContainerName.
    $OwnerName = $ContainerName
    $RecordClass = $Null
    $TTL = $Null

    # Preference, numeric value used to determine the preferred server(s)
    $Preference = 10
     
    $NewMXTypeClass = [WMIClass]"\\$ServerName\root\MicrosoftDNS:MicrosoftDNS_MXType"
    $NewMXRecord = $NewMXTypeClass.CreateInstanceFromPropertyData($ServerName, $ContainerName, $OwnerName, $RecordClass, $TTL, $Preference, $MailExchange)
    Write-Host -ForegroundColor yellow "Done"


    ################################
    ##Create IIS stuff

    #Creates Directory on hard drive for new site
    Write-Host -ForegroundColor green "Creating Physical site directory at inetpub\$name"
    New-Item c:\inetpub\$name -Type directory
    Write-Host -ForegroundColor yellow "Done"

    #Creates IIS AppPool
    Write-Host -ForegroundColor green "Creating IIS AppPool"
    New-Item IIS:\AppPools\$name
    Write-Host -ForegroundColor yellow "Done"

    #Creates Site within IIS
    Write-Host -ForegroundColor green "Creating IIS Site"
    New-Item IIS:\Sites\$name -bindings @{protocol="http";bindingInformation=":80:www.$name"} -physicalPath c:\inetpub\$name
    Write-Host -ForegroundColor yellow "Done"

    #Assigns the AppPool to the site
    Write-Host -ForegroundColor green "Attaching AppPool to Site"
    Set-ItemProperty IIS:\Sites\$name -name applicationPool -value "$name"
    Write-Host -ForegroundColor yellow "Done"

    Write-Host -ForegroundColor Magenta "Site fully created"

     

    And since i also had trouble finding this, here's what i put in a batch file to call this script (createsite.ps1 is the filename that i saved the above script to):

    powershell.exe -psconsolefile "C:\Program Files\IIS\PowerShellSnapin\IIsConsole.psc1" -NoExit -command  "c:\createsite.ps1"

     

     

    Hope this helps someone!

     

     

  • 07-20-2009, 7:06 PM In reply to

    • robol
    • Top 100 Contributor
    • Joined on 06-16-2008, 5:11 AM
    • Melbourne
    • Posts 54

    Re: Script to create a new IIS Site & DNS Entries

    Appreciated !
Page 1 of 1 (2 items)
Microsoft Communities