|
43 | 43 | .PARAMETER NotInternal |
44 | 44 | Do not use the internally provided PowerShellGet module versions. |
45 | 45 | This REQUIRES you to either provide the module data via -SourcePath or to have live online access. |
| 46 | +
|
| 47 | + .PARAMETER UserMode |
| 48 | + Deploy the resource into user paths, rather than computer-wide. |
| 49 | + This allows bootstrapping _without_ requiring elevation and is usually only needed on the local computer. |
| 50 | + This mode is automatically selected when deploying to the local computer and not running PowerShell "As Administrator". |
| 51 | + Only applies to / affects Windows computers. |
46 | 52 | |
47 | 53 | .EXAMPLE |
48 | 54 | PS C:\> Install-PSFPowerShell -Type V3Latest -ComputerName (Get-ADComputer -Filter * -SearchBase $myOU) |
|
69 | 75 | $Offline, |
70 | 76 |
|
71 | 77 | [switch] |
72 | | - $NotInternal |
| 78 | + $NotInternal, |
| 79 | + |
| 80 | + [switch] |
| 81 | + $UserMode |
73 | 82 | ) |
74 | 83 |
|
75 | 84 | begin { |
|
161 | 170 |
|
162 | 171 | $actualConfiguration = Import-PSFPowerShellDataFile -Path (Join-Path -Path $rootPath -ChildPath 'modules.json') |
163 | 172 | $data = @{ |
164 | | - Type = $Type |
| 173 | + Type = $Type |
165 | 174 | Config = $actualConfiguration |
166 | 175 | } |
167 | 176 | switch ($Type) { |
|
180 | 189 | #region Actual Code |
181 | 190 | $code = { |
182 | 191 | param ( |
183 | | - $Data |
| 192 | + $Data, |
| 193 | + |
| 194 | + $AsCurrentUser |
184 | 195 | ) |
185 | 196 |
|
186 | 197 | #region Functions |
|
233 | 244 | #region V2 Bootstrap |
234 | 245 | V2Binaries { |
235 | 246 | if ($isOnWindows) { |
236 | | - if (-not (Test-Path -Path "$env:ProgramFiles\Microsoft\Windows\PowerShell\PowerShellGet")) { |
237 | | - $null = New-Item -Path "$env:ProgramFiles\Microsoft\Windows\PowerShell\PowerShellGet" -ItemType Directory -Force |
| 247 | + $getRoot = "$env:ProgramFiles\Microsoft\Windows\PowerShell\PowerShellGet" |
| 248 | + if ($AsCurrentUser) { $getRoot = "$env:LocalAppData\Microsoft\Windows\PowerShell\PowerShellGet" } |
| 249 | + if (-not (Test-Path -Path $getRoot)) { |
| 250 | + $null = New-Item -Path $getRoot -ItemType Directory -Force |
238 | 251 | } |
239 | | - Copy-Item -Path (Join-Path -Path $tempFolder -ChildPath 'NuGet.exe') -Destination "$env:ProgramFiles\Microsoft\Windows\PowerShell\PowerShellGet" -Force |
240 | | - if (-not (Test-Path -Path "$env:ProgramFiles\PackageManagement\ProviderAssemblies\nuget\2.8.5.208")) { |
241 | | - $null = New-Item -Path "$env:ProgramFiles\PackageManagement\ProviderAssemblies\nuget\2.8.5.208" -ItemType Directory -Force |
| 252 | + Copy-Item -Path (Join-Path -Path $tempFolder -ChildPath 'NuGet.exe') -Destination $getRoot -Force |
| 253 | + |
| 254 | + $nugetRoot = "$env:ProgramFiles\PackageManagement\ProviderAssemblies\nuget\2.8.5.208" |
| 255 | + if ($AsCurrentUser) { $nugetRoot = "$env:LOCALAPPDATA\PackageManagement\ProviderAssemblies\nuget\2.8.5.208"} |
| 256 | + if (-not (Test-Path -Path $nugetRoot)) { |
| 257 | + $null = New-Item -Path $nugetRoot -ItemType Directory -Force |
242 | 258 | } |
243 | | - Copy-Item -Path (Join-Path -Path $tempFolder -ChildPath 'Microsoft.PackageManagement.NuGetProvider.dll') -Destination "$env:ProgramFiles\PackageManagement\ProviderAssemblies\nuget\2.8.5.208" -Force |
| 259 | + Copy-Item -Path (Join-Path -Path $tempFolder -ChildPath 'Microsoft.PackageManagement.NuGetProvider.dll') -Destination $nugetRoot -Force |
244 | 260 | } |
245 | 261 | else { |
246 | 262 | Copy-Item -Path (Join-Path -Path $tempFolder -ChildPath 'NuGet.exe') -Destination "$HOME/.config/powershell/powershellget" -Force |
|
251 | 267 | #region V2 Latest |
252 | 268 | V2Latest { |
253 | 269 | $modulesFolder = "$env:ProgramFiles\WindowsPowerShell\modules" |
| 270 | + if ($AsCurrentUser) { |
| 271 | + $modulesFolder = $env:PSModulePath -split ';' | Microsoft.PowerShell.Core\Where-Object { $_ -match '\\Documents\\' } | Microsoft.PowerShell.Utility\Select-Object -First 1 |
| 272 | + if (-not $modulesFolder) { $env:PSModulePath -split ';' | Microsoft.PowerShell.Utility\Select-Object -First 1 } |
| 273 | + } |
254 | 274 | if (-not $isOnWindows) { $modulesFolder = "/usr/local/share/powershell/Modules" } |
255 | 275 |
|
256 | 276 | Install-ZipModule -Config $data.Config.PSGetV2 -ModulesFolder $modulesFolder -TempFolder $tempFolder |
|
261 | 281 | #region V3 Latest |
262 | 282 | V3Latest { |
263 | 283 | $modulesFolder = "$env:ProgramFiles\WindowsPowerShell\modules" |
| 284 | + if ($AsCurrentUser) { |
| 285 | + $modulesFolder = $env:PSModulePath -split ';' | Microsoft.PowerShell.Core\Where-Object { $_ -match '\\Documents\\' } | Microsoft.PowerShell.Utility\Select-Object -First 1 |
| 286 | + if (-not $modulesFolder) { $env:PSModulePath -split ';' | Microsoft.PowerShell.Utility\Select-Object -First 1 } |
| 287 | + } |
264 | 288 | if (-not $isOnWindows) { $modulesFolder = "/usr/local/share/powershell/Modules" } |
265 | 289 |
|
266 | 290 | Install-ZipModule -Config $data.Config.PSGetV3 -ModulesFolder $modulesFolder -TempFolder $tempFolder |
|
285 | 309 | $useInternal = $false |
286 | 310 | } |
287 | 311 | } |
| 312 | + |
| 313 | + $asCurrentUser = $UserMode.ToBool() |
| 314 | + if (-not $asCurrentUser -and |
| 315 | + ($env:COMPUTERNAME -eq $ComputerName) -and |
| 316 | + (-not (Test-PSFPowerShell -Elevated)) |
| 317 | + ) { |
| 318 | + $asCurrentUser = $true |
| 319 | + } |
288 | 320 | #endregion Resolve Source Configuration |
289 | 321 | } |
290 | 322 | process { |
|
298 | 330 | $binaries = Resolve-PowerShellGet -Type $typeEntry -Offline:$stayOffline -SourcePath $SourcePath -NotInternal:$useInternal |
299 | 331 |
|
300 | 332 | # Execute Deployment |
301 | | - Invoke-PSFCommand -ComputerName $ComputerName -ScriptBlock $code -Credential $Credential -ArgumentList $binaries |
| 333 | + Invoke-PSFCommand -ComputerName $ComputerName -ScriptBlock $code -Credential $Credential -ArgumentList $binaries, $asCurrentUser |
302 | 334 | } |
303 | 335 | } |
304 | 336 | end { |
|
0 commit comments