From 3f05e893624cd9db7152fb86c12b77856616674f Mon Sep 17 00:00:00 2001 From: Simone Date: Wed, 13 May 2026 19:11:33 +0200 Subject: [PATCH] ci(lint): add CmdletBinding and ShouldProcess support to ISO creation functions for safer execution --- template/Deploy-LinuxBuild2404.ps1 | 4 ++++ template/Deploy-WinBuild2022.ps1 | 10 ++++++++++ template/Deploy-WinBuild2025.ps1 | 10 ++++++++++ 3 files changed, 24 insertions(+) diff --git a/template/Deploy-LinuxBuild2404.ps1 b/template/Deploy-LinuxBuild2404.ps1 index cdfcb10..f06a81a 100644 --- a/template/Deploy-LinuxBuild2404.ps1 +++ b/template/Deploy-LinuxBuild2404.ps1 @@ -242,11 +242,13 @@ if (-not ([System.Management.Automation.PSTypeName]'IsoStreamWriter2').Type) { } function New-IsoFromFolder { + [CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')] param( [Parameter(Mandatory)] [string] $SourceFolder, [Parameter(Mandatory)] [string] $IsoPath, [Parameter(Mandatory)] [string] $VolumeLabel ) + if (-not $PSCmdlet.ShouldProcess($IsoPath, 'Create ISO from folder')) { return } if (Test-Path $IsoPath) { Remove-Item $IsoPath -Force } $fsi = New-Object -ComObject IMAPI2FS.MsftFileSystemImage $result = $null @@ -296,11 +298,13 @@ function Invoke-Vmrun { } function Set-VmxKey { + [CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')] param( [Parameter(Mandatory)] [string] $VmxPath, [Parameter(Mandatory)] [string] $Key, [Parameter(Mandatory)] [string] $Value ) + if (-not $PSCmdlet.ShouldProcess($VmxPath, "Set VMX key '$Key'")) { return } $lines = Get-Content -LiteralPath $VmxPath $pattern = "^\s*$([regex]::Escape($Key))\s*=" $found = $false diff --git a/template/Deploy-WinBuild2022.ps1 b/template/Deploy-WinBuild2022.ps1 index 708375a..9d8f0cf 100644 --- a/template/Deploy-WinBuild2022.ps1 +++ b/template/Deploy-WinBuild2022.ps1 @@ -270,11 +270,13 @@ if (-not ([System.Management.Automation.PSTypeName]'IsoStreamWriter2').Type) { } function New-IsoFromFolder { + [CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')] param( [Parameter(Mandatory)] [string] $SourceFolder, [Parameter(Mandatory)] [string] $IsoPath, [Parameter(Mandatory)] [string] $VolumeLabel ) + if (-not $PSCmdlet.ShouldProcess($IsoPath, 'Create ISO from folder')) { return } if (Test-Path $IsoPath) { Remove-Item $IsoPath -Force } $fsi = New-Object -ComObject IMAPI2FS.MsftFileSystemImage $result = $null @@ -305,10 +307,12 @@ function New-WinIsoNoPrompt { Result: UEFI boot proceeds straight into Windows Setup, no "Press any key to boot from CD" prompt. #> + [CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')] param( [Parameter(Mandatory)] [string] $SourceIsoPath, [Parameter(Mandatory)] [string] $OutputIsoPath ) + if (-not $PSCmdlet.ShouldProcess($OutputIsoPath, 'Rebuild Windows ISO without boot prompt')) { return } if (Test-Path $OutputIsoPath) { Remove-Item $OutputIsoPath -Force } $mount = Mount-DiskImage -ImagePath $SourceIsoPath -PassThru -StorageType ISO -Access ReadOnly @@ -383,11 +387,13 @@ function Invoke-Vmrun { } function Set-VmxKey { + [CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')] param( [Parameter(Mandatory)] [string] $VmxPath, [Parameter(Mandatory)] [string] $Key, [Parameter(Mandatory)] [string] $Value ) + if (-not $PSCmdlet.ShouldProcess($VmxPath, "Set VMX key '$Key'")) { return } $lines = Get-Content -LiteralPath $VmxPath $pattern = "^\s*$([regex]::Escape($Key))\s*=" $found = $false @@ -400,8 +406,10 @@ function Set-VmxKey { } function Remove-VmxLines { + [CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')] param([Parameter(Mandatory)] [string] $VmxPath, [Parameter(Mandatory)] [string] $KeyPrefix) + if (-not $PSCmdlet.ShouldProcess($VmxPath, "Remove VMX lines starting with '$KeyPrefix'")) { return } $pat = "^\s*$([regex]::Escape($KeyPrefix))" $lines = Get-Content -LiteralPath $VmxPath | Where-Object { $_ -notmatch $pat } Set-Content -LiteralPath $VmxPath -Value $lines -Encoding ASCII @@ -428,8 +436,10 @@ function Invoke-VmrunBounded { } function Stop-Vm { + [CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')] param([Parameter(Mandatory)] [string] $VmxPath, [int] $SoftTimeoutMinutes = 5) + if (-not $PSCmdlet.ShouldProcess($VmxPath, 'Stop VM')) { return } # Send soft-stop (ACPI); vmrun may block until guest finishes — kill vmrun after 30 s. # Guest OS continues shutdown independently once the signal is received. Invoke-VmrunBounded -Arguments @('-T','ws','stop',$VmxPath,'soft') -TimeoutSeconds 30 diff --git a/template/Deploy-WinBuild2025.ps1 b/template/Deploy-WinBuild2025.ps1 index 1ebb04b..0cb2f8b 100644 --- a/template/Deploy-WinBuild2025.ps1 +++ b/template/Deploy-WinBuild2025.ps1 @@ -270,11 +270,13 @@ if (-not ([System.Management.Automation.PSTypeName]'IsoStreamWriter2').Type) { } function New-IsoFromFolder { + [CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')] param( [Parameter(Mandatory)] [string] $SourceFolder, [Parameter(Mandatory)] [string] $IsoPath, [Parameter(Mandatory)] [string] $VolumeLabel ) + if (-not $PSCmdlet.ShouldProcess($IsoPath, 'Create ISO from folder')) { return } if (Test-Path $IsoPath) { Remove-Item $IsoPath -Force } $fsi = New-Object -ComObject IMAPI2FS.MsftFileSystemImage $result = $null @@ -305,10 +307,12 @@ function New-WinIsoNoPrompt { Result: UEFI boot proceeds straight into Windows Setup, no "Press any key to boot from CD" prompt. #> + [CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')] param( [Parameter(Mandatory)] [string] $SourceIsoPath, [Parameter(Mandatory)] [string] $OutputIsoPath ) + if (-not $PSCmdlet.ShouldProcess($OutputIsoPath, 'Rebuild Windows ISO without boot prompt')) { return } if (Test-Path $OutputIsoPath) { Remove-Item $OutputIsoPath -Force } $mount = Mount-DiskImage -ImagePath $SourceIsoPath -PassThru -StorageType ISO -Access ReadOnly @@ -383,11 +387,13 @@ function Invoke-Vmrun { } function Set-VmxKey { + [CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')] param( [Parameter(Mandatory)] [string] $VmxPath, [Parameter(Mandatory)] [string] $Key, [Parameter(Mandatory)] [string] $Value ) + if (-not $PSCmdlet.ShouldProcess($VmxPath, "Set VMX key '$Key'")) { return } $lines = Get-Content -LiteralPath $VmxPath $pattern = "^\s*$([regex]::Escape($Key))\s*=" $found = $false @@ -400,8 +406,10 @@ function Set-VmxKey { } function Remove-VmxLines { + [CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')] param([Parameter(Mandatory)] [string] $VmxPath, [Parameter(Mandatory)] [string] $KeyPrefix) + if (-not $PSCmdlet.ShouldProcess($VmxPath, "Remove VMX lines starting with '$KeyPrefix'")) { return } $pat = "^\s*$([regex]::Escape($KeyPrefix))" $lines = Get-Content -LiteralPath $VmxPath | Where-Object { $_ -notmatch $pat } Set-Content -LiteralPath $VmxPath -Value $lines -Encoding ASCII @@ -428,8 +436,10 @@ function Invoke-VmrunBounded { } function Stop-Vm { + [CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')] param([Parameter(Mandatory)] [string] $VmxPath, [int] $SoftTimeoutMinutes = 5) + if (-not $PSCmdlet.ShouldProcess($VmxPath, 'Stop VM')) { return } # Send soft-stop (ACPI); vmrun may block until guest finishes — kill vmrun after 30 s. # Guest OS continues shutdown independently once the signal is received. Invoke-VmrunBounded -Arguments @('-T','ws','stop',$VmxPath,'soft') -TimeoutSeconds 30