-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_maps.ps1
124 lines (86 loc) · 4.65 KB
/
export_maps.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
120
121
122
123
124
#title :export_maps.ps1
#description :Cisco Prime Map Export - Version 3.7
#author :pieewiee
#licence :WTFPL
#date :2020-08-14
#version :1.0
#usage :powershell -f export_maps.ps1 -campus
#notes :
#powershell_version :5.1.15063.1478
#==============================================================================
using module '.\modules\logging.psm1'
using module '.\modules\WebRequest.psm1'
using module '.\modules\FileSystem.psm1'
using module '.\modules\ImageHandler.psm1'
param(
[string]$Campus
)
$global:scriptpath = $MyInvocation.MyCommand.Path
$global:Dir = Split-Path $global:scriptpath
$global:DirConfig = "$Dir\config"
$global:ImageDir = "$Dir\maps\"
$global:config = Get-Content "$DirConfig\config.json" | ConvertFrom-Json
if (!$config) {
Write-Error "No Valid Config File"
exit
}
$logging = [logging]::new($config.logging, $global:Dir) #init class logparameters are seperated by tabstop
$logging.create() #creates logfile if not created
Function ErrorStop($message)
{
$logging.writelog("Error", $message)
}
$webrequest = [webrequest]::new($config.Cprime.headerLogin, "$($config.Domain)$($config.cprime.logout)", "Post")
$webrequest.sendBody("$($config.Domain)$($config.cprime.login)", "j_username=$($config.username)&j_password=$($config.password)" + $config.cprime.LoginPayload) > $null
$webrequest.HasBody($false)
$sites = $webrequest.send("$($config.Domain)$($config.cprime.sites)") | ConvertFrom-Json
$sites = $sites.items | Where-Object { $_.name -eq $global:Campus }
if ($null -eq $sites.id)
{
$webrequest.send("$($config.Domain)$($config.cprime.logout)") > $null
$logging.writelog("Error", "No valid Campus found")
write-host "No valid Campus found"
exit
}
[FileSystem]::CreateFolder("$ImageDir\$global:Campus")
# --- Loop through all buildings ---
$buildings = $webrequest.send("$($config.Domain)$($config.cprime.Domains.replace("{id}", $sites.id))") | ConvertFrom-Json
foreach ($building in $buildings.items) {
$image_path = "$($ImageDir)$($global:Campus)\$([FileSystem]::IllegalCharacter($building.name))"
[FileSystem]::CreateFolder($image_path)
# --- Loop through all building levels ---
$levels = $webrequest.send("$($config.Domain)$($config.cprime.Domains.replace("{id}", $building.id))") | ConvertFrom-Json
foreach ($level in $levels.items) {
$image_name = [FileSystem]::IllegalCharacter($level.name) + ".png"
$image_new = [FileSystem]::IllegalCharacter($level.name) + "_new.png"
if (!$([FileSystem]::FileExists("$image_path/$image_name"))) {
$webrequest.Download("$($config.Domain)/webacs$($level.image)", "$($image_path)\$($image_name)")
}
$NewMap = [System.Drawing.Image]::FromFile("$image_path/$image_name")
if ($NewMap.PixelFormat -ne "Format24bppRgb")
{
$image_name_temp = $image_name.Replace(".png","temp.png");
$NewMap.Save("$image_path/$image_name_temp", [System.Drawing.Imaging.ImageFormat]::jpeg);
$NewMap.Dispose();
[FileSystem]::Remove($("$image_path/$image_name"))
[FileSystem]::Rename($("$image_path/$image_name_temp"), $("$image_path/$image_name"))
$NewMap = [System.Drawing.Image]::FromFile("$image_path/$image_name")
}
# Setup Image and Calculate Accesspoints Positions
$graphics = [System.Drawing.Graphics]::FromImage($NewMap)
$ImageHandler = [ImageHandler]::new($NewMap.Width, $NewMap.Height, $level.geometry.width, $level.geometry.length)
# Setup Font and Brush
$font = new-object System.Drawing.Font("Consolas", $ImageHandler.FontZize(), "Bold", "Pixel")
$brushFg = [System.Drawing.Brushes]::red
$AccesPoints = $webrequest.send("$($config.Domain)$($config.cprime.AccessPoints.replace("{id}", $level.id))") | ConvertFrom-Json
# --- Loop through Access Points ---
foreach ($AccesPoint in $AccesPoints.items) {
$graphics.DrawString($AccesPoint.attributes.name, $font, $brushFg, $ImageHandler.APPos($AccesPoint.position)[0], $ImageHandler.APPos($AccesPoint.position)[1])
$graphics.FillRectangle($brushFg, $ImageHandler.APPos($AccesPoint.position)[2], $ImageHandler.APPos($AccesPoint.position)[3], $ImageHandler.PointSize(), $ImageHandler.PointSize())
}
$NewMap.Save("$image_path/$image_new")
$NewMap.Dispose()
$graphics.Dispose()
}
}
$webrequest.send("$($config.Domain)$($config.cprime.logout)") > $null