- Created `DSC-UserInterfaceConfiguration.ps1` to manage user interface settings via registry changes. - Developed `DSC-WindowsFeatures.ps1` to install OpenSSH Client and enable NFS Client features. - Implemented `DSC-WindowsServices.ps1` to ensure Terminal Services are running and set to automatic startup. - Added `PS-InstallApps.ps1` to manage app installations and remove the msstore source if it exists. - Created `PS-RemoveApps.ps1` to remove unwanted apps, provisioned packages, and handle Office applications via winget.
181 lines
7.1 KiB
PowerShell
181 lines
7.1 KiB
PowerShell
# === Admin Check ===
|
|
|
|
# set-executionpolicy unrestricted
|
|
|
|
# Check if running as administrator
|
|
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
|
|
Write-Host "This script requires administrator privileges. Attempting to restart as administrator..." -ForegroundColor Yellow
|
|
|
|
# Get the current script path
|
|
$scriptPath = $MyInvocation.MyCommand.Path
|
|
|
|
# Restart the script with administrator privileges
|
|
try {
|
|
Start-Process PowerShell -Verb RunAs -ArgumentList "-ExecutionPolicy Bypass -File `"$scriptPath`""
|
|
exit
|
|
}
|
|
catch {
|
|
Write-Error "Failed to restart as administrator. Please run this script as administrator manually."
|
|
Write-Host "Right-click on PowerShell and select 'Run as administrator', then run this script again." -ForegroundColor Red
|
|
pause
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
Write-Host "Running with administrator privileges." -ForegroundColor Green
|
|
$uid = $Env:UserName # Get current username for use in paths
|
|
Write-Host "Current user: $uid" -ForegroundColor Green
|
|
|
|
# === PREREQUISITE CHECKS ===
|
|
|
|
# Check if winget is installed
|
|
Write-Host "Checking winget installation..." -ForegroundColor Yellow
|
|
try {
|
|
$wingetVersion = winget --version
|
|
Write-Host "winget is installed: $wingetVersion" -ForegroundColor Green
|
|
}
|
|
catch {
|
|
Write-Error "winget is not installed or not accessible. Please install winget first."
|
|
Write-Host "You can install winget from the Microsoft Store (App Installer) or GitHub." -ForegroundColor Red
|
|
Write-Host "GitHub: https://github.com/microsoft/winget-cli/releases" -ForegroundColor Cyan
|
|
pause
|
|
exit 1
|
|
}
|
|
|
|
# Install Microsoft DSC
|
|
Write-Host "Installing Microsoft DSC..." -ForegroundColor Yellow
|
|
try {
|
|
# Check if already installed
|
|
$dscInstalled = winget list --id Microsoft.DSC --exact 2>$null
|
|
if ($LASTEXITCODE -eq 0 -and $dscInstalled -match "Microsoft.DSC") {
|
|
Write-Host "Microsoft DSC is already installed." -ForegroundColor Green
|
|
} else {
|
|
Write-Host "Installing Microsoft DSC via winget..." -ForegroundColor Yellow
|
|
winget install Microsoft.DSC --silent --accept-source-agreements --accept-package-agreements
|
|
if ($LASTEXITCODE -eq 0) {
|
|
Write-Host "Microsoft DSC installed successfully." -ForegroundColor Green
|
|
} else {
|
|
Write-Warning "Failed to install Microsoft DSC. DSC configurations may not work properly."
|
|
}
|
|
}
|
|
}
|
|
catch {
|
|
Write-Warning "Failed to check/install Microsoft DSC: $_"
|
|
}
|
|
|
|
# Configure WinRM service for DSC
|
|
Write-Host "Configuring WinRM service..." -ForegroundColor Yellow
|
|
try {
|
|
# Start WinRM service
|
|
$winrmService = Get-Service -Name WinRM -ErrorAction SilentlyContinue
|
|
if ($winrmService) {
|
|
if ($winrmService.Status -ne "Running") {
|
|
Write-Host "Starting WinRM service..." -ForegroundColor Yellow
|
|
Start-Service WinRM
|
|
Write-Host "WinRM service started." -ForegroundColor Green
|
|
} else {
|
|
Write-Host "WinRM service is already running." -ForegroundColor Green
|
|
}
|
|
|
|
# Set to automatic startup
|
|
if ($winrmService.StartType -ne "Automatic") {
|
|
Write-Host "Setting WinRM service to automatic startup..." -ForegroundColor Yellow
|
|
Set-Service WinRM -StartupType Automatic
|
|
Write-Host "WinRM service set to automatic startup." -ForegroundColor Green
|
|
} else {
|
|
Write-Host "WinRM service is already set to automatic startup." -ForegroundColor Green
|
|
}
|
|
} else {
|
|
Write-Warning "WinRM service not found. DSC configurations may not work properly."
|
|
}
|
|
}
|
|
catch {
|
|
Write-Warning "Failed to configure WinRM service: $_"
|
|
}
|
|
|
|
# Clear any pending DSC configurations
|
|
Write-Host "Checking and clearing any pending DSC configurations..." -ForegroundColor Yellow
|
|
try {
|
|
# Force stop any running DSC operations
|
|
Write-Host "Stopping any active DSC operations..." -ForegroundColor Yellow
|
|
Stop-DscConfiguration -Force -ErrorAction SilentlyContinue
|
|
Start-Sleep -Seconds 3
|
|
|
|
# Check DSC status
|
|
$dscStatus = Get-DscLocalConfigurationManager -ErrorAction SilentlyContinue
|
|
if ($dscStatus) {
|
|
Write-Host "Current DSC LCM State: $($dscStatus.LCMState)" -ForegroundColor Cyan
|
|
|
|
# If still not idle, try more aggressive cleanup
|
|
if ($dscStatus.LCMState -ne "Idle") {
|
|
Write-Host "Performing aggressive DSC cleanup..." -ForegroundColor Yellow
|
|
|
|
# Try to cancel any pending operations
|
|
Stop-DscConfiguration -Force -ErrorAction SilentlyContinue
|
|
Start-Sleep -Seconds 2
|
|
|
|
# Remove any pending.mof files that might be causing issues
|
|
$pendingMof = "$env:SystemRoot\System32\Configuration\pending.mof"
|
|
$currentMof = "$env:SystemRoot\System32\Configuration\current.mof"
|
|
|
|
if (Test-Path $pendingMof) {
|
|
Remove-Item $pendingMof -Force -ErrorAction SilentlyContinue
|
|
Write-Host "Removed pending.mof file." -ForegroundColor Green
|
|
}
|
|
|
|
# Re-check status
|
|
Start-Sleep -Seconds 2
|
|
$dscStatus = Get-DscLocalConfigurationManager -ErrorAction SilentlyContinue
|
|
Write-Host "Final DSC LCM State: $($dscStatus.LCMState)" -ForegroundColor Cyan
|
|
}
|
|
|
|
if ($dscStatus.LCMState -eq "Idle") {
|
|
Write-Host "DSC is ready for new configurations." -ForegroundColor Green
|
|
} else {
|
|
Write-Warning "DSC may still be in pending state. Will use -Force parameter for configurations."
|
|
}
|
|
}
|
|
}
|
|
catch {
|
|
Write-Warning "Failed to check/clear DSC status: $_"
|
|
Write-Host "Will proceed with -Force parameter for DSC configurations." -ForegroundColor Yellow
|
|
}
|
|
|
|
# === Install Features/Enable Services ===
|
|
|
|
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
|
Write-Host "Running Windows Features DSC configuration..." -ForegroundColor Yellow
|
|
& "$scriptDir\DSC-WindowsFeatures.ps1"
|
|
|
|
Write-Host "Running Windows Services DSC configuration..." -ForegroundColor Yellow
|
|
& "$scriptDir\DSC-WindowsServices.ps1"
|
|
|
|
# === Disable Telemetry ===
|
|
|
|
Write-Host "Running Telemetry, Privacy & Security DSC configuration..." -ForegroundColor Yellow
|
|
& "$scriptDir\DSC-TelemetryPrivacySecurity.ps1"
|
|
|
|
# === Remove Apps ===
|
|
|
|
Write-Host "Running App Removal script..." -ForegroundColor Yellow
|
|
& "$scriptDir\PS-RemoveApps.ps1"
|
|
|
|
# === Install Apps ===
|
|
|
|
Write-Host "Running App Installation script..." -ForegroundColor Yellow
|
|
& "$scriptDir\PS-InstallApps.ps1"
|
|
|
|
# === Configure User Interface ===
|
|
|
|
Write-Host "Running User Interface DSC configuration..." -ForegroundColor Yellow
|
|
& "$scriptDir\DSC-UserInterfaceConfiguration.ps1"
|
|
|
|
# === Configure Environment Variables ===
|
|
|
|
Write-Host "Running Environment Variables DSC configuration..." -ForegroundColor Yellow
|
|
& "$scriptDir\DSC-EnvironmentVariables.ps1"
|
|
|
|
# === File Operations ===
|
|
Write-Host "Running File Operations DSC configuration..." -ForegroundColor Yellow
|
|
& "$scriptDir\DSC-FileOperations.ps1"
|