SHOW:
|
|
- or go back to the newest paste.
1 | #======================================================================== | |
2 | # Created with: SAPIEN Technologies, Inc., PowerShell Studio 2012 v3.1.13 | |
3 | # Created on: 4/22/2013 1:10 PM | |
4 | - | # Created by: |
4 | + | # Created by: Paul F |
5 | - | # Organization: Maritz, LLC |
5 | + | |
6 | #======================================================================== | |
7 | ||
8 | $start = Get-Date | |
9 | Import-Module ActiveDirectory | |
10 | ||
11 | function SQL-Connect($server, $port, $db, $userName, $passWord, $query) { | |
12 | $conn = New-Object System.Data.SqlClient.SqlConnection | |
13 | $ctimeout = 30 | |
14 | $qtimeout = 120 | |
15 | $constring = "Server={0},{5};Database={1};Integrated Security=False;User ID={2};Password={3};Connect Timeout={4}" -f $server,$db,$userName,$passWord,$ctimeout,$port | |
16 | $conn.ConnectionString = $constring | |
17 | $conn.Open() | |
18 | $cmd = New-Object System.Data.SqlClient.SqlCommand($query, $conn) | |
19 | $cmd.CommandTimeout = $qtimeout | |
20 | $ds = New-Object System.Data.DataSet | |
21 | $da = New-Object System.Data.SqlClient.SqlDataAdapter($cmd) | |
22 | $da.fill($ds) | |
23 | $conn.Close() | |
24 | return $ds | |
25 | } | |
26 | ||
27 | - | function Graph-Iterate($arList,$varRow,$varCol) { |
27 | + | function Graph-Iterate($arList,$varRow,$varCol,$strPass) { |
28 | Write-Host $strPass | |
29 | foreach($i in $arList.Keys) { | |
30 | if($arList[$i].duration -ne 0) { | |
31 | if($arList[$i].depName.Length -gt 1) { | |
32 | $varRow-- | |
33 | - | $arList[$i].depName |
33 | + | if($arList[$i].depName -eq $null){ $arList[$i].depName = "UNKNOWN" } |
34 | - | $arList[$i].duration |
34 | + | |
35 | $varRow++ | |
36 | $sheet.Cells.Item($varRow,$varCol) = $arList[$i].duration | |
37 | $varCol++ | |
38 | ||
39 | # Iterate Here | |
40 | - | Iterate($arList[$i]) |
40 | + | if($master -ne $true){ Iterate $arList[$i] $strPass } |
41 | } | |
42 | } | |
43 | } | |
44 | - | # Because PowerShell is dumb as shit and thinks "return" means "exit, and puke out all the output". |
44 | + | |
45 | - | # It's ok, I remember my first time using a programming language too... |
45 | + | |
46 | - | Write-Host $varCol |
46 | + | |
47 | function Iterate($arSub, $strCom) { | |
48 | $indSheet = $workbook.Worksheets.Add() | |
49 | $sheetName = ("{0}-{1}" -f $strCom,$arSub.depName) | |
50 | - | function Iterate($arSub) { |
50 | + | Write-Host $sheetName |
51 | - | # Create sheet, add cells. Destroy sheet object. |
51 | + | $nVar = 1 |
52 | - | $indSheet = $workbook.Worksheets.Add() |
52 | + | if($sheetName -eq "CSI-OPP MAX") |
53 | - | $indSheet.Name = $arSub.depName |
53 | + | { |
54 | - | $indRow = 1 |
54 | + | Write-Host "The Var is:" |
55 | - | $indCol = 1 |
55 | + | Write-Host $nVar |
56 | - | # Iterate through tertiary node (Users), $y returns username. |
56 | + | $sheetName = "{0} {1}" -f $sheetName,$nVar |
57 | - | foreach($y in $arSub.Keys) { |
57 | + | $nVar++ |
58 | - | if($y -ne "depNames" -and $y -ne "duration" -and $y.Length -gt 1) { |
58 | + | |
59 | - | $indSheet.Cells.Item($indRow,$indCol) = $y |
59 | + | $strip = [System.Text.RegularExpressions.Regex]::Replace($sheetName,"[^1-9a-zA-Z_-]"," "); |
60 | - | $indCol++ |
60 | + | if($strip.Length -gt 31) { $ln = 31 }else{ $ln = $strip.Length } |
61 | - | $indSheet.Cells.Item($indRow,$indCol) = $arSub[$y] |
61 | + | $indSheet.Name = $strip.Substring(0, $ln) |
62 | - | $indCol-- |
62 | + | $count = $arSub.Keys.Count |
63 | - | $indRow++ |
63 | + | $array = New-Object 'object[,]' $count,2 |
64 | $arRow = 0 | |
65 | foreach($y in $arSub.Keys) { | |
66 | if($y -ne "depName" -and $y -ne "duration" -and $y.Length -gt 1) { | |
67 | - | $indSheet = $null |
67 | + | $t = 0 |
68 | $array[$arRow,$t] = $y | |
69 | $t++ | |
70 | - | function Create-Graph($lSheet,$lTop,$lLeft,$range, $number) { |
70 | + | $array[$arRow,$t] = $arSub[$y] |
71 | - | # Add graph to Dashboard and configure. |
71 | + | $arRow++ |
72 | - | $chart = $lSheet.Shapes.AddChart().Chart |
72 | + | |
73 | - | $chartNum = ("Chart {0}" -f $cvar3) |
73 | + | |
74 | - | $sheet.Shapes.Item($chartNum).Placement = 3 |
74 | + | $rng = $indSheet.Range("A1",("B"+$count)) |
75 | - | $sheet.Shapes.Item($chartNum).Top = $top |
75 | + | $rng.Value2 = $array |
76 | - | $sheet.Shapes.Item($chartNum).Left = $left |
76 | + | |
77 | - | $sheet.Shapes.Item($chartNum).Height = 325 |
77 | + | |
78 | - | $sheet.Shapes.Item($chartNum).Width = 400 |
78 | + | function Create-Graph($lSheet,$lTop,$lLeft,$range, $number, $master) { |
79 | # Add graph to Dashboard and configure. | |
80 | $chart = $lSheet.Shapes.AddChart().Chart | |
81 | $chartNum = ("Chart {0}" -f $cvar3) | |
82 | $sheet.Shapes.Item($chartNum).Placement = 3 | |
83 | $sheet.Shapes.Item($chartNum).Top = $top | |
84 | - | $port = "40013" |
84 | + | $sheet.Shapes.Item($chartNum).Left = $left |
85 | - | $server = "10.65.1.45\fensqlmgtsp216m" |
85 | + | if($master -eq $true) { |
86 | - | $db = "CitrixRMSummary" |
86 | + | $sheet.Shapes.Item($chartNum).Height = 500 |
87 | - | $user = "CitrixRMReadOnly" |
87 | + | $sheet.Shapes.Item($chartNum).Width = 1220 |
88 | - | $password = "" |
88 | + | |
89 | - | $query = "SELECT TOP 2 * FROM CitrixRMIM.LU_USER" |
89 | + | $sheet.Shapes.Item($chartNum).Height = 325 |
90 | $sheet.Shapes.Item($chartNum).Width = 400 | |
91 | } | |
92 | $chart.ChartType = 69 | |
93 | $chart.SetSourceData($range) | |
94 | } | |
95 | ||
96 | ||
97 | $port = "<port>" | |
98 | - | $query2 = "SELECT * FROM CitrixRMIM.SDB_SESSION WHERE (CitrixRMIM.SDB_SESSION.FK_USERID = {0}) AND (CitrixRMIM.SDB_SESSION.SESSIONSTART > '01/03/2013')" -f $i.PK_USERID |
98 | + | $server = "<server>" |
99 | $db = "<db>" | |
100 | $user = "<db_user>" | |
101 | $password = "<pass>" | |
102 | $query = "SELECT * FROM CitrixRMIM.LU_USER" | |
103 | # For pulling elevated accounts. | |
104 | #$query = "SELECT * FROM CitrixRMIM.LU_USER WHERE USERNAME LIKE '%[_]%'" | |
105 | $userlist = SQL-Connect $server $port $db $user $password $query | |
106 | ||
107 | # Create Array (empty) for department data | |
108 | $arDept = @{} | |
109 | $arTotal = @{} | |
110 | foreach($i in $userlist.Tables[0].Rows) { | |
111 | $pass = $false | |
112 | # Pull sessions for users. | |
113 | $uName = $i.USERNAME | |
114 | - | #if($userInfo.Department -eq "ARG-DATA Capture"){ $userInfo.SamAccountName } |
114 | + | $query2 = "SELECT * FROM CitrixRMIM.SDB_SESSION WHERE (CitrixRMIM.SDB_SESSION.FK_USERID = {0}) AND (CitrixRMIM.SDB_SESSION.SESSIONSTART > '01/01/2013') AND (CitrixRMIM.SDB_SESSION.SESSIONSTART < '03/01/2013')" -f $i.PK_USERID |
115 | $result = SQL-Connect $server $port $db $user $password $query2 | |
116 | $duration = 0 | |
117 | foreach($i in $result.Tables[0].Rows){ | |
118 | [int64]$dur = $i.DURATION.ToString() | |
119 | $duration = ($duration + ($dur / 60000)) | |
120 | } | |
121 | ||
122 | - | if($arDept.Contains("Elevated") -eq $false) { $arDept["Elevated"] = @{} } |
122 | + | |
123 | - | $arDept["Elevated"][$userInfo.SamAccountName] = "{0:N0}" -f ($duration/60) |
123 | + | |
124 | - | # Write-Host "FAILURE!" |
124 | + | |
125 | - | # Write-Host $userInfo.DepartmentNumber[0] |
125 | + | |
126 | - | # Write-Host $userInfo.SamAccountName |
126 | + | |
127 | if($pass -ne $true) { | |
128 | # Check the user appears normal (a_ and h_ accounts likely wont have department numbers). | |
129 | if($userInfo.DepartmentNumber[0].Length -gt 1 -and $userInfo.SamAccountName.Length -gt 1 -and $userInfo.Company.Length -gt 1) { | |
130 | # Check whether or not the department element exists, if not, create it. | |
131 | if($arDept.Contains($userInfo.Company) -eq $false) { $arDept[$userInfo.Company] = @{};$arDept[$userInfo.Company]["comName"] = $userInfo.Company} | |
132 | if($arTotal.Contains($userInfo.Company) -eq $false) { $arTotal[$userInfo.Company] = @{};$arTotal[$userInfo.Company]["depName"] = $userInfo.Company; $arTotal[$userInfo.Company]["duration"] = $arTotal[$userInfo.Company]["duration"] + ($duration/60) } | |
133 | if($arDept[$userInfo.Company].Contains($userInfo.DepartmentNumber[0]) -eq $false) { $arDept[$userInfo.Company][$userInfo.DepartmentNumber[0]] = @{};$arDept[$userInfo.Company][$userInfo.DepartmentNumber[0]]["depName"] = $userInfo.Department } | |
134 | # Add user element to array with their total session duration. | |
135 | $arDept[$userInfo.Company][$userInfo.DepartmentNumber[0]][$userInfo.SamAccountName] = "{0:N0}" -f ($duration/60) | |
136 | # Update department duration total (pull user total from .Count()). | |
137 | $arDept[$userInfo.Company][$userInfo.DepartmentNumber[0]]["duration"] = $arDept[$userInfo.Company][$userInfo.DepartmentNumber[0]]["duration"] + ($duration/60) | |
138 | }else{ | |
139 | if($arTotal.Contains("Administrative") -eq $false) { $arTotal["Administrative"] = @{};$arTotal["Administrative"]["depName"] = "Administrative"; $arTotal["Administrative"]["duration"] = $arTotal["Administrative"]["duration"] + ($duration/60) } | |
140 | if($userInfo.SamAccountName -like "a_*") { | |
141 | if($arDept.Contains("Administrative") -eq $false) { $arDept["Administrative"] = @{};$arDept["Administrative"]["comName"] = "Administrative"} | |
142 | if($arDept["Administrative"].Contains("1337") -eq $false) { $arDept["Administrative"]["1337"] = @{};$arDept["Administrative"]["1337"]["depName"] = "Elevated IDs (A)" } | |
143 | $arDept["Administrative"]["1337"]["duration"] = $arDept["Administrative"]["1337"]["duration"] + ($duration/60) | |
144 | - | $row = 162 |
144 | + | $arDept["Administrative"]["1337"][$userInfo.SamAccountName] = "{0:N0}" -f ($duration/60) |
145 | }else{ | |
146 | if($arDept.Contains("Administrative") -eq $false) { $arDept["Administrative"] = @{};$arDept["Administrative"]["comName"] = "Administrative"} | |
147 | if($arDept["Administrative"].Contains("1337er") -eq $false) { $arDept["Administrative"]["1337er"] = @{};$arDept["Administrative"]["1337er"]["depName"] = "Elevated IDs (H)" } | |
148 | $arDept["Administrative"]["1337er"]["duration"] = $arDept["Administrative"]["1337er"]["duration"] + ($duration/60) | |
149 | $arDept["Administrative"]["1337er"][$userInfo.SamAccountName] = "{0:N0}" -f ($duration/60) | |
150 | } | |
151 | - | # Setup chart location vars. |
151 | + | |
152 | - | if($cvar -eq 1) { |
152 | + | |
153 | - | $left = 10 |
153 | + | |
154 | - | }elseif($cvar -eq 2){ |
154 | + | |
155 | - | $left = 420 |
155 | + | |
156 | - | }elseif($cvar -eq 3) { |
156 | + | |
157 | - | $left = 830 |
157 | + | |
158 | $excel.DisplayAlerts = $false | |
159 | - | $col = 2 |
159 | + | |
160 | - | $sheet.Cells.Item($row,1) = $arDept[$z].comName |
160 | + | |
161 | - | # Track chart range minimum cell address. |
161 | + | |
162 | - | $min = ($sheet.Cells.Item(($row)-1,1).Address()).Replace("$", "") |
162 | + | |
163 | - | # Iterate through secondary element (Departments), $i returns department name. |
163 | + | |
164 | ||
165 | - | # Graph-Iterate Here |
165 | + | |
166 | - | $vLoc = Graph-Iterate $arDept[$z] $row $col |
166 | + | |
167 | # $cvar tracks $left position, resets when it reaches 3. | |
168 | - | #Write-Host "ugh: "+Get-TypeData $vLoc |
168 | + | |
169 | - | Get-Member -InputObject $vLoc |
169 | + | $row = 202 |
170 | - | $vLoc.GetType() |
170 | + | $col = 2 |
171 | $cvar = 1 | |
172 | - | # Track chart range maximum cell address. |
172 | + | |
173 | - | $max = ($sheet.Cells.Item($row,$col).Address()).Replace("$", "") |
173 | + | |
174 | - | $range = $sheet.Range($min,$max) |
174 | + | |
175 | # Iterate through main element (Companies), $z returns company name (MGTS, MR, etc.). | |
176 | - | Create-Graph $sheet $top $left $range $cvar3 |
176 | + | |
177 | - | $row++;$row++ |
177 | + | $min = ($sheet.Cells.Item(($row)-1,1).Address()).Replace("$", "") |
178 | - | # Increment or reset tracking vars. |
178 | + | foreach($q in $arTotal.Keys) { |
179 | - | if($cvar -eq 3) { |
179 | + | $sheet.Cells.Item($row,1) = "Maritz Total Citrix Usage (by hours)" |
180 | - | $top = ($top)+340 |
180 | + | $row-- |
181 | $sheet.Cells.Item($row,$col) = $arTotal[$q].depName | |
182 | - | if($cvar -eq 1 -or $cvar -eq 2){ $cvar++ }elseif($cvar -eq 3){ $cvar = 1} |
182 | + | $row++ |
183 | - | $cvar3++ |
183 | + | $sheet.Cells.Item($row,$col) = $arTotal[$q].duration |
184 | $col++ | |
185 | } | |
186 | - | $sheet.Activate() |
186 | + | $max = ($sheet.Cells.Item($row,($col)-1).Address()).Replace("$", "") |
187 | $range = $sheet.Range($min,$max) | |
188 | Create-Graph $sheet $top $left $range $cvar3 $true | |
189 | $row++;$row++ | |
190 | $col = 2 | |
191 | $top = ($top)+510 | |
192 | $cvar3++ | |
193 | ||
194 | foreach($z in $arDept.Keys) { | |
195 | if($z.Length -gt 1 -and $z -ne "112 MAS"){ | |
196 | # Setup chart location vars. | |
197 | if($cvar -eq 1) { | |
198 | $left = 10 | |
199 | }elseif($cvar -eq 2){ | |
200 | $left = 420 | |
201 | }elseif($cvar -eq 3) { | |
202 | $left = 830 | |
203 | } | |
204 | $col = 2 | |
205 | $sheet.Cells.Item($row,1) = $arDept[$z].comName | |
206 | # Track chart range minimum cell address. | |
207 | $min = ($sheet.Cells.Item(($row)-1,1).Address()).Replace("$", "") | |
208 | # Iterate through secondary element (Departments), $i returns department name. | |
209 | ||
210 | # Graph-Iterate Here | |
211 | Write-Host $arDept[$z].comName | |
212 | $vLoc = Graph-Iterate $arDept[$z] $row $col $arDept[$z].comName | |
213 | ||
214 | # Track chart range maximum cell address. | |
215 | $max = ($sheet.Cells.Item($row,($vLoc)-1).Address()).Replace("$", "") | |
216 | $range = $sheet.Range($min,$max) | |
217 | ||
218 | Create-Graph $sheet $top $left $range $cvar3 | |
219 | $row++;$row++ | |
220 | # Increment or reset tracking vars. | |
221 | if($cvar -eq 3) { | |
222 | $top = ($top)+340 | |
223 | } | |
224 | if($cvar -eq 1 -or $cvar -eq 2){ $cvar++ }elseif($cvar -eq 3){ $cvar = 1} | |
225 | $cvar3++ | |
226 | } | |
227 | } | |
228 | # Show dashboard page rather than some random department. | |
229 | $sheet.Activate() | |
230 | $stop = Get-Date | |
231 | New-TimeSpan $start $stop |