-
Notifications
You must be signed in to change notification settings - Fork 0
/
autobackup.ps1
51 lines (43 loc) · 1.51 KB
/
autobackup.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
$sourceFolder = "$env:USERPROFILE\AppData\Roaming\HelloGames"
$destinationFolder = "$env:USERPROFILE\Desktop\NMS Backups"
# Without .exe
$gameProcessName = "NMS"
# Check if the game is running
function Is-GameRunning {
param([string]$processName)
return (Get-Process $processName -ErrorAction SilentlyContinue) -ne $null
}
# Create the destination folder if it does not exist
if (-not (Test-Path -Path $destinationFolder )) {
New-Item -ItemType Directory -Path $destinationFolder
}
function Copy-Folder {
param(
[String]$from,
[String]$to
)
Copy-Item -Path $from -Destination $to -Recurse -Force
}
# Check how many times the game is not running
$notRunningCount = 0
# Save every 5 minutes
while ($true) {
if (Is-GameRunning -processName $gameProcessName) {
Write-Host "NMS is running. Performing backup..."
# Start Saving
Copy-Folder -from $sourceFolder\* -to $destinationFolder
$backupFolderItem = Get-Item -Path $destinationFolder
$backupFolderItem.Refresh()
$backupFolderItem.LastWriteTime = Get-Date
Write-Host "Backup completed."
$notRunningCount = 0 # Reinit because the game is running
} else {
Write-Host "NMS is not running."
$notRunningCount++
if ($notRunningCount -ge 2) {
Write-Host "NMS has not been running for 2 consecutive checks. Stopping the script."
break # Stop the script
}
}
Start-Sleep -Seconds 300 # Wait 5 minutes before checking again
}