new file: functions/Compress-File.ps1
new file: functions/StartStoper.ps1 new file: functions/Write-LogMessage.ps1
This commit is contained in:
parent
ed53008b4b
commit
76ec9778ca
61
functions/Compress-File.ps1
Normal file
61
functions/Compress-File.ps1
Normal file
@ -0,0 +1,61 @@
|
||||
# <>
|
||||
#
|
||||
# 1) Compress-File -Path C:\Users\userprofile\file.txt -Compression high -DeleteOriginal
|
||||
# 2) Compress-File -Path C:\Users\userprofile\file.txt -Compression medium
|
||||
#
|
||||
# 1 Will compress at High level and after succesfull compression will delete source file.
|
||||
# 2 Will just compress file at medium level.
|
||||
#
|
||||
function Compress-File {
|
||||
param (
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$Path,
|
||||
[Parameter(Mandatory=$false)]
|
||||
[ValidateSet('low', 'medium', 'high')]
|
||||
[string]$Compression = 'medium',
|
||||
[Parameter(Mandatory=$false)]
|
||||
[switch]$DeleteOriginal
|
||||
)
|
||||
|
||||
try {
|
||||
# Check if the file exists
|
||||
if (!(Test-Path -Path $Path -PathType Leaf)) {
|
||||
Write-Error "File $Path does not exist or is not a file."
|
||||
return
|
||||
}
|
||||
|
||||
# Set the compression level
|
||||
switch ($Compression) {
|
||||
'low' { $compressionLevel = [System.IO.Compression.CompressionLevel]::NoCompression }
|
||||
'medium' { $compressionLevel = [System.IO.Compression.CompressionLevel]::Fastest }
|
||||
'high' { $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal }
|
||||
}
|
||||
|
||||
# Get the file name
|
||||
$fileName = [System.IO.Path]::GetFileName($Path)
|
||||
$zipFileName = "$($fileName).zip"
|
||||
|
||||
# Create the path to the ZIP file
|
||||
$zipFilePath = Join-Path -Path (Split-Path -Parent -Path $Path) -ChildPath $zipFileName
|
||||
|
||||
# Compress the file to ZIP format
|
||||
$zipArchive = [System.IO.Compression.ZipFile]::Open($zipFilePath, [System.IO.Compression.ZipArchiveMode]::Create)
|
||||
$zipEntry = $zipArchive.CreateEntry($fileName)
|
||||
$stream = $zipEntry.Open()
|
||||
$fileStream = [System.IO.File]::OpenRead($Path)
|
||||
$fileStream.CopyTo($stream)
|
||||
$fileStream.Dispose()
|
||||
$stream.Dispose()
|
||||
$zipArchive.Dispose()
|
||||
|
||||
Write-Output "File $fileName has been compressed to $zipFilePath with compression level '$Compression'."
|
||||
|
||||
if ($DeleteOriginal) {
|
||||
Remove-Item -Path $Path -Force
|
||||
Write-Output "Original file $Path has been deleted."
|
||||
}
|
||||
}
|
||||
catch {
|
||||
Write-Error "An error occurred while compressing the file: $($_.Exception.Message)"
|
||||
}
|
||||
}
|
4
functions/StartStoper.ps1
Normal file
4
functions/StartStoper.ps1
Normal file
@ -0,0 +1,4 @@
|
||||
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
|
||||
# Some code block
|
||||
$stopwatch.Stop()
|
||||
Write-Host "Execution time: $($stopwatch.ElapsedMilliseconds) ms"
|
25
functions/Write-LogMessage.ps1
Normal file
25
functions/Write-LogMessage.ps1
Normal file
@ -0,0 +1,25 @@
|
||||
# <>
|
||||
#
|
||||
# 1) Write-LogMessage -level "INFO" -message "Some Messaage to LOG" -Path C:\Users\userprofile\file.txt
|
||||
# 2) Write-LogMessage -level "ERROR" -message "Some Messaage to LOG" -Path C:\Users\userprofile\file.txt
|
||||
#
|
||||
# 1 Will log directly INFO message
|
||||
# 2 Will log directly ERROR message and system $error[0] variable
|
||||
#
|
||||
|
||||
function Write-LogMessage {
|
||||
param (
|
||||
[ValidateSet("INFO", "WARNING", "ERROR")]
|
||||
[string]$Level,
|
||||
[string]$Message,
|
||||
[string]$Path
|
||||
)
|
||||
|
||||
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff"
|
||||
if ($Level -eq "ERROR") {
|
||||
$logMessage = "[$timestamp] [$Level] $Message | $($Error[0] | Select-Object *)"
|
||||
} else {
|
||||
$logMessage = "[$timestamp] [$Level] $Message"
|
||||
}
|
||||
$logMessage | Add-Content $Path -Encoding utf8
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user