-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.ps1
99 lines (87 loc) · 2.99 KB
/
utils.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
function CreateDirIfNotExist([string]$dirName)
{
if (!(Test-Path $dirName -PathType Container))
{
New-Item -ItemType Directory -Force -Path $dirName | Out-Null
}
}
function CommandExists([string]$cmdName)
{
$local:return = Get-Command $cmdName -ErrorAction SilentlyContinue
return $local:return
}
function ScheduledTaskExists([string]$taskName)
{
$local:taskExists = Get-ScheduledTask | Where-Object {$_.TaskName -like $taskName }
return $local:taskExists
}
function WaitForProcessToStart([string]$procName)
{
$running = $false;
while( $running -eq $false ) {
$running = ( ( Get-Process | Where-Object ProcessName -eq $procName).Length -gt 0);
Start-Sleep -s 1
}
}
function lns([String]$link, [String]$target) {
$file = Get-Item $link -ErrorAction SilentlyContinue
$toFile = Get-Item $target -ErrorAction SilentlyContinue
$target = $toFile.FullName
if($file) {
if ($file.LinkType -ne "SymbolicLink") {
Write-Warn "$($file.FullName) already exists and is not a symbolic link creating a backup"
$newName = $file.Name #my powershellfu is not strong at this point...
Rename-Item -Path $file.FullName -NewName "$newName.bck"
} elseif ($file.Target -ne $target) {
Write-Error "$($file.FullName) already exists and points to '$($file.Target)', it should point to '$target'"
Remove-Item $file
lns $link $target
} else {
Write-Warn "$($file.FullName) already linked"
return
}
} else {
$folder = Split-Path $link
if(-not (Test-Path $folder)) {
Write-Output "Creating folder $folder"
New-Item -Type Directory -Path $folder
}
}
Write-Output "Creating link $link to $target"
(New-Item -Path $link -ItemType SymbolicLink -Value $target -ErrorAction Continue) | Out-Null
}
function SetEnvVariable([string]$target, [string]$name, [string]$value) {
$existing = [Environment]::GetEnvironmentVariable($name,$target)
if($existing) {
Write-Warn "Environment variable $name already set to '$existing'"
} else {
Write-Output "Adding the $name environment variable to '$value'"
[Environment]::SetEnvironmentVariable($name, $value, $target)
}
}
function Write-Error([string]$message) {
[Console]::ForegroundColor = 'red'
[Console]::Error.WriteLine($message)
[Console]::ResetColor()
}
function Write-Warn([string]$message) {
[Console]::ForegroundColor = 'yellow'
[Console]::Error.WriteLine($message)
[Console]::ResetColor()
}
function Install([String]$package, [string]$params = "") {
if(-not ((choco list $package --exact --local-only --limitoutput) -like "$package*")) {
Write-Output "Installing package $package"
if ($params -eq "")
{
choco install $package -y --limit-output
}
else
{
choco install $package -y --params $params --limit-output
}
} else {
Write-Warn "Package $package already installed, checking for update."
choco upgrade $package -y --limit-output
}
}