forked from sanderstad/Export-DMVInformation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-ExportDMVInformation.ps1
116 lines (101 loc) · 3.81 KB
/
Get-ExportDMVInformation.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
# Original file from https://github.com/psget/psget/
#
# Adjusted to import the Export-DMVInformation module
param (
[string[]]$url = ("https://raw.githubusercontent.com/sanderstad/Export-DMVInformation/master/Export-DMVInformation.psm1", "https://raw.githubusercontent.com/sanderstad/Export-DMVInformation/master/Export-DMVInformation.psd1")
)
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-Module {
param (
[string[]]
# URL to the respository to download PSSQLLib from
$url
)
$moduleName = "Export-DMVInformation"
$ModulePaths = @($env:PSModulePath -split ';')
# $PSSQLLibDestinationModulePath is mostly needed for testing purposes,
if ((Test-Path -Path Variable:ModulePath) -and $ModulePath) {
$Destination = $ModulePath
if ($ModulePaths -notcontains $Destination) {
Write-Warning "$moduleName 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 + "\$moduleName\") -ItemType Directory -Force | Out-Null
Write-Host ('Downloading PSSQLLib from {0}' -f $url[0])
Get-File -Url $url[0] -SaveToLocation "$Destination\$moduleName\Export-DMVInformation.psm1"
Write-Host ('Downloading PSSQLLib from {0}' -f $url[1])
Get-File -Url $url[1] -SaveToLocation "$Destination\$moduleName\Export-DMVInformation.psd1"
$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 PSSQLLib is imported from the location it was just installed to
Import-Module -Name $Destination\Export-DMVInformation
}
Write-Host "$moduleName is installed and ready to use" -Foreground Green
}
Install-Module -Url $url