-
Notifications
You must be signed in to change notification settings - Fork 20
/
ResourceImporter.ps1
92 lines (71 loc) · 3.75 KB
/
ResourceImporter.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
<#------------- 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
}
<#------------- ACTUAL SCRIPT -------------#>
clear-host
$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"
Start-Transcript -Path $Logfile -Append -IncludeInvocationHeader
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
while(!$swistest) {
$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"
}
"Connected to $hostname Successfully using $connectiontype credentials"
$quit = $null
while ($quit -ne "Quit" ) {
"`nPlease provide the resource to import"
$quit = Read-Host 'Press Enter to select file to import, or type [Quit] to exit'
switch -regex ($quit) {
"quit" { "`n`nQuitting"; $quit="Quit" ; break}
default {
Add-Type -AssemblyName System.Windows.Forms
$inputfolder = New-Object System.Windows.Forms.OpenFileDialog -Property @{
InitialDirectory = [Environment]::GetFolderPath('Desktop')
Filter = 'XML (*.xml)|*.xml'
}
$null = $inputfolder.ShowDialog()
$inputfolder = $inputfolder.FileName
"$inputfolder selected..."
$resourceproperties = Import-Clixml ("$inputfolder")
$viewid = Read-Host -Prompt "Which ViewID # should we add this resource to?"
$resourceResults = Invoke-SwisVerb $swis Orion.Views AddResourceToView @($viewid, $ResourceProperties)
"`nThese queries need to be run in SQL to finish cleaning Up"
#$cleanup = Invoke-SwisVerb $swis 'Orion.Reporting' 'ExecuteSQL'
@"
update resourceproperties
set propertyvalue = replace(replace(propertyvalue, 'linebreak', char(10)),'ampersand',char(38))
where propertyvalue like '%linebreak%' or propertyvalue like '%ampersand%'
;
update resources
set resourcename = replace(replace(ResourceName,'ampersand',char(38)),'doublequotes',char(34)),
resourcetitle = replace(replace(ResourceTitle,'ampersand',char(38)),'doublequotes',char(34)),
resourcesubtitle = replace(replace(resourcesubtitle,'ampersand',char(38)),'doublequotes',char(34))
where resourcename like '%ampersand%' or resourcetitle like '%ampersand%' or resourcesubtitle like
'%ampersand%' or resourcename like '%doublequotes%' or resourcetitle like '%doublequotes%' or
resourcesubtitle like '%doublequotes%'
"@
}
}
}
"Finished"
Stop-Transcript