-
Notifications
You must be signed in to change notification settings - Fork 1
/
task_script.ps1
44 lines (41 loc) · 1.42 KB
/
task_script.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
#!/usr/bin/env pwsh
# Path to the script folder
param([string]$scriptFolder = "")
# So that the script can be run manually
if (!($scriptFolder)) {
$scriptFolder = Split-Path -Parent $MyInvocation.MyCommand.Path
}
# Path to the folder with shortcuts
$pathToLinks = "$scriptFolder/links"
# Checks if the file is locked by any process
function isLockedBySomeProcesses ($fileName) {
Get-Process | ForEach-Object {
$_.Modules | ForEach-Object {
if ($_.FileName -eq $exePath) {
return 1
}
}
}
return 0
}
# For each file in the folder with shortcuts
$files = (Get-ChildItem "$pathToLinks")
foreach ($filePath in $files) {
# Consider only labels
if ($filePath -match "^(.*).lnk$") {
$fileName = ($filePath | ForEach-Object { $_.Name })
$fileFullPath = "$pathToLinks/$fileName"
# Path to the exe file
$exePath = (New-Object -ComObject WScript.Shell).CreateShortcut("$fileFullPath").TargetPath
if ((Test-Path -Path "$exePath") -eq $false) {
Write-Error "The file `"$exePath`" does not exist!"
} else {
if (isLockedBySomeProcesses("$exePath")) {
Write-Warning "The `"$exePath`" process is already running!"
} else {
Start-Process -FilePath "$fileFullPath" -Verb RunAs
Write-Host "The process `"$exePath`" has started!"
}
}
}
}