system-utils.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#
# system_utils.ps1
#
#http://www.adminarsenal.com/admin-arsenal-blog/powershell-zip-up-files-using-net-and-add-type/
function Zip-Directory {
    Param(
      [Parameter(Mandatory=$True)][string]$DestinationFileName,
      [Parameter(Mandatory=$True)][string]$SourceDirectory,
      [Parameter(Mandatory=$False)][string]$CompressionLevel = "Optimal",
      [Parameter(Mandatory=$False)][switch]$IncludeParentDir
    )
Write-Verbose "Compress directory $SourceDirectory to $DestinationFileName"
    Add-Type -AssemblyName System.IO.Compression.FileSystem
    $CompressionLevel    = [System.IO.Compression.CompressionLevel]::$CompressionLevel  
    [System.IO.Compression.ZipFile]::CreateFromDirectory($SourceDirectory, $DestinationFileName, $CompressionLevel, $IncludeParentDir)
}


function UnZip-Directory {
    Param(
      [Parameter(Mandatory=$True)][string]$SourceZipFile,
      [Parameter(Mandatory=$True)][string]$DestinationDirectory
    )

    Add-Type -AssemblyName System.IO.Compression.FileSystem
    
    [System.IO.Compression.ZipFile]::ExtractToDirectory($SourceZipFile, $DestinationDirectory)
}

function Backup-File {
[CmdletBinding(SupportsShouldProcess=$true)]
 Param(
[Parameter(Mandatory=$True)]
[string]$SourceFile
    )

#More about date formating - https://technet.microsoft.com/en-us/library/ee692801.aspx
$date = Get-Date -Format d-M-yyyy-HH-MM-ss

$backupFile = [System.IO.Path]::GetFileNameWithoutExtension($SourceFile) + "_" +  $date + ".bak"
$backupFolder = [System.IO.Path]::GetDirectoryName($SourceFile)

$backupPath = Join-Path -Path $backupFolder -ChildPath $backupFile

Copy-Item -Path $SourceFile -Destination $backupPath
}


function Set-FilesReadOnly
{
Param(
[Parameter(Mandatory=$true)]
[string]$Path
)
Set-ItemProperty -Path $Path -Name IsReadOnly -Value $true
}