Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Build Web Server for a DSC Demo
$ConfigData =@{
AllNodes = @(
@{
NodeName = "localhost"
PSDSCAllowPlainTextPassword = $True
}
)
}
Configuration BuildLabWebServer{
param(
[parameter(Mandatory)]
[ValidateNotNullorEmpty()]
[string]$NodeName,
[parameter(Mandatory=$True)]
[ValidateNotNullorEmpty()]
[string]$ComputerName,
[parameter(Mandatory=$True)]
[ValidateNotNullorEmpty()]
[string]$Domain,
[parameter(Mandatory=$True)]
[ValidateNotNullorEmpty()]
[string]$IP,
[parameter(Mandatory=$True)]
[ValidateNotNullorEmpty()]
[string]$DNSIP,
[parameter(Mandatory=$True)]
[ValidateNotNullorEmpty()]
[string]$Gateway,
[parameter(Mandatory=$True)]
[ValidateNotNullorEmpty()]
[string]$Subnet,
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[String]$WebSiteName,
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[String]$SourcePath
)
#unsecure, not safe or recommended way to do this
$Creds = ConvertTo-SecureString "Passw0rd!" -AsPlainText -Force
$DomainAdminCred = New-Object System.Management.Automation.PSCredential ("$Domain\Administrator", $Creds)
$SafeModeAdminCred = New-Object System.Management.Automation.PSCredential ("Administrator", $Creds)
Import-DscResource -ModuleName xNetworking,xComputerManagement,xPendingReboot,LutzResources,xWebAdministration
Node $NodeName{
LocalConfigurationManager{
RebootNodeIfNeeded = $True
}
WindowsFeature IIS
{
Ensure = "Present"
Name = "Web-Server"
}
WindowsFeature AspNet45
{
Ensure = "Present"
Name = "Web-Asp-Net45"
}
WindowsFeature IISMgmtTools
{
Ensure = "Present"
Name = "Web-Mgmt-Tools"
}
xWebsite DefaultSite
{
Ensure = "Present"
Name = "Default Web Site"
State = "Stopped"
PhysicalPath = "C:\inetpub\wwwroot"
DependsOn = "[WindowsFeature]IIS"
}
xIPAddress WEBIP{
IPAddress = $IP
DefaultGateway = $Gateway
SubnetMask = $Subnet
AddressFamily = "IPv4"
InterfaceAlias = "Ethernet"
}
xDNSServerAddress DomainDNS{
Address = $DNSIP
InterfaceAlias = "Ethernet"
AddressFamily = "IPv4"
}
LutzFirewall Domain{
Profile = "Domain"
Status = "Disabled"
}
xComputer RenameAndDomainJoin{
Name = $ComputerName
DomainName = $Domain
Credential = $DomainAdminCred
DependsOn = "[xIPAddress]WEBIP","[xDNSServerAddress]DomainDNS"
}
xPendingReboot DomainJoin{
Name = "Check for reboot after domain join"
}
File WebSiteCopy{
Ensure = "Present"
DependsOn = "[xPendingReboot]DomainJoin"
SourcePath = $SourcePath
DestinationPath = "C:\$WebsiteName"
Recurse = $True
Type = "Directory"
}
# Create the new Website with HTTP
xWebsite DSCWebsite
{
Ensure = "Present"
Name = $WebSiteName
State = "Started"
PhysicalPath = "C:\$WebsiteName"
BindingInfo = MSFT_xWebBindingInformation
{
Protocol = "HTTP"
Port = 8080
}
DependsOn = "[File]WebSiteCopy"
}
}#Node
}#configuration
BuildLabWebServer -ConfigurationData $ConfigData -NodeName localhost -computername $WebServerName -Domain $YourDomain -IP $YourIP -Gateway $YourGateway -Subnet 24 -DNSIP $YourDCIP -OutputPath $YourPath -WebsiteName "DSCTest" -SourcePath "\\PULLSERVER\WebServerFiles"
Set-DscLocalConfigurationManager -Path $YourPath
Get-DSCLocalConfigurationManager
Start-DscConfiguration -Wait -Force -Verbose -Path $YourPath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment