forked from Mesverrum/MyPublicWork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PowershellTemplate.ps1
102 lines (77 loc) · 4.19 KB
/
PowershellTemplate.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
requires -version 2
<#
.SYNOPSIS
<Overview of script>
.DESCRIPTION
<Brief description of script>
.PARAMETER <Parameter_Name>
<Brief description of parameter input required. Repeat this attribute if required>
.INPUTS
<Inputs if any, otherwise state None>
.OUTPUTS
<Outputs if any, otherwise state None - example: Log file stored in C:\Windows\Temp\<name>.log>
.NOTES
Version: 1.0
Author: <Name>
Creation Date: <Date>
Purpose/Change: Initial script development
.EXAMPLE
<Example goes here. Repeat this attribute for more than one example>
#>
<#----------------------------------------------------------[VARIABLE DECLARATIONS]----------------------------------------------------------#>
<#----------------------------------------------------------------[FUNCTIONS]----------------------------------------------------------------#>
Function Set-SwisConnection {
Param(
[Parameter(Mandatory=$true, HelpMessage = "What SolarWinds server are you connecting to (Hostname or IP)?" ) ] [string] $solarWindsServer,
[Parameter(Mandatory=$true, HelpMessage = "Do you want to use the credentials from PowerShell (Trusted), or a new login (Explicit)?" ) ] [ ValidateSet( 'Trusted', 'Explicit' ) ] [ string ] $connectionType,
[Parameter(HelpMessage = "Which credentials should we use for an explicit logon type" ) ] $creds
)
IF ( $connectionType -eq 'Trusted' ) { $swis = Connect-Swis -Trusted -Hostname $solarWindsServer }
ELSEIF(!$creds) {
$creds = Get-Credential -Message "Please provide a Domain or Local Login for SolarWinds"
$swis = Connect-Swis -Credential $creds -Hostname $solarWindsServer
} ELSE { $swis = Connect-Swis -Credential $creds -Hostname $solarWindsServer }
RETURN $swis
}
<#----------------------------------------------------------[START LOGGING]----------------------------------------------------------#>
clear-host
Start-Transcript -Path $Logfile -Append -IncludeInvocationHeader | Out-Null
if (!(Get-PSSnapin | Where-Object { $_.Name -eq "SwisSnapin" })) {
Add-PSSnapin "SwisSnapin"
}
$now = Get-Date -Format "yyyyMMdd_HHmm"
$script = $MyInvocation.MyCommand
if($script.path){ $dir = Split-Path $script.path }
else { $dir = [Environment]::GetFolderPath("Desktop") }
$Logfile = "$dir\$($script.name)_$now.log"
<#----------------------------------------------------------[ORION CONNECTION]----------------------------------------------------------#>
while (!$swistest) {
if(!$hostname) {
$hostname = "localhost"
$connectionType = "Trusted"
$swis = Set-SwisConnection $hostname $connectionType
$swistest = get-swisdata $swis "SELECT TOP 1 servername FROM Orion.Websites"
if (!$swistest) {
"`nFailed to connect to SWIS using $hostname address and $connectiontype credentials"
$hostname = Read-Host -Prompt "what server should we connect to?"
$connectionType = Read-Host -Prompt "Should we use the current powershell credentials [Trusted], or specify credentials [Explicit]?"
$swis = Set-SwisConnection $hostname $connectionType
$swistest = get-swisdata $swis "SELECT TOP 1 servername FROM Orion.Websites"
}
} else {
$swistest = get-swisdata $swis "SELECT TOP 1 servername FROM Orion.Websites"
if (!$swistest) {
"`nFailed to connect to SWIS using $hostname address and $connectiontype credentials"
$hostname = Read-Host -Prompt "what server should we connect to?"
$connectionType = Read-Host -Prompt "Should we use the current powershell credentials [Trusted], or specify credentials [Explicit]?"
$swis = Set-SwisConnection $hostname $connectionType
$swistest = get-swisdata $swis "SELECT TOP 1 servername FROM Orion.Websites"
}
}
}
$swistest = $null
"Connected to $hostname Successfully using $connectiontype credentials"
<#----------------------------------------------------------[BODY]----------------------------------------------------------#>
<#----------------------------------------------------------[POST SCRIPT]----------------------------------------------------------#>
"`n`nFinished"
Stop-Transcript