Skip to content

Instantly share code, notes, and snippets.

@steviecoaster
Created January 9, 2025 02:44
Show Gist options
  • Save steviecoaster/1c7515e2b1f478b818c757a9116d9093 to your computer and use it in GitHub Desktop.
Save steviecoaster/1c7515e2b1f478b818c757a9116d9093 to your computer and use it in GitHub Desktop.
Boot strap IIS on windows to host PowerShell Scripts
<#
.SYNOPSIS
Creates the `ChocolateyInstall` IIS fileshare site.
.DESCRIPTION
Creates a new IIS website named `RtpsugDemo` which hosts the
a collection of scripts for onboarding clients to retrieve and run during their
setup.
If you have a need to re-create this for any reason, ensure the existing
`ChocolateyInstall` IIS site has been disabled and removed.
#>
[CmdletBinding()]
param(
# The path to a local directory which will be used to host the
# Import-ChocoServerCertificate.ps1 file over IIS for clients to utilize.
[Parameter()]
[Alias('LocalDir')]
[string]
$Path = 'C:\rtpsug_site'
)
end {
Invoke-RestMethod ch0.co/go | Invoke-Expression
choco install IIS-WebServer -y -s windowsfeatures
Import-Module WebAdministration
if (-not (Test-Path $Path)) {
$null = New-Item -Path $Path -ItemType Directory -Force
}
$siteName = 'RtpsugDemo'
if (-not (Get-Website -Name $siteName)) {
Write-Host "Creating Website: $siteName" -ForegroundColor Green
$null = New-Website -Name $siteName -Port 80 -PhysicalPath $Path -Force
Add-WebConfigurationProperty -PSPath IIS:\Sites\$siteName -Filter system.webServer/staticContent -Name "." -Value @{ fileExtension = '.ps1'; mimeType = 'text/plain' }
}
else {
Write-Host "Website for hosting certificate import already created" -ForegroundColor Green
}
if ((Get-Website -Name 'Default Web Site')) {
Get-Website -Name 'Default Web Site' | Remove-Website
}
else {
Write-Host "Default website already removed" -ForegroundColor Green
}
Write-Host "Restarting IIS to refresh bindings" -ForegroundColor Green
$null = iisreset
if ((Get-Website -Name $siteName).State -ne 'Started') {
Start-Website -Name $siteName
}
Write-Host "IIS website started on port 80 hosting PowerShell scripts from $Path"
Copy-Item C:\choco-setup\files\scripts\Register-C4bEndpoint.ps1 -Destination $Path
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment