-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.ps1
More file actions
232 lines (203 loc) · 8.88 KB
/
Copy pathsetup.ps1
File metadata and controls
232 lines (203 loc) · 8.88 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
## content of .\setup.ps1 ##
<#PSScriptInfo
.VERSION 0.8
.AUTHOR Kai Krutscho
.PROJECTURI https://www.github.com/Kaimodo/PowerShell-Profile
.DESCRIPTION
Setup-Script for Power-Shell Profile
#>
#Requires -RunAsAdministrator
using namespace System.Management.Automation.Host
[CmdletBinding(SupportsShouldProcess=$true)]
Param()
begin {
$FunctionName = $MyInvocation.MyCommand.Name
Write-Verbose "$($FunctionName): Begin."
$TmpLocation = Get-Location
$TempErrAct = $ErrorActionPreference
$ErrorActionPreference = "Stop"
#region Helper-Function
function Get-TimeStamp {
Param(
[switch]$NoWrap,
[switch]$Utc
)
$dt = Get-Date
if ($Utc -eq $true) {
$dt = $dt.ToUniversalTime()
}
$str = "{0:MM/dd/yy} {0:HH:mm:ss}" -f $dt
if ($NoWrap -ne $true) {
$str = "[$str]"
}
return $str
}
function New-TemporaryDirectory {
$parent = [System.IO.Path]::GetTempPath()
[string] $name = [System.Guid]::NewGuid()
Write-Host "Parent: $($parent)"
Write-Host "Name: $($name)"
return New-Item -ItemType Directory -Path (Join-Path $parent $name)
}
#endregion
#region Environment
$FunctionName = $MyInvocation.MyCommand.Name
Write-Verbose "$(Get-TimeStamp):$($FunctionName): Begin."
[string]$logFilePrefix = "Setup_"
[string]$logFileDateFormat = "yyyyMMdd_HHmmss"
[int]$logFileRetentionDays = 30
$EnvPath = $ProfileRoot +"\Profile"
$Environment = (Get-Content ($EnvPath + "\environment.json") -Raw) | ConvertFrom-Json
if($Environment.Variables.Logging -eq $true){
[string]$logFileFolderPath = $EnvPath + $Environment.Variables.LogPath.ToString()
} else {
$logFileFolderPath = ""
}
$TempErrAct = $ErrorActionPreference
$ErrorActionPreference = $Environment.Variables.ErrorActionPreference.ToString()
Write-Host "$(Get-TimeStamp):$($FunctionName):ErrorActionPreference: $ErrorActionPreference" -ForegroundColor Blue
#endregion
#region initialization
if ($logFileFolderPath -ne "")
{
if (!(Test-Path -PathType Container -Path $logFileFolderPath)) {
Write-Output "$(Get-TimeStamp):$($FunctionName): Creating directory $logFileFolderPath"
New-Item -ItemType Directory -Force -Path $logFileFolderPath | Out-Null
} else {
$DatetoDelete = $(Get-Date).AddDays(-$logFileRetentionDays)
Get-ChildItem $logFileFolderPath | Where-Object { $_.Name -like "*$logFilePrefix*" -and $_.LastWriteTime -lt $DatetoDelete } | Remove-Item | Out-Null
}
$logFilePath = $logFileFolderPath + "\$logFilePrefix" + "-" + (Get-Date -Format $logFileDateFormat) + ".LOG"
# attempt to start the transcript log, but don't fail the script if unsuccessful:
try
{
Start-Transcript -Path $logFilePath -Append
}
catch [Exception]
{
Write-Warning "$(Get-TimeStamp):$($FunctionName): Unable to start Transcript: $($_.Exception.Message)"
$logFileFolderPath = ""
}
}
#endregion initialization
}
process {
try {
#region Copy Profile
Write-Verbose "$(Get-TimeStamp):$($FunctionName):Process"
if ($PSVersionTable.PSEdition -eq "Core" ) {
Write-Host "$(Get-TimeStamp):$($FunctionName):This Script is meant to be run on Windows PowerShell Desktop $($PSVersionTable.PSEdition)" -ForegroundColor Red
} else {
Write-Host "$(Get-TimeStamp):$($FunctionName):PSDesktop: $($PSVersionTable.PSVersion)" -ForegroundColor Green
$File = Get-ChildItem $PROFILE.CurrentUserCurrentHost
$FileLocation = $File.Directoryname.ToString()
Write-Host "$(Get-TimeStamp):$($FunctionName):FL: $($FileLocation)" -ForegroundColor Green
if (Test-Path -Path $FileLocation) {
Write-Host "$(Get-TimeStamp):$($FunctionName):Yes: $($FileLocation)" -ForegroundColor Green
} else {
Write-Host "$(Get-TimeStamp):$($FunctionName):No: $($FileLocation)" -ForegroundColor Red
Write-Host "$(Get-TimeStamp):$($FunctionName):Creating directory $FileLocation" -ForegroundColor Blue
New-Item -ItemType Directory -Force -Path $FileLocation | Out-Null
}
$Tmp=New-TemporaryDirectory
$Out="$($Tmp.FullName)\Profile.zip"
Invoke-WebRequest 'https://github.com/Kaimodo/PowerShell-Profile/archive/refs/heads/main.zip' -OutFile $Out
Write-Host "$(Get-TimeStamp):$($FunctionName):Tmp: $($Tmp)"
Set-Location $Tmp
Expand-Archive $Out
$TmpProf = Join-Path -Path $Tmp -ChildPath "Profile"
Write-Host $TmpProf
Set-Location $TmpProf
Rename-Item .\PowerShell-Profile-main .\PowerShell-Profile
Remove-Item .\Powershell-Profile\.vscode -Recurse -Force -Confirm:$false
Remove-Item .\Powershell-Profile\media -Recurse -Force -Confirm:$false
Remove-Item .\Powershell-Profile\.gitignore
Remove-Item .\Powershell-Profile\setup.ps1
Set-Location $Tmp
Remove-Item .\Profile.zip
$CopyLocation = Join-path $TmpProf "PowerShell-Profile\*"
Write-Host "$(Get-TimeStamp):$($FunctionName):CopyLocation: $($CopyLocation)"
Write-Host "$(Get-TimeStamp):$($FunctionName):DestinationPath: $($FileLocation)"
if (!(Test-Path -PathType Container -Path $FileLocation)) {
Write-Host "$(Get-TimeStamp):$($FunctionName):Creating directory $FileLocation" -ForegroundColor Blue
New-Item -ItemType Directory -Force -Path $FileLocation | Out-Null
}
Copy-Item -Path $CopyLocation -Destination $FileLocation -Recurse -Force -Confirm:$false
Set-Location $TmpLocation
$answer = $Host.UI.PromptForChoice('title', 'Delete Temporary Folder?', @('&Yes', '&No'), 1)
if ($answer -eq 0) {
Write-Host "$(Get-TimeStamp):$($FunctionName):Removing Folder: $($Tmp)" -ForegroundColor Green
try {
Remove-Item $Tmp -Recurse -Force -Confirm:$false -ErrorAction Stop
}catch [Exception] {
Write-Verbose "$(Get-TimeStamp):$($FunctionName):Process.catch"
Write-Host "$(Get-TimeStamp):$($FunctionName):Error on line $($_.InvocationInfo.ScriptLineNumber): $($_.Exception.Message)"
Write-Output $_.Exception|format-list -force
}
}else{
Write-Host "$(Get-TimeStamp):$($FunctionName):Folder: $($Tmp) has NOT been removed!" -ForegroundColor Red
}
}
#endregion
#TODO: Checkfrom here
#& $profile
#region OMP Install
winget install -e --accept-source-agreements --accept-package-agreements JanDeDobbeleer.OhMyPosh
#endregion
#region Font Install
# Get all installed font families
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$fontFamilies = (New-Object System.Drawing.Text.InstalledFontCollection).Families
# Check if CaskaydiaCove NF is installed
if ($fontFamilies -notcontains "CaskaydiaCove NF") {
# Download and install CaskaydiaCove NF
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile("https://github.com/ryanoasis/nerd-fonts/releases/download/v3.0.2/CascadiaCode.zip", ".\CascadiaCode.zip")
Expand-Archive -Path ".\CascadiaCode.zip" -DestinationPath ".\CascadiaCode" -Force
$destination = (New-Object -ComObject Shell.Application).Namespace(0x14)
Get-ChildItem -Path ".\CascadiaCode" -Recurse -Filter "*.ttf" | ForEach-Object {
If (-not(Test-Path "C:\Windows\Fonts\$($_.Name)")) {
# Install font
$destination.CopyHere($_.FullName, 0x10)
}
}
# Clean up
Remove-Item -Path ".\CascadiaCode" -Recurse -Force
Remove-Item -Path ".\CascadiaCode.zip" -Force
}
#endregion
#region Choco install
$answer = $Host.UI.PromptForChoice('Chocolatey', 'install Chocolatey?', @('&Yes', '&No'), 1)
if ($answer -eq 0) {
#yes
Write-Host "Installing Chocolatey" -ForegroundColor Blue
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
}
#endregion
#region WinGetUI
$answer = $Host.UI.PromptForChoice('Chocolatey', 'install WinGetUI?', @('&Yes', '&No'), 1)
if ($answer -eq 0) {
#yes
Write-Host "Installing WinGetUI" -ForegroundColor Blue
winget install -e --accept-source-agreements --accept-package-agreements SomePythonThings.WingetUIStore
}
#endregion
#region Infos
Write-Host "$(Get-TimeStamp):$($FunctionName): You have to make a Copy of:" -ForegroundColor Green
Write-Host "$(Get-TimeStamp):$($FunctionName):- environment.json.sample" -ForegroundColor Blue
Write-Host "$(Get-TimeStamp):$($FunctionName):- module.json.sample" -ForegroundColor Blue
Write-Host "$(Get-TimeStamp):$($FunctionName):And Rename both to .json-Files"
Write-Host "$(Get-TimeStamp):$($FunctionName):After that populute both with your favorite settings/Modules"
#endregion
}
catch [Exception] {
Write-Verbose "$(Get-TimeStamp):$($FunctionName): Process.catch"
Write-Host "$(Get-TimeStamp):$($FunctionName):Error on line $($_.InvocationInfo.ScriptLineNumber): $($_.Exception.Message)"
Write-Output $_.Exception|format-list -force
}
}
end {
Write-Verbose "$(Get-TimeStamp):$($FunctionName): End."
$ErrorActionPreference = $TempErrAct
if ($logFileFolderPath -ne "") { Stop-Transcript }
}