-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcompress-file.ps1
111 lines (100 loc) · 3.17 KB
/
compress-file.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<#
.Synopsis
Use 7-zip to compress a remote shared directory
.DESCRIPTION
Use 7-zip to compress a remote shared directory
.EXAMPLE
Example of how to use this cmdlet
.EXAMPLE
Another example of how to use this cmdlet
.INPUTS
Inputs to this cmdlet (if any)
.OUTPUTS
Output from this cmdlet (if any)
.NOTES
General notes
.COMPONENT
The component this cmdlet belongs to
.ROLE
The role this cmdlet belongs to
.FUNCTIONALITY
The functionality that best describes this cmdlet
#>
function Compress-File
{
[CmdletBinding(PositionalBinding=$true,
SupportsShouldProcess=$True)]
[OutputType([String])]
Param
(
# Param1 help description
[Parameter(#Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]
$server = 'uhserver1'.Replace('\\','')
, # Param1 help description
[Parameter(#Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]
$share = 'globalshared'
, # Param2 help description
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$true)]
[string]
$destination = 'TS-XL19C\share\'.Replace('\\','')
, # Param2 help description
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$true)]
[Alias('filename')]
[string]
$name = $share.Replace('\', '-')
, # Param2 help description
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$true)]
[string]
$filter = '*.*'
)
Begin
{
# Alias for 7-zip, use 7zG.exe for GUI progress
if (-not (Test-Path "$env:ProgramFiles\7-Zip\7z.exe"))
{
throw "$env:ProgramFiles\7-Zip\7z.exe needed"
}
Set-Alias sz "$env:ProgramFiles\7-Zip\7z.exe"
$source = Join-Path -Path $server `
-ChildPath $share |
Join-Path -ChildPath $filter
$destination = Join-Path -path $destination `
-childPath ("$name-" + (Get-Date -Format dd-MM-yyy))
# Error checking access to files
if (-not (Test-Path (Split-Path "\\$source")) )
{
throw "$source not accecible"
}
if (-not (Test-Path (Split-Path "\\$destination")) )
{
throw "$destination not accecible"
}
}
Process
{
Write-Verbose "Executing: sz a -t7z -r '\\$destination.7z' '\\$source'"
Invoke-Expression "sz a -t7z -r '\\$destination.7z' '\\$source'"
}
End
{
#cleanup files if needed
}
}
compress-file -server "xps-laptop" -share "share\fog_0.32" -Verbose -WhatIf