From 21de19d2573b802d93cd0a9af739ca2937e28b66 Mon Sep 17 00:00:00 2001 From: heqnx Date: Sun, 13 Jul 2025 16:01:51 +0300 Subject: adding a websql to combine iis + mssql --- ansible/scripts/setup-websql.ps1 | 236 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 236 insertions(+) create mode 100644 ansible/scripts/setup-websql.ps1 (limited to 'ansible/scripts/setup-websql.ps1') diff --git a/ansible/scripts/setup-websql.ps1 b/ansible/scripts/setup-websql.ps1 new file mode 100644 index 0000000..9f5db5c --- /dev/null +++ b/ansible/scripts/setup-websql.ps1 @@ -0,0 +1,236 @@ +param ( + [string]$DomainName = "contoso.com", + [string]$SvcUsername = "svc_websql01", + [string]$SvcPassword = "Svc1234!" +) + +$scriptName = $MyInvocation.MyCommand.Name +$logFile = "C:\Logs\${scriptName}_log.txt" +$NetBiosName = $DomainName.Split(".")[0].ToUpper() +$wwwroot1 = "C:\inetpub\wwwroot" +$wwwroot2 = "C:\inetpub\wwwroot2" +$wwwroot3 = "C:\inetpub\wwwroot3" + +Start-Transcript -Path $logFile -Append + +# --- IIS Setup --- +try { + Install-WindowsFeature -Name Web-Server -IncludeManagementTools + Install-WindowsFeature -Name Web-Asp-Net45 + Write-Host "[INFO] Installed IIS and ASP.NET" +} catch { + Write-Host "[ERR] Failed to install IIS and ASP.NET" +} + +# Upload form content +@" +using System; +using System.IO; +using System.Web.UI; + +public partial class UploadPage : Page +{ + protected void UploadFile(object sender, EventArgs e) + { + if (fileUpload.PostedFile != null && fileUpload.PostedFile.ContentLength > 0) + { + try + { + string filename = Path.GetFileName(fileUpload.PostedFile.FileName); + fileUpload.PostedFile.SaveAs(Server.MapPath(filename)); + lblMessage.Text = "File uploaded successfully!"; + } + catch (Exception ex) + { + lblMessage.Text = "Error: " + ex.Message; + } + } + else + { + lblMessage.Text = "Please select a file to upload."; + } + } +} +"@ | Out-File "$wwwroot1\upload.aspx.cs" -Force + +@" +<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="UploadPage" %> + + +File Upload Page + +
+
+ +
+ +
+ +
+
+ + +"@ | Out-File "$wwwroot1\upload.aspx" -Force + +@" + + + + + + + + +"@ | Out-File "$wwwroot1\Web.config" -Force + +# Default site - port 80 +try { + New-WebSite -Name "MyASPXSite" -Port 80 -PhysicalPath $wwwroot1 -ApplicationPool "DefaultAppPool" + Set-ItemProperty "IIS:\AppPools\DefaultAppPool" -Name processModel -Value @{userName="$SvcUsername";password="$SvcPassword";identityType=3} + New-NetFirewallRule -DisplayName "HTTP (80)" -Direction Inbound -Protocol TCP -LocalPort 80 -Action Allow + Restart-WebAppPool -Name "DefaultAppPool" + Write-Host "[INFO] Site 1 created on port 80" +} catch { + Write-Host "[ERR] Failed to create site 1" +} + +# ACLs for wwwroot1 +try { + $svcRule = New-Object System.Security.AccessControl.FileSystemAccessRule("$DomainName\$SvcUsername", "Modify", "ContainerInherit,ObjectInherit", "None", "Allow") + $acl = Get-Acl $wwwroot1 + $acl.SetAccessRule($svcRule) + Set-Acl -Path $wwwroot1 -AclObject $acl + Write-Host "[INFO] ACL set for $wwwroot1" +} catch { + Write-Host "[ERR] Failed to set ACL for $wwwroot1" +} + +# Second site - port 8080 +try { + Copy-Item $wwwroot1 -Destination $wwwroot2 -Recurse -Force + New-WebAppPool -Name "DefaultAppPool2" + New-WebSite -Name "MyASPXSite2" -Port 8080 -PhysicalPath $wwwroot2 -ApplicationPool "DefaultAppPool2" + Set-ItemProperty "IIS:\AppPools\DefaultAppPool2" -Name processModel -Value @{identityType=2} + New-NetFirewallRule -DisplayName "HTTP (8080)" -Direction Inbound -Protocol TCP -LocalPort 8080 -Action Allow + $acl = Get-Acl $wwwroot2 + $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("IIS_IUSRS", "Modify", "ContainerInherit,ObjectInherit", "None", "Allow") + $acl.SetAccessRule($rule) + Set-Acl -Path $wwwroot2 -AclObject $acl + Restart-WebAppPool -Name "DefaultAppPool2" + Write-Host "[INFO] Site 2 created on port 8080" +} catch { + Write-Host "[ERR] Failed to create site 2" +} + +# SQL Server Express setup +try { + New-Item -Path "C:\setup\media" -ItemType "Directory" -Force + @" +;SQL Server Configuration File +[OPTIONS] +IACCEPTSQLSERVERLICENSETERMS="True" +ACTION="Install" +ENU="True" +QUIET="True" +FEATURES=SQLENGINE,FULLTEXT +INSTANCENAME="SQLEXPRESS" +SQLSVCACCOUNT="NT AUTHORITY\NETWORK SERVICE" +SQLSYSADMINACCOUNTS="BUILTIN\Administrators" +ADDCURRENTUSERASSQLADMIN="True" +TCPENABLED="1" +NPENABLED="0" +SAPWD="$SvcPassword" +"@ | Out-File "C:\setup\sql_conf.ini" + + Start-Process -FilePath "C:\setup\SQL2019-SSEI-Expr.exe" -ArgumentList "/configurationfile=C:\setup\sql_conf.ini /IACCEPTSQLSERVERLICENSETERMS /MEDIAPATH=C:\setup\media /QUIET" -Wait + Set-ItemProperty -Path "HKLM:\Software\Microsoft\Microsoft SQL Server\MSSQL15.SQLEXPRESS\MSSQLServer\SuperSocketNetLib\Tcp\IPAll" -Name "TcpPort" -Value "1433" + Restart-Service -Name "MSSQL`$SQLEXPRESS" + New-NetFirewallRule -DisplayName "SQLServer 1433" -Direction Inbound -LocalPort 1433 -Protocol TCP -Action Allow + + $env:Path += ";C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn" + SqlCmd -E -Q "CREATE LOGIN [$NetBiosName\$SvcUsername] FROM WINDOWS" + SqlCmd -E -Q "SP_ADDSRVROLEMEMBER '$NetBiosName\$SvcUsername', 'SYSADMIN'" + SqlCmd -E -Q "ALTER LOGIN sa ENABLE" + SqlCmd -E -Q "ALTER LOGIN sa WITH PASSWORD = '$SvcPassword', CHECK_POLICY=OFF" + Write-Host "[INFO] SQL Server installed/configured" +} catch { + Write-Host "[ERR] SQL Server setup failed" +} + +# Third site - port 9090 with upload + SQL query page +try { + Copy-Item $wwwroot1 -Destination $wwwroot3 -Recurse -Force + New-WebAppPool -Name "SqlQueryAppPool" + New-WebSite -Name "SqlQuerySite" -Port 9090 -PhysicalPath $wwwroot3 -ApplicationPool "SqlQueryAppPool" + Set-ItemProperty "IIS:\AppPools\SqlQueryAppPool" -Name processModel -Value @{userName="$SvcUsername";password="$SvcPassword";identityType=3} + New-NetFirewallRule -DisplayName "HTTP (9090)" -Direction Inbound -Protocol TCP -LocalPort 9090 -Action Allow + + # SQL Query Page + @" +<%@ Page Language="C#" Debug="true" %> +<%@ Import Namespace="System.Data" %> +<%@ Import Namespace="System.Data.SqlClient" %> + + +SQL Query Tester + +

Enter a SQL Query

+
+

+

+ + + + +"@ | Out-File "$wwwroot3\sqlquery.aspx" -Force + + Restart-WebAppPool -Name "SqlQueryAppPool" + Write-Host "[INFO] Site 3 created on port 9090 with SQL query page" +} catch { + Write-Host "[ERR] Failed to create SQL query site" +} + +Stop-Transcript -- cgit v1.2.3