diff --git a/functions/Compress-File.ps1 b/functions/Compress-File.ps1 new file mode 100644 index 0000000..291277f --- /dev/null +++ b/functions/Compress-File.ps1 @@ -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)" + } +} \ No newline at end of file diff --git a/functions/StartStoper.ps1 b/functions/StartStoper.ps1 new file mode 100644 index 0000000..8043804 --- /dev/null +++ b/functions/StartStoper.ps1 @@ -0,0 +1,4 @@ +$stopwatch = [System.Diagnostics.Stopwatch]::StartNew() +# Some code block +$stopwatch.Stop() +Write-Host "Execution time: $($stopwatch.ElapsedMilliseconds) ms" \ No newline at end of file diff --git a/functions/Write-LogMessage.ps1 b/functions/Write-LogMessage.ps1 new file mode 100644 index 0000000..f816cd9 --- /dev/null +++ b/functions/Write-LogMessage.ps1 @@ -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 + } \ No newline at end of file