This repository was archived by the owner on Nov 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjob.ps1
More file actions
131 lines (127 loc) · 4.98 KB
/
job.ps1
File metadata and controls
131 lines (127 loc) · 4.98 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#requires -version 3.0
function Start-ModifyFile {
[CmdletBinding()]
param(
[string]
[ValidateScript({Test-Path -LiteralPath $_ -PathType "Leaf"})]
[Parameter(Mandatory)]
# File path for processing
$filePathOriginal,
[string]
[ValidateScript({(-not(Test-Path $_))-or($PSBoundParameters.Keys -contains 'force')})]
[Parameter(Mandatory)]
# Tmp file path
$filePathTemp,
[string]
[ValidateScript({Test-Path $_ -PathType "Leaf"})]
[Parameter(Mandatory)]
# Program to run
$exePath,
[Parameter(Mandatory)]
# Program args
$exeArgs,
# Set replace original file
[bool]$replaceOriginal=$false,
# Set alternative NTFS stream name for saving info
[string]$streamName='ns.mod',
# replace tmp files
[switch]$force
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'stop'
function Copy-Property{
[CmdletBinding()]
param([Parameter(ValueFromPipeline=$true)]$InputObject,
$SourceObject,
[string[]]$Property,
[switch]$Passthru)
$passthruHash=@{Passthru=$passthru.IsPresent}
$propHash=@{}
$property | Foreach-Object {$propHash+=@{$_=$SourceObject.$_}}
$inputObject | Add-Member -NotePropertyMembers $propHash @passthruHash
}
# create result hashtable
$result=[ordered]@{}
$result.filePathOriginal = $filePathOriginal
$result.filePathTemp = $filePathTemp
$result.exitCode = 255
$result.errorMessage = $null
$result.replaceOriginal = $replaceOriginal
$result.log = @()
# print all param
foreach ($key in $PSBoundParameters.Keys)
{
$result.log += ("Args: $($key) = $($PSBoundParameters[$key])")
}
# set process param
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = $exePath
$pinfo.RedirectStandardError = $false
$pinfo.RedirectStandardOutput = $false
$pinfo.UseShellExecute = $false
$pinfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden
# hide window
$pinfo.CreateNoWindow = $true
$pinfo.Arguments = $exeArgs
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
try {
$result.log += ("Start process $($pinfo.FileName)")
$result.log += ("Start args $($pinfo.Arguments)")
$p.Start() | Out-Null
$p.WaitForExit()
$result.exitcode = $p.exitcode
# check compact ok
if ($result.exitcode -ne 0)
{
$result.log += ("Process exit with exitcode $($p.exitcode)")
#$result.errormessage = $($p.StandardError.ReadToEnd().trim())
#Clean tmp file
if (Test-Path $filePathTemp)
{
$result.log += ("Remove tmp file $($filePathTemp)")
Remove-Item $filePathTemp
}
}
else
{
$result.log += ("Process exitcode 0")
if ($replaceOriginal)
{
$result.log += ("Select - Remove original file")
$fileinfo_original = get-item -LiteralPath $filePathOriginal
$fileinfo_temp = get-item -LiteralPath $filePathTemp
$fileinfo_stream = New-Object psobject
$fileinfo_stream | Copy-Property -SourceObject $fileinfo_original -Property fullname,length,CreationTime,LastAccessTime,LastWriteTime -Passthru | out-null
$fileinfo_stream | Add-Member -MemberType NoteProperty -Name 'Compressed' -Value $true
#in some case tmp file size over original (
if ($fileinfo_original.length -le $fileinfo_temp.length)
{
$result.log += ("Original file size smaller then temp file size")
$result.log += ("Write info to alt NTFS stream in file $filePathOriginal")
convertto-json $fileinfo_stream | Set-Content -LiteralPath $filePathOriginal -stream $streamName -force
$result.log += ("Remove tmp file $($filePathTemp)")
remove-item -LiteralPath $filePathTemp
$result.replaceOriginal = $false
}
else
{
$result.log += ("Write info to alt NTFS stream in file $filePathOriginal")
convertto-json $fileinfo_stream | Set-Content -LiteralPath $filePathTemp -stream $streamName -force
$result.log += ("Move to original file $($filePathOriginal)")
Move-Item -LiteralPath $filePathTemp -Destination $filePathOriginal -Force
$result.replaceOriginal = $true
}
}
}
}
catch
{
$result.errorMessage = $_.Exception.Message
}
finally
{
# Return result object
New-Object -TypeName psobject -Property $result
}
}