|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Backs up all LabTech scripts. |
| 4 | +
|
| 5 | +.DESCRIPTION |
| 6 | + This script will export all LabTech sctipts in xml format to a specified destination. |
| 7 | + Requires the MySQL .NET connector. |
| 8 | +
|
| 9 | +.LINK |
| 10 | + http://www.labtechconsulting.com |
| 11 | + https://dev.mysql.com/downloads/connector/net/6.9.html |
| 12 | + |
| 13 | +.OUTPUTS |
| 14 | + Default values - |
| 15 | + Log file stored in: $($env:windir)\LTScv\Logs\LT-ScriptExport.log |
| 16 | + Scripts exported to: $($env:windir)\Program Files(x86)\LabTech\Backup\Scripts |
| 17 | + Credentials file: $PSScriptRoot |
| 18 | +
|
| 19 | +.NOTES |
| 20 | + Version: 1.0 |
| 21 | + Author: Chris Taylor |
| 22 | + Website: www.labtechconsulting.com |
| 23 | + Creation Date: 9/11/2015 |
| 24 | + Purpose/Change: Initial script development |
| 25 | +#> |
| 26 | + |
| 27 | +#Requires -Version 3.0 |
| 28 | + |
| 29 | +#region-[Declarations]---------------------------------------------------------- |
| 30 | + |
| 31 | + #Set Error Action to Silently Continue |
| 32 | + $ErrorActionPreference = "SilentlyContinue" |
| 33 | + |
| 34 | + #Get/Save config info |
| 35 | + if($(Test-Path $PSScriptRoot\LT-ScriptExport-Config.xml) -eq $false) { |
| 36 | + #Config file template |
| 37 | + $Config = [xml]@' |
| 38 | +<Settings> |
| 39 | + <LogPath></LogPath> |
| 40 | + <BackupRoot></BackupRoot> |
| 41 | + <MySQLDatabase></MySQLDatabase> |
| 42 | + <MySQLHost></MySQLHost> |
| 43 | + <CredPath></CredPath> |
| 44 | +</Settings> |
| 45 | +'@ |
| 46 | + |
| 47 | + #Create config file |
| 48 | + $Config.Settings.LogPath = "$(Read-Host "Path of log file ($($env:windir)\LTScv\Logs)")" |
| 49 | + if ($Config.Settings.LogPath -eq '') {$Config.Settings.LogPath = "$($env:windir)\LTScv\Logs"} |
| 50 | + $Config.Settings.BackupRoot = "$(Read-Host "Path of exported scripts ($($env:windir)\Program Files(x86)\LabTech\Backup\Scripts)")" |
| 51 | + if ($Config.Settings.BackupRoot -eq '') {$Config.Settings.BackupRoot = "$($env:windir)\Program Files(x86)\LabTech\Backup\Scripts"} |
| 52 | + $Config.Settings.MySQLDatabase = "$(Read-Host "Name of LabTech database (labtech)")" |
| 53 | + if ($Config.Settings.MySQLDatabase -eq '') {$Config.Settings.MySQLDatabase = "labtech"} |
| 54 | + $Config.Settings.MySQLHost = "$(Read-Host "FQDN of LabTechServer (localhost)")" |
| 55 | + if ($Config.Settings.MySQLHost -eq '') {$Config.Settings.MySQLHost = "localhost"} |
| 56 | + $Config.Settings.CredPath = "$(Read-Host "Path of credentials ($PSScriptRoot)")" |
| 57 | + if ($Config.Settings.CredPath -eq '') {$Config.Settings.CredPath = "$PSScriptRoot"} |
| 58 | + $Config.Save("$PSScriptRoot\LT-ScriptExport-Config.xml") |
| 59 | + } |
| 60 | + Else { |
| 61 | + [xml]$Config = Get-Content "$PSScriptRoot\LT-ScriptExport-Config.xml" |
| 62 | + } |
| 63 | + |
| 64 | + #Get/Save user/password info |
| 65 | + if ($(Test-Path $CredPath) -eq $false) {New-Item -ItemType Directory -Force -Path $CredPath | Out-Null} |
| 66 | + if($(Test-Path $CredPath\LTDBCredentials.xml) -eq $false){Get-Credential -Message "Please provide the credentials to the LabTech MySQL database." | Export-Clixml $CredPath\LTDBCredentials.xml -Force} |
| 67 | + |
| 68 | + #Transcript File Info |
| 69 | + $sTranscriptName = "LT-ScriptExport.log" |
| 70 | + $sTranscriptFile = ($Config.Settings.LogPath) + "\" + $sTranscriptName |
| 71 | + |
| 72 | + #Location to the backp repository |
| 73 | + $BackupRoot = $Config.Settings.BackupRoot |
| 74 | + |
| 75 | + #MySQL connection info |
| 76 | + $MySQLDatabase = $Config.Settings.MySQLDatabase |
| 77 | + $MySQLHost = $Config.Settings.MySQLHost |
| 78 | + $MySQLAdminPassword = (IMPORT-CLIXML $CredPath\LTDBCredentials.xml).GetNetworkCredential().Password |
| 79 | + $MySQLAdminUserName = (IMPORT-CLIXML $CredPath\LTDBCredentials.xml).GetNetworkCredential().UserName |
| 80 | + |
| 81 | + #Location to credentials file |
| 82 | + $CredPath = $Config.Settings.CredPath |
| 83 | + |
| 84 | +#endregion |
| 85 | + |
| 86 | +#region-[Functions]------------------------------------------------------------ |
| 87 | + |
| 88 | +Function Get-LTData { |
| 89 | + <# |
| 90 | + .SYNOPSIS |
| 91 | + Executes a MySQL query aginst the LabTech Databse. |
| 92 | +
|
| 93 | + .DESCRIPTION |
| 94 | + This comandlet will execute a MySQL query aginst the LabTech database. |
| 95 | + Requires the MySQL .NET connector. |
| 96 | + Original script by Dan Rose |
| 97 | +
|
| 98 | + .LINK |
| 99 | + https://dev.mysql.com/downloads/connector/net/6.9.html |
| 100 | + https://www.cogmotive.com/blog/powershell/querying-mysql-from-powershell |
| 101 | + http://www.labtechconsulting.com |
| 102 | +
|
| 103 | + .PARAMETER Query |
| 104 | + Input your MySQL query in double quotes. |
| 105 | +
|
| 106 | + .INPUTS |
| 107 | + Pipeline |
| 108 | +
|
| 109 | + .NOTES |
| 110 | + Version: 1.0 |
| 111 | + Author: Chris Taylor |
| 112 | + Website: www.labtechconsulting.com |
| 113 | + Creation Date: 9/11/2015 |
| 114 | + Purpose/Change: Initial script development |
| 115 | + |
| 116 | + .EXAMPLE |
| 117 | + Get-LTData "SELECT ScriptID FROM lt_scripts" |
| 118 | + $Query | Get-LTData |
| 119 | + #> |
| 120 | + |
| 121 | + Param( |
| 122 | + [Parameter( |
| 123 | + Mandatory = $true, |
| 124 | + ValueFromPipeline = $true)] |
| 125 | + [string]$Query |
| 126 | + ) |
| 127 | + |
| 128 | + Begin { |
| 129 | + $ConnectionString = "server=" + $MySQLHost + ";port=3306;uid=" + $MySQLAdminUserName + ";pwd=" + $MySQLAdminPassword + ";database="+$MySQLDatabase |
| 130 | + } |
| 131 | + |
| 132 | + Process { |
| 133 | + Try { |
| 134 | + [void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data") |
| 135 | + $Connection = New-Object MySql.Data.MySqlClient.MySqlConnection |
| 136 | + $Connection.ConnectionString = $ConnectionString |
| 137 | + $Connection.Open() |
| 138 | + |
| 139 | + $Command = New-Object MySql.Data.MySqlClient.MySqlCommand($Query, $Connection) |
| 140 | + $DataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($Command) |
| 141 | + $DataSet = New-Object System.Data.DataSet |
| 142 | + $RecordCount = $dataAdapter.Fill($dataSet, "data") |
| 143 | + $DataSet.Tables[0] |
| 144 | + } |
| 145 | + |
| 146 | + Catch { |
| 147 | + Write-Error "Unable to run query : $query `n$Error[0]" |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + End { |
| 152 | + $Connection.Close() |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +Function Export-LTScript { |
| 157 | + <# |
| 158 | + .SYNOPSIS |
| 159 | + Exports a LabTech script as an xml file. |
| 160 | +
|
| 161 | + .DESCRIPTION |
| 162 | + This commandlet will execute a MySQL query aginst the LabTech database. |
| 163 | + Requires Get-LTData |
| 164 | + |
| 165 | + .LINK |
| 166 | + http://www.labtechconsulting.com |
| 167 | +
|
| 168 | + .PARAMETER Query |
| 169 | + Input your MySQL query in double quotes. |
| 170 | + |
| 171 | + .PARAMETER FilePath |
| 172 | + File path of exported script. |
| 173 | +
|
| 174 | + .NOTES |
| 175 | + Version: 1.0 |
| 176 | + Author: Chris Taylor |
| 177 | + Website: www.labtechconsulting.com |
| 178 | + Creation Date: 9/11/2015 |
| 179 | + Purpose/Change: Initial script development |
| 180 | + |
| 181 | + .EXAMPLE |
| 182 | + Get-LTData "SELECT ScriptID FROM lt_scripts" -FilePath C:\Windows\Temp |
| 183 | + #> |
| 184 | + |
| 185 | + [CmdletBinding()] |
| 186 | + Param( |
| 187 | + [Parameter(Mandatory=$True,Position=1)] |
| 188 | + [string]$ScriptID, |
| 189 | + [Parameter(Mandatory=$True,Position=2)] |
| 190 | + [string]$FilePath |
| 191 | + ) |
| 192 | + |
| 193 | + #LabTech XML template |
| 194 | + $ExportTemplate = [xml] @" |
| 195 | +<LabTech_Expansion |
| 196 | + Version="100.332" |
| 197 | + Name="LabTech Script Expansion" |
| 198 | + Type="PackedScript"> |
| 199 | + <PackedScript> |
| 200 | + <NewDataSet> |
| 201 | + <Table> |
| 202 | + <ScriptId></ScriptId> |
| 203 | + <FolderId></FolderId> |
| 204 | + <ScriptName></ScriptName> |
| 205 | + <ScriptNotes></ScriptNotes> |
| 206 | + <Permission></Permission> |
| 207 | + <EditPermission></EditPermission> |
| 208 | + <ComputerScript></ComputerScript> |
| 209 | + <LocationScript></LocationScript> |
| 210 | + <MaintenanceScript></MaintenanceScript> |
| 211 | + <FunctionScript></FunctionScript> |
| 212 | + <LicenseData></LicenseData> |
| 213 | + <ScriptData></ScriptData> |
| 214 | + <ScriptVersion></ScriptVersion> |
| 215 | + <ScriptGuid></ScriptGuid> |
| 216 | + <ScriptFlags></ScriptFlags> |
| 217 | + <Parameters></Parameters> |
| 218 | + </Table> |
| 219 | + </NewDataSet> |
| 220 | + <ScriptFolder> |
| 221 | + <NewDataSet> |
| 222 | + <Table> |
| 223 | + <FolderID></FolderID> |
| 224 | + <ParentID></ParentID> |
| 225 | + <Name></Name> |
| 226 | + <GUID></GUID> |
| 227 | + </Table> |
| 228 | + </NewDataSet> |
| 229 | + </ScriptFolder> |
| 230 | + </PackedScript> |
| 231 | +</LabTech_Expansion> |
| 232 | +"@ |
| 233 | + |
| 234 | + #Query MySQL for script data. |
| 235 | + $ScriptXML = Get-LTData -query "SELECT * FROM lt_scripts WHERE ScriptID=$ScriptID" |
| 236 | + $ScriptData = Get-LTData -query "SELECT CONVERT(ScriptData USING utf8) AS Data FROM lt_scripts WHERE ScriptID=$ScriptID" |
| 237 | + $ScriptLicense = Get-LTData -query "SELECT CONVERT(LicenseData USING utf8) AS License FROM lt_scripts WHERE ScriptID=$ScriptID" |
| 238 | + |
| 239 | + #Save script data to the template. |
| 240 | + $ExportTemplate.LabTech_Expansion.PackedScript.NewDataSet.Table.ScriptId = "$($ScriptXML.ScriptId)" |
| 241 | + $ExportTemplate.LabTech_Expansion.PackedScript.NewDataSet.Table.FolderId = "$($ScriptXML.FolderId)" |
| 242 | + $ExportTemplate.LabTech_Expansion.PackedScript.NewDataSet.Table.ScriptName = "$($ScriptXML.ScriptName)" |
| 243 | + $ExportTemplate.LabTech_Expansion.PackedScript.NewDataSet.Table.ScriptNotes = "$($ScriptXML.ScriptNotes)" |
| 244 | + $ExportTemplate.LabTech_Expansion.PackedScript.NewDataSet.Table.Permission = "$($ScriptXML.Permission)" |
| 245 | + $ExportTemplate.LabTech_Expansion.PackedScript.NewDataSet.Table.EditPermission = "$($ScriptXML.EditPermission)" |
| 246 | + $ExportTemplate.LabTech_Expansion.PackedScript.NewDataSet.Table.ComputerScript = "$($ScriptXML.ComputerScript)" |
| 247 | + $ExportTemplate.LabTech_Expansion.PackedScript.NewDataSet.Table.LocationScript = "$($ScriptXML.LocationScript)" |
| 248 | + $ExportTemplate.LabTech_Expansion.PackedScript.NewDataSet.Table.MaintenanceScript = "$($ScriptXML.MaintenanceScript)" |
| 249 | + $ExportTemplate.LabTech_Expansion.PackedScript.NewDataSet.Table.FunctionScript = "$($ScriptXML.FunctionScript)" |
| 250 | + $ExportTemplate.LabTech_Expansion.PackedScript.NewDataSet.Table.LicenseData = "$($ScriptLicense.License)" |
| 251 | + $ExportTemplate.LabTech_Expansion.PackedScript.NewDataSet.Table.ScriptData = "$($ScriptData.Data)" |
| 252 | + $ExportTemplate.LabTech_Expansion.PackedScript.NewDataSet.Table.ScriptVersion = "$($ScriptXML.ScriptVersion)" |
| 253 | + $ExportTemplate.LabTech_Expansion.PackedScript.NewDataSet.Table.ScriptGuid = "$($ScriptXML.ScriptGuid)" |
| 254 | + $ExportTemplate.LabTech_Expansion.PackedScript.NewDataSet.Table.ScriptFlags = "$($ScriptXML.ScriptFlags)" |
| 255 | + $ExportTemplate.LabTech_Expansion.PackedScript.NewDataSet.Table.Parameters = "$($ScriptXML.Parameters)" |
| 256 | + |
| 257 | + #Format Script Name |
| 258 | + #Remove special characters |
| 259 | + $FileName = $($ExportTemplate.LabTech_Expansion.PackedScript.NewDataSet.Table.ScriptName).Replace('*','') |
| 260 | + $FileName = $FileName.Replace('/','') |
| 261 | + $FileName = $FileName.Replace('<','') |
| 262 | + $FileName = $FileName.Replace('>','') |
| 263 | + $FileName = $FileName.Replace(':','') |
| 264 | + $FileName = $FileName.Replace('"','') |
| 265 | + $FileName = $FileName.Replace('\','') |
| 266 | + $FileName = $FileName.Replace('|','') |
| 267 | + $FileName = $FileName.Replace('?','') |
| 268 | + #Add last modification date |
| 269 | + $FileName = $($FileName) + '--' + $($ScriptXML.Last_Date.ToString("yyyy-MM-dd--HH-mm-ss")) |
| 270 | + #Add last user to modify |
| 271 | + $FileName = $($FileName) + '--' + $($ScriptXML.Last_User.Substring(0, $ScriptXML.Last_User.IndexOf('@'))) |
| 272 | + $ScriptFolderName = $($FolderData.Name) |
| 273 | + |
| 274 | + #Check folder information |
| 275 | + |
| 276 | + #Check if script is at root and not in a folder |
| 277 | + If ($($ScriptXML.FolderId) -eq 0) { |
| 278 | + #Delete folder information from template |
| 279 | + $ExportTemplate.LabTech_Expansion.PackedScript.ScriptFolder.RemoveAttribute() |
| 280 | + } |
| 281 | + Else { |
| 282 | + #Query MySQL for folder data. |
| 283 | + $FolderData = Get-LTData -query "SELECT * FROM `scriptfolders` WHERE FolderID=$($ScriptXML.FolderId)" |
| 284 | + |
| 285 | + #Check if folder is no longer present. |
| 286 | + if ($FolderData -eq $null) { |
| 287 | + Write-Output "ScritID $($ScriptXML.ScriptId) references folder $($ScriptXML.FolderId), this folder is no longer present. Setting to root folder." |
| 288 | + Write-Output "It is recomended that you move this script to a folder." |
| 289 | + |
| 290 | + #Set to FolderID 0 |
| 291 | + $ExportTemplate.LabTech_Expansion.PackedScript.NewDataSet.Table.FolderId = "0" |
| 292 | + $ScriptXML.FolderID = 0 |
| 293 | + |
| 294 | + #Delete folder information from template |
| 295 | + $ExportTemplate.LabTech_Expansion.PackedScript.ScriptFolder.RemoveAttribute() |
| 296 | + } |
| 297 | + Else { |
| 298 | + #Format the folder name. |
| 299 | + #Remove special characters |
| 300 | + $FolderName = $($FolderData.Name).Replace('*','') |
| 301 | + $FolderName = $FolderName.Replace('/','') |
| 302 | + $FolderName = $FolderName.Replace('<','') |
| 303 | + $FolderName = $FolderName.Replace('>','') |
| 304 | + $FolderName = $FolderName.Replace(':','') |
| 305 | + $FolderName = $FolderName.Replace('"','') |
| 306 | + $FolderName = $FolderName.Replace('\','') |
| 307 | + $FolderName = $FolderName.Replace('|','') |
| 308 | + $FolderName = $FolderName.Replace('?','') |
| 309 | + |
| 310 | + # Save folder data to the template. |
| 311 | + $ExportTemplate.LabTech_Expansion.PackedScript.ScriptFolder.NewDataSet.Table.FolderID = "$($FolderData.FolderID)" |
| 312 | + $ExportTemplate.LabTech_Expansion.PackedScript.ScriptFolder.NewDataSet.Table.ParentID = "$($FolderData.ParentID)" |
| 313 | + $ExportTemplate.LabTech_Expansion.PackedScript.ScriptFolder.NewDataSet.Table.Name = "$FolderName" |
| 314 | + $ExportTemplate.LabTech_Expansion.PackedScript.ScriptFolder.NewDataSet.Table.GUID = "$($FolderData.GUID)" |
| 315 | + } |
| 316 | + } |
| 317 | + |
| 318 | + #Create Folder Structure |
| 319 | + If ($($ScriptXML.FolderId) -eq 0) { |
| 320 | + #Create folder |
| 321 | + New-Item -ItemType Directory -Force -Path $BackupRoot\$($FolderData.Name) | Out-Null |
| 322 | + |
| 323 | + #Save XML |
| 324 | + $ExportTemplate.Save("$BackupRoot\$($FolderData.Name)\$($FileName).xml") |
| 325 | + } |
| 326 | + Else { |
| 327 | + #Query info for parent folder |
| 328 | + $ParentFolderName = $(Get-LTData "SELECT * FROM scriptfolders WHERE FolderID=$($FolderData.ParentID)").Name |
| 329 | + |
| 330 | + #Format parent folder name |
| 331 | + #Remove special characters |
| 332 | + $ParentFolderName = $ParentFolderName.Replace('*','') |
| 333 | + $ParentFolderName = $ParentFolderName.Replace('/','') |
| 334 | + $ParentFolderName = $ParentFolderName.Replace('<','') |
| 335 | + $ParentFolderName = $ParentFolderName.Replace('>','') |
| 336 | + $ParentFolderName = $ParentFolderName.Replace(':','') |
| 337 | + $ParentFolderName = $ParentFolderName.Replace('"','') |
| 338 | + $ParentFolderName = $ParentFolderName.Replace('\','') |
| 339 | + $ParentFolderName = $ParentFolderName.Replace('|','') |
| 340 | + $ParentFolderName = $ParentFolderName.Replace('?','') |
| 341 | + |
| 342 | + $FilePath = "$BackupRoot\$($ParentFolderName)\$($ScriptFolderName)" |
| 343 | + |
| 344 | + #Create folder |
| 345 | + New-Item -ItemType Directory -Force -Path $FilePath | Out-Null |
| 346 | + |
| 347 | + #Save XML |
| 348 | + $ExportTemplate.Save("$FilePath\$($FileName).xml") |
| 349 | + } |
| 350 | + |
| 351 | +} |
| 352 | + |
| 353 | +#endregion |
| 354 | + |
| 355 | +#region-[Execution]------------------------------------------------------------ |
| 356 | + |
| 357 | + Start-Transcript -Path $sTranscriptFile -Force -Append |
| 358 | + |
| 359 | + Write-Output "Getting list of all scripts." |
| 360 | + |
| 361 | + #Query list of all ScriptID's |
| 362 | + $ScriptIDs = Get-LTData "SELECT ScriptID FROM lt_scripts" |
| 363 | + |
| 364 | + Write-Output "$($ScriptIDs.count) scripts to process." |
| 365 | + |
| 366 | + #Process each ScriptID |
| 367 | + $n = 0 |
| 368 | + foreach ($ScriptID in $ScriptIDs) { |
| 369 | + #Progress bar |
| 370 | + $n++ |
| 371 | + Write-Progress -Activity "Backing up LT scripts to $BackupRoot" -Status "Processing ScriptID $($ScriptID.ScriptID)" -PercentComplete ($n / $ScriptIDs.Count*100) |
| 372 | + |
| 373 | + #Export current script |
| 374 | + Export-LTScript -ScriptID $($ScriptID.ScriptID) -FilePath $FilePath |
| 375 | + } |
| 376 | + |
| 377 | + Write-Output "Export finished." |
| 378 | + |
| 379 | + Stop-Transcript |
| 380 | + |
| 381 | + #Limit Log file to 1000 lines |
| 382 | + (Get-Content $sTranscriptFile -tail 1000 -readcount 0) | Set-Content $sTranscriptFile -Force |
| 383 | + |
| 384 | +#endregion |
| 385 | + |
| 386 | + |
| 387 | +.PARAMETER Query |
| 388 | + Input your MySQL query in double quotes. |
| 389 | + |
| 390 | + |
| 391 | + |
| 392 | + |
| 393 | + |
0 commit comments