-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStratoHiDriveUtils.psm1
More file actions
188 lines (151 loc) · 4.87 KB
/
Copy pathStratoHiDriveUtils.psm1
File metadata and controls
188 lines (151 loc) · 4.87 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<#
.SYNOPSIS
PowerShell module to control STRATO HiDrive and read the configured sync root.
.DESCRIPTION
Provides functions to start and stop the HiDrive desktop app and to determine the
current sync root folder from HiDrive log files.
.EXAMPLE
Import-Module .\StratoHiDriveUtils.psd1 -Force
Start-HiDrive
Loads the module from the current directory and starts HiDrive.
.EXAMPLE
Import-Module .\StratoHiDriveUtils.psd1 -Force
Stop-HiDrive
Loads the module and stops all running HiDrive processes.
.EXAMPLE
Import-Module .\StratoHiDriveUtils.psd1 -Force
Get-HiDriveSyncRoot
Returns the sync root path directly, for example:
C:\Users\<User>\HiDrive
If no entry is available in logs, the function returns $null.
.EXAMPLE
Import-Module .\StratoHiDriveUtils.psd1 -Force
$syncRoot = Get-HiDriveSyncRoot
if ($null -ne $syncRoot) {
"Sync root: $syncRoot"
} else {
"No sync root entry found in HiDrive logs."
}
Loads the module and reads the current HiDrive sync root from logs.
#>
Set-StrictMode -Version Latest
function Start-HiDrive {
<#
.SYNOPSIS
Starts the STRATO HiDrive desktop application.
.DESCRIPTION
Checks common installation paths and starts HiDrive if the executable exists.
Throws if no executable is found.
#>
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
[OutputType([void])]
param()
$installationRoots = @(
$env:ProgramFiles
${env:ProgramFiles(x86)}
$env:LocalAppData
) | Where-Object { $_ }
$hiDrivePotentialPaths = foreach ($root in $installationRoots) {
Join-Path $root 'STRATO\HiDrive\HiDrive.App.exe'
}
$hiDrivePath = $hiDrivePotentialPaths |
Where-Object { Test-Path -LiteralPath $_ } |
Select-Object -First 1
if (-not $hiDrivePath) {
throw 'HiDrive.App.exe was not found in known installation paths.'
}
if ($PSCmdlet.ShouldProcess($hiDrivePath, 'Start HiDrive application')) {
Start-Process -FilePath $hiDrivePath -ErrorAction Stop | Out-Null
}
}
function Stop-HiDrive {
<#
.SYNOPSIS
Stops the STRATO HiDrive desktop application.
.DESCRIPTION
Stops all running processes with HiDrive in the process name.
First requests a graceful close for UI processes and then, after a short wait,
forces termination of any remaining matching processes.
#>
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
[OutputType([void])]
param()
$processes = Get-Process -Name '*HiDrive*' -ErrorAction SilentlyContinue
if (-not $processes) {
return
}
$gracefulProcesses = $processes | Where-Object { $_.MainWindowHandle -ne 0 }
foreach ($process in $gracefulProcesses) {
if ($PSCmdlet.ShouldProcess("$($process.ProcessName) (PID $($process.Id))", 'Request graceful shutdown')) {
$null = $process.CloseMainWindow()
}
}
if ($gracefulProcesses) {
Start-Sleep -Milliseconds 1500
}
$remainingProcesses = Get-Process -Name '*HiDrive*' -ErrorAction SilentlyContinue
if (-not $remainingProcesses) {
return
}
foreach ($process in $remainingProcesses) {
if ($PSCmdlet.ShouldProcess("$($process.ProcessName) (PID $($process.Id))", 'Force stop remaining process')) {
Stop-Process -Id $process.Id -Force -ErrorAction SilentlyContinue
}
}
}
function Get-HiDriveSyncRoot {
<#
.SYNOPSIS
Returns the HiDrive sync root folder path.
.DESCRIPTION
Reads HiDrive logs and extracts the latest root folder path from
"FileSystemSnapshot: Get file system snapshot started. Root ... |" entries.
Returns $null if no matching log entry exists.
.OUTPUTS
System.String or $null
#>
[CmdletBinding()]
[OutputType([string])]
param()
$pattern = 'FileSystemSnapshot: Get file system snapshot started\. Root (?<Root>.+?)\s*\|'
$logRoot = Join-Path $env:LOCALAPPDATA 'HiDrive\Logs'
$currentLogPath = Join-Path $logRoot 'log.txt'
if (Test-Path -LiteralPath $currentLogPath) {
try {
$match = Get-Content -LiteralPath $currentLogPath -ErrorAction Stop |
Select-String -Pattern $pattern |
Select-Object -Last 1
if ($match) {
return $match.Matches[0].Groups['Root'].Value.Trim()
}
}
catch {
# Continue with legacy fallback if the current log cannot be read.
}
}
$dataRoot = Join-Path $env:LOCALAPPDATA 'HiDrive\Data'
if (Test-Path -LiteralPath $dataRoot) {
$candidateDirectories = Get-ChildItem -LiteralPath $dataRoot -Directory -Recurse -Force -ErrorAction SilentlyContinue |
Where-Object { $_.Name -match '^\d+\.\d+$' } |
Sort-Object LastWriteTime -Descending
foreach ($directory in $candidateDirectories) {
$syncLogPath = Join-Path $directory.FullName 'syncLog.txt'
if (-not (Test-Path -LiteralPath $syncLogPath)) {
continue
}
try {
$match = Get-Content -LiteralPath $syncLogPath -ErrorAction Stop |
Select-String -Pattern $pattern |
Select-Object -Last 1
if ($match) {
return $match.Matches[0].Groups['Root'].Value.Trim()
}
}
catch {
continue
}
}
}
return $null
}
Export-ModuleMember -Function Start-HiDrive, Stop-HiDrive, Get-HiDriveSyncRoot