forked from psget/psget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetPsGet.ps1
119 lines (103 loc) · 3.47 KB
/
GetPsGet.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
param (
$url = "https://github.com/psget/psget/raw/master/PsGet/PsGet.psm1"
)
function Find-Proxy() {
if ((Test-Path Env:HTTP_PROXY) -Or (Test-Path Env:HTTPS_PROXY)) {
return $true
}
Else {
return $false
}
}
function Get-Proxy() {
if (Test-Path Env:HTTP_PROXY) {
return $Env:HTTP_PROXY
}
ElseIf (Test-Path Env:HTTPS_PROXY) {
return $Env:HTTPS_PROXY
}
}
function Get-File {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[String] $Url,
[Parameter(Mandatory=$true)]
[String] $SaveToLocation
)
$command = (Get-Command Invoke-WebRequest -ErrorAction SilentlyContinue)
if($command -ne $null) {
if (Find-Proxy) {
$proxy = Get-Proxy
Write-Host "Proxy detected"
Write-Host "Using proxy address $proxy"
Invoke-WebRequest -Uri $Url -OutFile $SaveToLocation -Proxy $proxy
}
else {
Invoke-WebRequest -Uri $Url -OutFile $SaveToLocation
}
}
else {
$client = (New-Object Net.WebClient)
$client.UseDefaultCredentials = $true
if (Find-Proxy) {
$proxy = Get-Proxy
Write-Host "Proxy detected"
Write-Host "Using proxy address $proxy"
$webproxy = new-object System.Net.WebProxy
$webproxy.Address = $proxy
$client.proxy = $webproxy
}
$client.DownloadFile($Url, $SaveToLocation)
}
}
function Install-PsGet {
param (
[string]
# URL to the respository to download PSGet from
$url
)
$ModulePaths = @($env:PSModulePath -split ';')
# $PsGetDestinationModulePath is mostly needed for testing purposes,
if ((Test-Path -Path Variable:PsGetDestinationModulePath) -and $PsGetDestinationModulePath) {
$Destination = $PsGetDestinationModulePath
if ($ModulePaths -notcontains $Destination) {
Write-Warning 'PsGet install destination is not included in the PSModulePath environment variable'
}
}
else {
$ExpectedUserModulePath = Join-Path -Path ([Environment]::GetFolderPath('MyDocuments')) -ChildPath WindowsPowerShell\Modules
$Destination = $ModulePaths | Where-Object { $_ -eq $ExpectedUserModulePath }
if (-not $Destination) {
$Destination = $ModulePaths | Select-Object -Index 0
}
}
New-Item -Path ($Destination + "\PsGet\") -ItemType Directory -Force | Out-Null
Write-Host ('Downloading PsGet from {0}' -f $url)
Get-File -Url $url -SaveToLocation "$Destination\PsGet\PsGet.psm1"
$executionPolicy = (Get-ExecutionPolicy)
$executionRestricted = ($executionPolicy -eq "Restricted")
if ($executionRestricted) {
Write-Warning @"
Your execution policy is $executionPolicy, this means you will not be able import or use any scripts including modules.
To fix this change your execution policy to something like RemoteSigned.
PS> Set-ExecutionPolicy RemoteSigned
For more information execute:
PS> Get-Help about_execution_policies
"@
}
if (!$executionRestricted) {
# ensure PsGet is imported from the location it was just installed to
Import-Module -Name $Destination\PsGet
}
Write-Host "PsGet is installed and ready to use" -Foreground Green
Write-Host @"
USAGE:
PS> import-module PsGet
PS> install-module PsUrl
For more details:
get-help install-module
Or visit http://psget.net
"@
}
Install-PsGet -Url $url