This article provides a PowerShell script that validates the syntax and integrity of the web.config file used by Snow License Manager. The script performs several checks, including:
- Verifying the file exists
- Validating XML syntax
- Using .NET’s ConfigurationManager for structure validation
- Checking Internet Information Services (IIS) configuration using
appcmd.exe
These checks help identify configuration errors before deployment and generate a detailed log for troubleshooting.
Parameters
- $ConfigPath: The path to the web.config file to be validated.
- $LogPath: The path to the log file where validation results will be recorded.
Example
To use the script, specify the paths to the web.config file and the log file
.\Validate-WebConfig.ps1 -ConfigPath "C:\Program Files\Snow Software\Snow License Manager\Web\web.config" -LogPath "C:\Program Files\Snow Software\Snow License Manager\Web\Logs\webconfig_validation.log"
What the script does
- Write-Log
This function logs messages with color-coded output based on the log type (INFO, SUCCESS, WARNING, ERROR). It also writes the log messages to a specified log file.
- Validate-FileExists
This function checks if the web.config file exists at the specified path. It logs a success message if the file is found and an error message if it is not.
- Validate-XML
This function validates the XML syntax of the web.config file. It reads the file content and attempts to parse it as XML. If the syntax is correct, it logs a success message; otherwise, it logs an error message.
- Validate-ConfigurationManager
This function validates the web.config file using .NET's ConfigurationManager. It first calls Validate-XML to ensure the XML syntax is correct. Then, it attempts to load the configuration using ConfigurationManager. If successful, it logs a success message; otherwise, it logs an error message.
- Validate-IIS
This function validates the IIS configuration using appcmd.exe. It checks if appcmd.exe exists and then attempts to validate the defaultDocument section of the IIS configuration. If successful, it logs a success message; otherwise, it logs an error message.
Main execution
- Starting the validation: The script logs the start of the validation process.
- File existence check: It validates the existence of the web.config file using the
Validate-FileExistsfunction. - ConfigurationManager validation: It validates the web.config file using .NET's ConfigurationManager by calling the
Validate-ConfigurationManagerfunction. - IIS Configuration validation: It validates the IIS configuration using
appcmd.exeby calling theValidate-IISfunction. - Logging results: If any of the checks fail, it logs an error message and exits with a status code of 1. If all checks pass, it logs a success message and exits with a status code of 0.
Example usage
To use the script, you would specify the paths to the web.config file and the log file. For example:
.\Validate-WebConfig.ps1 -ConfigPath "C:\Program Files\Snow Software\Snow License Manager\Web\web.config" -LogPath "C:\Program Files\Snow Software\Snow License Manager\Web\Logs\webconfig_validation.log"
Script
# Run in PowerShell ISE with Administrator privileges
param (
[string]$ConfigPath = "C:\Program Files\Snow Software\Snow License Manager\Web\web.config",
[string]$LogPath = "C:\Program Files\Snow Software\Snow License Manager\Web\Logs\webconfig_validation.log"
)
# Ensure log directory exists
$logDir = Split-Path -Path $LogPath
if (!(Test-Path $logDir)) {
New-Item -ItemType Directory -Path $logDir -Force | Out-Null
}
function Write-Log {
param ([string]$Message, [string]$Type = "INFO")
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logMessage = "[$timestamp] [$Type] $Message"
switch ($Type) {
"ERROR" { Write-Host $logMessage -ForegroundColor Red }
"SUCCESS" { Write-Host $logMessage -ForegroundColor Green }
"WARNING" { Write-Host $logMessage -ForegroundColor Yellow }
default { Write-Host $logMessage }
}
$logMessage | Out-File -Append -FilePath $LogPath -Encoding utf8
}
function Validate-FileExists {
if (!(Test-Path $ConfigPath)) {
Write-Log "web.config not found at '$ConfigPath'" "ERROR"
return $false
}
Write-Log "web.config found at: $ConfigPath" "SUCCESS"
return $true
}
function Validate-XML {
try {
$xmlContent = Get-Content -Path $ConfigPath -Raw -ErrorAction Stop
$xml = [xml]$xmlContent
Write-Log "XML syntax check passed." "SUCCESS"
return $true
} catch {
Write-Log "XML validation failed: $($_.Exception.Message)" "ERROR"
return $false
}
}
function Validate-ConfigurationManager {
if (-not (Validate-XML)) {
Write-Log "Skipping ConfigurationManager validation due to XML failure." "ERROR"
return $false
}
try {
Add-Type -AssemblyName System.Configuration -ErrorAction Stop
$configMap = New-Object System.Configuration.ExeConfigurationFileMap
$configMap.ExeConfigFilename = $ConfigPath
[System.Configuration.ConfigurationManager]::OpenMappedExeConfiguration($configMap, [System.Configuration.ConfigurationUserLevel]::None) | Out-Null
Write-Log "ConfigurationManager successfully loaded the web.config." "SUCCESS"
return $true
} catch {
Write-Log "ConfigurationManager validation failed: $($_.Exception.Message)" "ERROR"
return $false
}
}
function Validate-IIS {
$AppCmd = "$env:SystemRoot\System32\inetsrv\appcmd.exe"
if (!(Test-Path $AppCmd)) {
Write-Log "IIS appcmd.exe not found. Skipping IIS validation." "WARNING"
return $false
}
$result = & $AppCmd list config /section:system.webServer/defaultDocument 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Log "IIS appcmd validation passed." "SUCCESS"
return $true
} else {
Write-Log "IIS appcmd validation failed: $result" "ERROR"
return $false
}
}
Write-Log "Starting web.config validation..."
$checks = @(
Validate-FileExists
Validate-ConfigurationManager
Validate-IIS
)
if ($checks -contains $false) {
Write-Log "Validation failed. Check logs for details." "ERROR"
exit 1
}
Write-Log "All checks completed successfully. The web.config is valid." "SUCCESS"
exit 0
Related Articles
How to: Check logs for Scheduled reports in Snow license Manager 55Number of Views Find duplicate records in Snow License Manager 259Number of Views Create SQL database backups of Snow Inventory and Snow License Manager for Support 52Number of Views Snow License Manager 25.1 update fails due to invalid SameSite attribute in web.config 20Number of Views Create a custom field in Snow License Manager 18Number of Views
Hi, I am Reva - Ask me anything.
No new updates
Thanks for the feedback!
Your feedback has been saved.Rate this response:
Add Additional feedback ( Optional )
Are you sure you want to cancel
the case creation?
Are you sure you want to cancel the case creation?
Are you sure you want to close this case
| Products | Region | Phone Numbers |
|---|---|---|
| FlexNet Operations FlexNet Embedded FlexNet Publisher FlexNet Connect FlexNet Code Insight InstallAnywhere InstallShield |
North America * |
+1 630-332-2513 (toll) +1 877-279-2853 (toll-free in North America) |
| Europe * |
+44 1925 944367 (toll) +44 800 047 8642 (toll-free in Europe) |
|
| Japan * | +81 3-4540-5335 (select option 2) | |
| Australia * |
+61 3 9895 2177 +61 1800 560 603 (toll-free in Australia) |
|
|
Usage Intelligence (formerly
Revulytics) Compliance Intelligence |
Please use the Case Portal to submit your support ticket or reach out to your Revenera contact. | |
Revenera Assistant
Case id: 00001065
Activity: Status change: 2 hours ago