2222 Website: www.labtechconsulting.com
2323 Creation Date: 9/11/2015
2424 Purpose/Change: Initial script development
25+
26+ Version: 1.1
27+ Author: Chris Taylor
28+ Website: www.labtechconsulting.com
29+ Creation Date: 9/23/2015
30+ Purpose/Change: Added error catching
2531#>
2632
2733# Requires -Version 3.0
2834
2935# region-[Declarations]----------------------------------------------------------
3036
37+ $ScriptVersion = " 1.1"
38+
3139 $ErrorActionPreference = " Stop"
3240
3341 $Date = Get-Date
4553 <LastExport>0</LastExport>
4654</Settings>
4755'@
48-
49- # Create config file
50- $Config.Settings.LogPath = " $ ( Read-Host " Path of log file ($ ( $env: windir ) \LTSvc\Logs)" ) "
51- if ($Config.Settings.LogPath -eq ' ' ) {$Config.Settings.LogPath = " $ ( $env: windir ) \LTSvc\Logs" }
52- $Config.Settings.BackupRoot = " $ ( Read-Host " Path of exported scripts (${env: ProgramFiles(x86)} \LabTech\Backup\Scripts)" ) "
53- if ($Config.Settings.BackupRoot -eq ' ' ) {$Config.Settings.BackupRoot = " ${env: ProgramFiles(x86)} \LabTech\Backup\Scripts" }
54- $Config.Settings.MySQLDatabase = " $ ( Read-Host " Name of LabTech database (labtech)" ) "
55- if ($Config.Settings.MySQLDatabase -eq ' ' ) {$Config.Settings.MySQLDatabase = " labtech" }
56- $Config.Settings.MySQLHost = " $ ( Read-Host " FQDN of LabTechServer (localhost)" ) "
57- if ($Config.Settings.MySQLHost -eq ' ' ) {$Config.Settings.MySQLHost = " localhost" }
58- $Config.Settings.CredPath = " $ ( Read-Host " Path of credentials ($PSScriptRoot )" ) "
59- if ($Config.Settings.CredPath -eq ' ' ) {$Config.Settings.CredPath = " $PSScriptRoot " }
60- $Config.Save (" $PSScriptRoot \LT-ScriptExport-Config.xml" )
56+ try {
57+ # Create config file
58+ $Config.Settings.LogPath = " $ ( Read-Host " Path of log file ($ ( $env: windir ) \LTSvc\Logs)" ) "
59+ if ($Config.Settings.LogPath -eq ' ' ) {$Config.Settings.LogPath = " $ ( $env: windir ) \LTSvc\Logs" }
60+ $Config.Settings.BackupRoot = " $ ( Read-Host " Path of exported scripts (${env: ProgramFiles(x86)} \LabTech\Backup\Scripts)" ) "
61+ if ($Config.Settings.BackupRoot -eq ' ' ) {$Config.Settings.BackupRoot = " ${env: ProgramFiles(x86)} \LabTech\Backup\Scripts" }
62+ $Config.Settings.MySQLDatabase = " $ ( Read-Host " Name of LabTech database (labtech)" ) "
63+ if ($Config.Settings.MySQLDatabase -eq ' ' ) {$Config.Settings.MySQLDatabase = " labtech" }
64+ $Config.Settings.MySQLHost = " $ ( Read-Host " FQDN of LabTechServer (localhost)" ) "
65+ if ($Config.Settings.MySQLHost -eq ' ' ) {$Config.Settings.MySQLHost = " localhost" }
66+ $Config.Settings.CredPath = " $ ( Read-Host " Path of credentials ($PSScriptRoot )" ) "
67+ if ($Config.Settings.CredPath -eq ' ' ) {$Config.Settings.CredPath = " $PSScriptRoot " }
68+ $Config.Save (" $PSScriptRoot \LT-ScriptExport-Config.xml" )
69+ }
70+ Catch {
71+ $ErrorMessage = $_.Exception.Message
72+ $FailedItem = $_.Exception.ItemName
73+ Write-Error " Error durring config creation: $FailedItem , $ErrorMessage `n $Error [0]"
74+ }
6175 }
6276 Else {
6377 [xml ]$Config = Get-Content " $PSScriptRoot \LT-ScriptExport-Config.xml"
7185 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}
7286
7387 # Transcript File Info
74- $sTranscriptName = " LT-ScriptExport.log"
75- $sTranscriptFile = ($Config.Settings.LogPath ) + " \" + $sTranscriptName
88+ $TranscriptName = " LT-ScriptExport.log"
89+ $TranscriptFile = ($Config.Settings.LogPath ) + " \" + $TranscriptName
7690
7791 # Location to the backp repository
7892 $BackupRoot = $Config.Settings.BackupRoot
@@ -275,8 +289,15 @@ Function Export-LTScript {
275289
276290 # Check if script is at root and not in a folder
277291 If ($ ($ScriptXML.FolderId ) -eq 0 ) {
278- # Delete folder information from template
279- $ExportTemplate.LabTech_Expansion.PackedScript.ScriptFolder.RemoveAttribute ()
292+ try {
293+ # Delete folder information from template
294+ $ExportTemplate.LabTech_Expansion.PackedScript.ScriptFolder.RemoveAttribute ()
295+ }
296+ Catch {
297+ $ErrorMessage = $_.Exception.Message
298+ $FailedItem = $_.Exception.ItemName
299+ Write-Error " Unable to remove folder data from XML: $FailedItem , $ErrorMessage `n $Error [0]"
300+ }
280301 }
281302 Else {
282303 # Query MySQL for folder data.
@@ -290,9 +311,16 @@ Function Export-LTScript {
290311 # Set to FolderID 0
291312 $ExportTemplate.LabTech_Expansion.PackedScript.NewDataSet.Table.FolderId = " 0"
292313 $ScriptXML.FolderID = 0
293-
294- # Delete folder information from template
295- $ExportTemplate.LabTech_Expansion.PackedScript.ScriptFolder.RemoveAttribute ()
314+
315+ try {
316+ # Delete folder information from template
317+ $ExportTemplate.LabTech_Expansion.PackedScript.ScriptFolder.RemoveAttribute ()
318+ }
319+ Catch {
320+ $ErrorMessage = $_.Exception.Message
321+ $FailedItem = $_.Exception.ItemName
322+ Write-Error " Unable to remove folder data from XML: $FailedItem , $ErrorMessage `n $Error [0]"
323+ }
296324 }
297325 Else {
298326 # Format the folder name.
@@ -307,7 +335,6 @@ Function Export-LTScript {
307335 $FolderName = $FolderName.Replace (' |' , ' ' )
308336 $FolderName = $FolderName.Replace (' ?' , ' ' )
309337
310-
311338 # Save folder data to the template.
312339 $ExportTemplate.LabTech_Expansion.PackedScript.ScriptFolder.NewDataSet.Table.FolderID = " $ ( $FolderData.FolderID ) "
313340 $ExportTemplate.LabTech_Expansion.PackedScript.ScriptFolder.NewDataSet.Table.ParentID = " $ ( $FolderData.ParentID ) "
@@ -318,11 +345,18 @@ Function Export-LTScript {
318345
319346 # Create Folder Structure. Check for parent folder
320347 If ($ ($FolderData.ParentId ) -eq 0 ) {
321- # Create folder
322- New-Item - ItemType Directory - Force - Path $BackupRoot \$ ($FolderData.Name ) | Out-Null
348+ try {
349+ # Create folder
350+ New-Item - ItemType Directory - Force - Path $BackupRoot \$ ($FolderData.Name ) | Out-Null
323351
324- # Save XML
325- $ExportTemplate.Save (" $BackupRoot \$FolderName \$ ( $FileName ) .xml" )
352+ # Save XML
353+ $ExportTemplate.Save (" $BackupRoot \$FolderName \$ ( $FileName ) .xml" )
354+ }
355+ Catch {
356+ $ErrorMessage = $_.Exception.Message
357+ $FailedItem = $_.Exception.ItemName
358+ Write-Error " Unable to save script: $FailedItem , $ErrorMessage `n $Error [0]"
359+ }
326360 }
327361 Else {
328362 # Query info for parent folder
@@ -342,22 +376,40 @@ Function Export-LTScript {
342376
343377 $FilePath = " $BackupRoot \$ ( $ParentFolderName ) \$ ( $ScriptFolderName ) "
344378
345- # Create folder
346- New-Item - ItemType Directory - Force - Path $FilePath | Out-Null
379+ try {
380+ # Create folder
381+ New-Item - ItemType Directory - Force - Path $FilePath | Out-Null
347382
348- # Save XML
349- $ExportTemplate.Save (" $FilePath \$ ( $FileName ) .xml" )
383+ # Save XML
384+ $ExportTemplate.Save (" $FilePath \$ ( $FileName ) .xml" )
385+ }
386+ Catch {
387+ $ErrorMessage = $_.Exception.Message
388+ $FailedItem = $_.Exception.ItemName
389+ Write-Error " Unable to save script: $FailedItem , $ErrorMessage `n $Error [0]"
390+ }
350391 }
351392
352393}
353394
354395# endregion
355396
356397# region-[Execution]------------------------------------------------------------
357-
358- Start-Transcript - Path $sTranscriptFile - Force - Append
398+ try {
399+ # Create log
400+ if ((Test-Path $Config.Settings.LogPath ) -eq $false ){New-Item - ItemType Directory - Force - Path $Config.Settings.LogPath | Out-Null }
401+ Start-Transcript - Path $TranscriptFile - Force - Append
359402
403+ # Check backup directory
360404 if ((Test-Path $BackupRoot ) -eq $false ){New-Item - ItemType Directory - Force - Path $BackupRoot | Out-Null }
405+ }
406+ Catch {
407+ $ErrorMessage = $_.Exception.Message
408+ $FailedItem = $_.Exception.ItemName
409+ Write-Error " Error durring log/backup directory creation: $FailedItem , $ErrorMessage `n $Error [0]"
410+ }
411+
412+ Write-Output " Script version: $ScriptVersion "
361413
362414 Write-Output " Getting list of all scripts."
363415
@@ -385,12 +437,20 @@ Function Export-LTScript {
385437 }
386438
387439 Write-Output " Export finished."
388- $Config.Settings.LastExport = " $ ( $Date.ToString (" yyy-MM-dd HH:mm:ss" )) "
389- $Config.Save (" $PSScriptRoot \LT-ScriptExport-Config.xml" )
440+
441+ try {
442+ $Config.Settings.LastExport = " $ ( $Date.ToString (" yyy-MM-dd HH:mm:ss" )) "
443+ $Config.Save (" $PSScriptRoot \LT-ScriptExport-Config.xml" )
444+ }
445+ Catch {
446+ $ErrorMessage = $_.Exception.Message
447+ $FailedItem = $_.Exception.ItemName
448+ Write-Error " Unable to update config with last export date: $FailedItem , $ErrorMessage `n $Error [0]"
449+ }
390450
391451 Stop-Transcript
392452
393453 # Limit Log file to 50000 lines
394- (Get-Content $sTranscriptFile - tail 50000 - readcount 0 ) | Set-Content $sTranscriptFile - Force - Encoding Ascii
454+ (Get-Content $TranscriptFile - tail 50000 - readcount 0 ) | Set-Content $TranscriptFile - Force - Encoding Ascii
395455
396456# endregion
0 commit comments