ci(lint): add CmdletBinding and ShouldProcess support to ISO creation functions for safer execution
This commit is contained in:
@@ -242,11 +242,13 @@ if (-not ([System.Management.Automation.PSTypeName]'IsoStreamWriter2').Type) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function New-IsoFromFolder {
|
function New-IsoFromFolder {
|
||||||
|
[CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')]
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory)] [string] $SourceFolder,
|
[Parameter(Mandatory)] [string] $SourceFolder,
|
||||||
[Parameter(Mandatory)] [string] $IsoPath,
|
[Parameter(Mandatory)] [string] $IsoPath,
|
||||||
[Parameter(Mandatory)] [string] $VolumeLabel
|
[Parameter(Mandatory)] [string] $VolumeLabel
|
||||||
)
|
)
|
||||||
|
if (-not $PSCmdlet.ShouldProcess($IsoPath, 'Create ISO from folder')) { return }
|
||||||
if (Test-Path $IsoPath) { Remove-Item $IsoPath -Force }
|
if (Test-Path $IsoPath) { Remove-Item $IsoPath -Force }
|
||||||
$fsi = New-Object -ComObject IMAPI2FS.MsftFileSystemImage
|
$fsi = New-Object -ComObject IMAPI2FS.MsftFileSystemImage
|
||||||
$result = $null
|
$result = $null
|
||||||
@@ -296,11 +298,13 @@ function Invoke-Vmrun {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function Set-VmxKey {
|
function Set-VmxKey {
|
||||||
|
[CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')]
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory)] [string] $VmxPath,
|
[Parameter(Mandatory)] [string] $VmxPath,
|
||||||
[Parameter(Mandatory)] [string] $Key,
|
[Parameter(Mandatory)] [string] $Key,
|
||||||
[Parameter(Mandatory)] [string] $Value
|
[Parameter(Mandatory)] [string] $Value
|
||||||
)
|
)
|
||||||
|
if (-not $PSCmdlet.ShouldProcess($VmxPath, "Set VMX key '$Key'")) { return }
|
||||||
$lines = Get-Content -LiteralPath $VmxPath
|
$lines = Get-Content -LiteralPath $VmxPath
|
||||||
$pattern = "^\s*$([regex]::Escape($Key))\s*="
|
$pattern = "^\s*$([regex]::Escape($Key))\s*="
|
||||||
$found = $false
|
$found = $false
|
||||||
|
|||||||
@@ -270,11 +270,13 @@ if (-not ([System.Management.Automation.PSTypeName]'IsoStreamWriter2').Type) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function New-IsoFromFolder {
|
function New-IsoFromFolder {
|
||||||
|
[CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')]
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory)] [string] $SourceFolder,
|
[Parameter(Mandatory)] [string] $SourceFolder,
|
||||||
[Parameter(Mandatory)] [string] $IsoPath,
|
[Parameter(Mandatory)] [string] $IsoPath,
|
||||||
[Parameter(Mandatory)] [string] $VolumeLabel
|
[Parameter(Mandatory)] [string] $VolumeLabel
|
||||||
)
|
)
|
||||||
|
if (-not $PSCmdlet.ShouldProcess($IsoPath, 'Create ISO from folder')) { return }
|
||||||
if (Test-Path $IsoPath) { Remove-Item $IsoPath -Force }
|
if (Test-Path $IsoPath) { Remove-Item $IsoPath -Force }
|
||||||
$fsi = New-Object -ComObject IMAPI2FS.MsftFileSystemImage
|
$fsi = New-Object -ComObject IMAPI2FS.MsftFileSystemImage
|
||||||
$result = $null
|
$result = $null
|
||||||
@@ -305,10 +307,12 @@ function New-WinIsoNoPrompt {
|
|||||||
Result: UEFI boot proceeds straight into Windows Setup, no
|
Result: UEFI boot proceeds straight into Windows Setup, no
|
||||||
"Press any key to boot from CD" prompt.
|
"Press any key to boot from CD" prompt.
|
||||||
#>
|
#>
|
||||||
|
[CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')]
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory)] [string] $SourceIsoPath,
|
[Parameter(Mandatory)] [string] $SourceIsoPath,
|
||||||
[Parameter(Mandatory)] [string] $OutputIsoPath
|
[Parameter(Mandatory)] [string] $OutputIsoPath
|
||||||
)
|
)
|
||||||
|
if (-not $PSCmdlet.ShouldProcess($OutputIsoPath, 'Rebuild Windows ISO without boot prompt')) { return }
|
||||||
if (Test-Path $OutputIsoPath) { Remove-Item $OutputIsoPath -Force }
|
if (Test-Path $OutputIsoPath) { Remove-Item $OutputIsoPath -Force }
|
||||||
|
|
||||||
$mount = Mount-DiskImage -ImagePath $SourceIsoPath -PassThru -StorageType ISO -Access ReadOnly
|
$mount = Mount-DiskImage -ImagePath $SourceIsoPath -PassThru -StorageType ISO -Access ReadOnly
|
||||||
@@ -383,11 +387,13 @@ function Invoke-Vmrun {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function Set-VmxKey {
|
function Set-VmxKey {
|
||||||
|
[CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')]
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory)] [string] $VmxPath,
|
[Parameter(Mandatory)] [string] $VmxPath,
|
||||||
[Parameter(Mandatory)] [string] $Key,
|
[Parameter(Mandatory)] [string] $Key,
|
||||||
[Parameter(Mandatory)] [string] $Value
|
[Parameter(Mandatory)] [string] $Value
|
||||||
)
|
)
|
||||||
|
if (-not $PSCmdlet.ShouldProcess($VmxPath, "Set VMX key '$Key'")) { return }
|
||||||
$lines = Get-Content -LiteralPath $VmxPath
|
$lines = Get-Content -LiteralPath $VmxPath
|
||||||
$pattern = "^\s*$([regex]::Escape($Key))\s*="
|
$pattern = "^\s*$([regex]::Escape($Key))\s*="
|
||||||
$found = $false
|
$found = $false
|
||||||
@@ -400,8 +406,10 @@ function Set-VmxKey {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function Remove-VmxLines {
|
function Remove-VmxLines {
|
||||||
|
[CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')]
|
||||||
param([Parameter(Mandatory)] [string] $VmxPath,
|
param([Parameter(Mandatory)] [string] $VmxPath,
|
||||||
[Parameter(Mandatory)] [string] $KeyPrefix)
|
[Parameter(Mandatory)] [string] $KeyPrefix)
|
||||||
|
if (-not $PSCmdlet.ShouldProcess($VmxPath, "Remove VMX lines starting with '$KeyPrefix'")) { return }
|
||||||
$pat = "^\s*$([regex]::Escape($KeyPrefix))"
|
$pat = "^\s*$([regex]::Escape($KeyPrefix))"
|
||||||
$lines = Get-Content -LiteralPath $VmxPath | Where-Object { $_ -notmatch $pat }
|
$lines = Get-Content -LiteralPath $VmxPath | Where-Object { $_ -notmatch $pat }
|
||||||
Set-Content -LiteralPath $VmxPath -Value $lines -Encoding ASCII
|
Set-Content -LiteralPath $VmxPath -Value $lines -Encoding ASCII
|
||||||
@@ -428,8 +436,10 @@ function Invoke-VmrunBounded {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function Stop-Vm {
|
function Stop-Vm {
|
||||||
|
[CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')]
|
||||||
param([Parameter(Mandatory)] [string] $VmxPath,
|
param([Parameter(Mandatory)] [string] $VmxPath,
|
||||||
[int] $SoftTimeoutMinutes = 5)
|
[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.
|
# 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.
|
# Guest OS continues shutdown independently once the signal is received.
|
||||||
Invoke-VmrunBounded -Arguments @('-T','ws','stop',$VmxPath,'soft') -TimeoutSeconds 30
|
Invoke-VmrunBounded -Arguments @('-T','ws','stop',$VmxPath,'soft') -TimeoutSeconds 30
|
||||||
|
|||||||
@@ -270,11 +270,13 @@ if (-not ([System.Management.Automation.PSTypeName]'IsoStreamWriter2').Type) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function New-IsoFromFolder {
|
function New-IsoFromFolder {
|
||||||
|
[CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')]
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory)] [string] $SourceFolder,
|
[Parameter(Mandatory)] [string] $SourceFolder,
|
||||||
[Parameter(Mandatory)] [string] $IsoPath,
|
[Parameter(Mandatory)] [string] $IsoPath,
|
||||||
[Parameter(Mandatory)] [string] $VolumeLabel
|
[Parameter(Mandatory)] [string] $VolumeLabel
|
||||||
)
|
)
|
||||||
|
if (-not $PSCmdlet.ShouldProcess($IsoPath, 'Create ISO from folder')) { return }
|
||||||
if (Test-Path $IsoPath) { Remove-Item $IsoPath -Force }
|
if (Test-Path $IsoPath) { Remove-Item $IsoPath -Force }
|
||||||
$fsi = New-Object -ComObject IMAPI2FS.MsftFileSystemImage
|
$fsi = New-Object -ComObject IMAPI2FS.MsftFileSystemImage
|
||||||
$result = $null
|
$result = $null
|
||||||
@@ -305,10 +307,12 @@ function New-WinIsoNoPrompt {
|
|||||||
Result: UEFI boot proceeds straight into Windows Setup, no
|
Result: UEFI boot proceeds straight into Windows Setup, no
|
||||||
"Press any key to boot from CD" prompt.
|
"Press any key to boot from CD" prompt.
|
||||||
#>
|
#>
|
||||||
|
[CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')]
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory)] [string] $SourceIsoPath,
|
[Parameter(Mandatory)] [string] $SourceIsoPath,
|
||||||
[Parameter(Mandatory)] [string] $OutputIsoPath
|
[Parameter(Mandatory)] [string] $OutputIsoPath
|
||||||
)
|
)
|
||||||
|
if (-not $PSCmdlet.ShouldProcess($OutputIsoPath, 'Rebuild Windows ISO without boot prompt')) { return }
|
||||||
if (Test-Path $OutputIsoPath) { Remove-Item $OutputIsoPath -Force }
|
if (Test-Path $OutputIsoPath) { Remove-Item $OutputIsoPath -Force }
|
||||||
|
|
||||||
$mount = Mount-DiskImage -ImagePath $SourceIsoPath -PassThru -StorageType ISO -Access ReadOnly
|
$mount = Mount-DiskImage -ImagePath $SourceIsoPath -PassThru -StorageType ISO -Access ReadOnly
|
||||||
@@ -383,11 +387,13 @@ function Invoke-Vmrun {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function Set-VmxKey {
|
function Set-VmxKey {
|
||||||
|
[CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')]
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory)] [string] $VmxPath,
|
[Parameter(Mandatory)] [string] $VmxPath,
|
||||||
[Parameter(Mandatory)] [string] $Key,
|
[Parameter(Mandatory)] [string] $Key,
|
||||||
[Parameter(Mandatory)] [string] $Value
|
[Parameter(Mandatory)] [string] $Value
|
||||||
)
|
)
|
||||||
|
if (-not $PSCmdlet.ShouldProcess($VmxPath, "Set VMX key '$Key'")) { return }
|
||||||
$lines = Get-Content -LiteralPath $VmxPath
|
$lines = Get-Content -LiteralPath $VmxPath
|
||||||
$pattern = "^\s*$([regex]::Escape($Key))\s*="
|
$pattern = "^\s*$([regex]::Escape($Key))\s*="
|
||||||
$found = $false
|
$found = $false
|
||||||
@@ -400,8 +406,10 @@ function Set-VmxKey {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function Remove-VmxLines {
|
function Remove-VmxLines {
|
||||||
|
[CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')]
|
||||||
param([Parameter(Mandatory)] [string] $VmxPath,
|
param([Parameter(Mandatory)] [string] $VmxPath,
|
||||||
[Parameter(Mandatory)] [string] $KeyPrefix)
|
[Parameter(Mandatory)] [string] $KeyPrefix)
|
||||||
|
if (-not $PSCmdlet.ShouldProcess($VmxPath, "Remove VMX lines starting with '$KeyPrefix'")) { return }
|
||||||
$pat = "^\s*$([regex]::Escape($KeyPrefix))"
|
$pat = "^\s*$([regex]::Escape($KeyPrefix))"
|
||||||
$lines = Get-Content -LiteralPath $VmxPath | Where-Object { $_ -notmatch $pat }
|
$lines = Get-Content -LiteralPath $VmxPath | Where-Object { $_ -notmatch $pat }
|
||||||
Set-Content -LiteralPath $VmxPath -Value $lines -Encoding ASCII
|
Set-Content -LiteralPath $VmxPath -Value $lines -Encoding ASCII
|
||||||
@@ -428,8 +436,10 @@ function Invoke-VmrunBounded {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function Stop-Vm {
|
function Stop-Vm {
|
||||||
|
[CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')]
|
||||||
param([Parameter(Mandatory)] [string] $VmxPath,
|
param([Parameter(Mandatory)] [string] $VmxPath,
|
||||||
[int] $SoftTimeoutMinutes = 5)
|
[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.
|
# 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.
|
# Guest OS continues shutdown independently once the signal is received.
|
||||||
Invoke-VmrunBounded -Arguments @('-T','ws','stop',$VmxPath,'soft') -TimeoutSeconds 30
|
Invoke-VmrunBounded -Arguments @('-T','ws','stop',$VmxPath,'soft') -TimeoutSeconds 30
|
||||||
|
|||||||
Reference in New Issue
Block a user