From 4cc7e9172288ca092dba893cad7a50fa2aea82a9 Mon Sep 17 00:00:00 2001 From: David Yoder Date: Fri, 6 Oct 2023 13:04:09 -0400 Subject: [PATCH] too many updates --- Tools.ps1 | 141 +++++++++++++++++++++++++++++------------------------- 1 file changed, 76 insertions(+), 65 deletions(-) diff --git a/Tools.ps1 b/Tools.ps1 index bd8d9f9..1bdd7ec 100644 --- a/Tools.ps1 +++ b/Tools.ps1 @@ -270,10 +270,21 @@ Function IsWindowsBuild { Function Get-TempPath { Return [System.IO.Path]::GetTempPath() } # Get a random file name with random extension -Function Get-RandomFile { Return [System.IO.Path]::GetRandomFileName() } +Function Get-RandomFileName { Return [System.IO.Path]::GetRandomFileName() } -# Get a random temp file in the current TEMP folder -Function Get-RandomTempFile { Return [System.IO.Path]::Combine($(Get-TempPath), $(Get-RandomFile)) } +# Create a new temp file in the current TEMP folder +Function New-TemporaryFile { + $file = [System.IO.Path]::Combine($(Get-TempPath), $(Get-RandomFileName)) + New-Item -Path $file -ItemType File -Force -ErrorAction SilentlyContinue | Out-Null + Return Get-Item -Path $file +} + +# Create a new temp directory in the current TEMP folder +Function New-TemporaryDirectory { + $directory = [System.IO.Path]::Combine($(Get-TempPath), $(Get-RandomFileName)) + New-Item -Path $directory -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null + Return Get-Item -Path $directory +} # Determine if the system is 64-bit or not Function Is64bit { @@ -449,7 +460,7 @@ Function End-Process } } -Function Create-Shortcut { +Function New-Shortcut { param( [Parameter(Mandatory=$true)][string]$Path, [Parameter(Mandatory=$true)][string]$TargetPath, @@ -480,33 +491,26 @@ Function Create-Shortcut { Else { Write-Output "Cannot create shortcut because the target path does not exist" } } -# Function to enable or disable a specified log -Function Toggle-Log { - param ( - [Parameter(Mandatory = $true)][string]$Name, - [Parameter(Mandatory = $true, ParameterSetName = 'Enable')][switch]$Enable, - [Parameter(Mandatory = $true, ParameterSetName = 'Disable')][switch]$Disable - ) +# Enable a Windows Event Log +Function Enable-Log { + param ([Parameter(Mandatory = $true,ValueFromPipeline=$true)][string]$Name) Try { - $log = New-Object System.Diagnostics.Eventing.Reader.EventLogConfiguration $Name - If ( $Enable ) { Write-Output "Enabing ${Name} event log" ; $log.IsEnabled = $true } - If ( $Disable ) { Write-Output "Disabing ${Name} event log" ; $log.IsEnabled = $false } - $log.SaveChanges() + $log = New-Object System.Diagnostics.Eventing.Reader.EventLogConfiguration $Name + Write-Output "Enabing ${Name} event log" + $log.IsEnabled = $true + $log.SaveChanges() } - Catch { Write-Output $_.Exception.Message } + Catch { Write-Error $_.Exception.Message } } -# Function to install the PSAtera Powershell module -Function Install-PSAteraModule { +# Disable a Windows Event Log +Function Disable-Log { + param ([Parameter(Mandatory = $true,ValueFromPipeline=$true)][string]$Name) Try { - $module = Get-Module -ListAvailable -Name PSAtera - If ( (-not $module) -and (IsAdmin) ) { - Write-Output "Installing PSAtera Powershell module" - Install-Module -Name PSAtera -Force - } - ElseIf ( (-not $module) -and (-not (IsAdmin)) ) { - Write-Output "The PSAtera module needs to be installed, but the current security context is not sufficient to install it" - } + $log = New-Object System.Diagnostics.Eventing.Reader.EventLogConfiguration $Name + Write-Output "Disabing ${Name} event log" + $log.IsEnabled = $false + $log.SaveChanges() } Catch { Write-Error $_.Exception.Message } } @@ -527,51 +531,58 @@ Function Install-MSP360Module { Catch { Write-Error $_.Exception.Message } } - # Function to delete a file -Function Remove-File ([Parameter(ValueFromPipeline=$true)][string]$path) { - If ( Test-Path $path ) { - If ( !((Get-Item $path) -is [System.IO.DirectoryInfo]) ) { - Try { - Write-Output "Delete ""${path}""" - Remove-Item $path -Force - } - Catch { Write-Error $_.Exception.Message } - } - } -} - -# Function to ZIP a file or directory -Function Zip-Object { +# Function to ZIP a file or directory, returns the newly created zip file +Function Compress-Archive { param( - [Parameter(ValueFromPipeline=$true)][string]$path, - [Parameter(ValueFromPipeline=$false)][string]$dest + [Parameter(ValueFromPipeline=$true)][string]$Path, + [Parameter(ValueFromPipeline=$false)][string]$DestinationPath, + [Parameter(ValueFromPipeline=$false)][switch]$Force=$false ) - If ( $(Get-Item $path) -is [System.IO.DirectoryInfo] ) { - If ( [string]::IsNullOrEmpty($dest) ) { $dest = '{0}\{1}.zip' -f $(Split-Path -Parent $path),$(Split-Path -Leaf $path) } - Write-Output "Creating zip file of: ${path}" - Try { - Add-Type -Assembly "System.IO.Compression.FileSystem" - [IO.Compression.ZipFile]::CreateFromDirectory($path, $dest) - } - Catch { Write-Error $_.Exception.Message } - If ( Test-Path $dest ) { Return $dest } Else { Return $false } + BEGIN { + If ( ($Force) -and (Test-Path $DestinationPath) ) { + Write-Output "Deleting existing archive: ""${DestinationPath}""" + Remove-Item -Path $DestinationPath -Force -ErrorAction Stop } - ElseIf ( $(Get-Item $path) -is [System.IO.FileInfo] ) { - $dir = $path.Substring(0, $path.LastIndexOf('.')) - Try { - Write-Output "Creating directory: ${path}" - New-Item -ItemType Directory -Path $dir -ErrorAction Stop | Out-Null - Write-Output "Moving ""${path}"" into ""${dir}""" - Move-Item -Path $path -Destination $dir -Force -ErrorAction Stop | Out-Null - } - Catch { Write-Error $_.Exception.Message } - Zip-Object $dir } - Else { Write-Output "Could not determine the file/folder status: ${path}" } + PROCESS { + If ( !($Force) -and (Test-Path $DestinationPath) ) { $CreateNewArchive = $false } Else { $CreateNewArchive = $true } + Try { + Add-Type -Assembly "System.IO.Compression" + Add-Type -Assembly "System.IO.Compression.FileSystem" + Switch ( $(Get-Item -Path $Path) ) { + {$_ -is [System.IO.DirectoryInfo]} { + If ( $CreateNewArchive ) { + Write-Output "Creating new zip archive: ""${DestinationPath}""" + [System.IO.Compression.ZipFile]::CreateFromDirectory($Path, $DestinationPath, [System.IO.Compression.CompressionLevel]::Optimal, $true) + } Else { + If ( (Get-Command Microsoft.Powershell.Archive\Compress-Archive -ErrorAction SilentlyContinue) ) { + Microsoft.Powershell.Archive\Compress-Archive -Path $Path -DestinationPath $DestinationPath -Update -CompressionLevel Optimal + } Else { + Write-Error "Updating an existing zip archive with a folder structure is not currently supported" + } + } + } + {$_ -is [System.IO.FileInfo]} { + If ( $CreateNewArchive ) { + Write-Output "Creating new zip archive: ""${DestinationPath}""" + $ArchiveMode = [System.IO.Compression.ZipArchiveMode]::Create + } Else { $ArchiveMode = [System.IO.Compression.ZipArchiveMode]::Update } + [System.IO.Compression.ZipArchive]$DestinationArchive = [System.IO.Compression.ZipFile]::Open($DestinationPath, $ArchiveMode) + [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($DestinationArchive, $Path, (Split-Path $Path -Leaf)) | Out-Null + $DestinationArchive.Dispose() + } + } + } + Catch { Write-Error $_.Exception.Message } + } + END { + If ( !$CreateNewArchive ) { Write-Output "Finished updating existing archive: ""${DestinationPath}""" } + If ( Test-Path $DestinationPath ) { Return Get-Item -Path $DestinationPath } Else { Return $null } + } } # Function to UNZIP a file or directory -Function Unzip-Object ([Parameter(ValueFromPipeline=$true)][string]$path) { +Function Expand-Archive ([Parameter(ValueFromPipeline=$true)][string]$path) { If ( [System.IO.Path]::GetExtension($path) -eq ".zip" ) { $dest = Split-Path -Parent $path Write-Output "Extracting ""${path}"" to ""${dest}""" @@ -601,7 +612,7 @@ Function Get-LiquidFilesCLI { Catch { Write-Error $_.Exception.Message } } Download-File -URL $url -File $zip_path | Out-Null - Unzip-Object $zip_path + Expand-Archive $zip_path Return $exe_path }