2828
2929# region-[Declarations]----------------------------------------------------------
3030
31- # Set Error Action to Silently Continue
32- $ErrorActionPreference = " SilentlyContinue"
31+ $Date = Get-Date
3332
3433 # Get/Save config info
3534 if ($ (Test-Path $PSScriptRoot \LT- ScriptExport- Config.xml) -eq $false ) {
4140 <MySQLDatabase></MySQLDatabase>
4241 <MySQLHost></MySQLHost>
4342 <CredPath></CredPath>
43+ <LastExport>0</LastExport>
4444</Settings>
4545'@
4646
4747 # 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" }
48+ $Config.Settings.LogPath = " $ ( Read-Host " Path of log file ($ ( $env: windir ) \LTSvc \Logs)" ) "
49+ if ($Config.Settings.LogPath -eq ' ' ) {$Config.Settings.LogPath = " $ ( $env: windir ) \LTSvc \Logs" }
50+ $Config.Settings.BackupRoot = " $ ( Read-Host " Path of exported scripts (${ env: ProgramFiles (x86)} \LabTech\Backup\Scripts)" ) "
51+ if ($Config.Settings.BackupRoot -eq ' ' ) {$Config.Settings.BackupRoot = " ${ env: ProgramFiles (x86)} \LabTech\Backup\Scripts" }
5252 $Config.Settings.MySQLDatabase = " $ ( Read-Host " Name of LabTech database (labtech)" ) "
5353 if ($Config.Settings.MySQLDatabase -eq ' ' ) {$Config.Settings.MySQLDatabase = " labtech" }
5454 $Config.Settings.MySQLHost = " $ ( Read-Host " FQDN of LabTechServer (localhost)" ) "
6161 [xml ]$Config = Get-Content " $PSScriptRoot \LT-ScriptExport-Config.xml"
6262 }
6363
64+ # Location to credentials file
65+ $CredPath = $Config.Settings.CredPath
66+
6467 # Get/Save user/password info
6568 if ($ (Test-Path $CredPath ) -eq $false ) {New-Item - ItemType Directory - Force - Path $CredPath | Out-Null }
6669 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}
7881 $MySQLAdminPassword = (IMPORT-CLIXML $CredPath \LTDBCredentials.xml).GetNetworkCredential().Password
7982 $MySQLAdminUserName = (IMPORT-CLIXML $CredPath \LTDBCredentials.xml).GetNetworkCredential().UserName
8083
81- # Location to credentials file
82- $CredPath = $Config.Settings.CredPath
83-
8484# endregion
8585
8686# region-[Functions]------------------------------------------------------------
@@ -185,9 +185,7 @@ Function Export-LTScript {
185185 [CmdletBinding ()]
186186 Param (
187187 [Parameter (Mandatory = $True , Position = 1 )]
188- [string ]$ScriptID ,
189- [Parameter (Mandatory = $True , Position = 2 )]
190- [string ]$FilePath
188+ [string ]$ScriptID
191189 )
192190
193191 # LabTech XML template
@@ -256,20 +254,20 @@ Function Export-LTScript {
256254
257255 # Format Script Name
258256 # Remove special characters
259- $FileName = $ ($ExportTemplate .LabTech_Expansion.PackedScript.NewDataSet.Table .ScriptName ).Replace(' *' , ' ' )
260- $FileName = $FileName.Replace (' /' , ' ' )
257+ $FileName = $ ($ScriptXML .ScriptName ).Replace(' *' , ' ' )
258+ $FileName = $FileName.Replace (' /' , ' - ' )
261259 $FileName = $FileName.Replace (' <' , ' ' )
262260 $FileName = $FileName.Replace (' >' , ' ' )
263261 $FileName = $FileName.Replace (' :' , ' ' )
264262 $FileName = $FileName.Replace (' "' , ' ' )
265- $FileName = $FileName.Replace (' \' , ' ' )
263+ $FileName = $FileName.Replace (' \' , ' - ' )
266264 $FileName = $FileName.Replace (' |' , ' ' )
267265 $FileName = $FileName.Replace (' ?' , ' ' )
268266 # Add last modification date
269267 $FileName = $ ($FileName ) + ' --' + $ ($ScriptXML.Last_Date.ToString (" yyyy-MM-dd--HH-mm-ss" ))
270268 # Add last user to modify
271269 $FileName = $ ($FileName ) + ' --' + $ ($ScriptXML.Last_User.Substring (0 , $ScriptXML.Last_User.IndexOf (' @' )))
272- $ScriptFolderName = $ ( $FolderData .Name )
270+
273271
274272 # Check folder information
275273
@@ -298,15 +296,16 @@ Function Export-LTScript {
298296 # Format the folder name.
299297 # Remove special characters
300298 $FolderName = $ ($FolderData.Name ).Replace(' *' , ' ' )
301- $FolderName = $FolderName.Replace (' /' , ' ' )
299+ $FolderName = $FolderName.Replace (' /' , ' - ' )
302300 $FolderName = $FolderName.Replace (' <' , ' ' )
303301 $FolderName = $FolderName.Replace (' >' , ' ' )
304302 $FolderName = $FolderName.Replace (' :' , ' ' )
305303 $FolderName = $FolderName.Replace (' "' , ' ' )
306- $FolderName = $FolderName.Replace (' \' , ' ' )
304+ $FolderName = $FolderName.Replace (' \' , ' - ' )
307305 $FolderName = $FolderName.Replace (' |' , ' ' )
308306 $FolderName = $FolderName.Replace (' ?' , ' ' )
309307
308+
310309 # Save folder data to the template.
311310 $ExportTemplate.LabTech_Expansion.PackedScript.ScriptFolder.NewDataSet.Table.FolderID = " $ ( $FolderData.FolderID ) "
312311 $ExportTemplate.LabTech_Expansion.PackedScript.ScriptFolder.NewDataSet.Table.ParentID = " $ ( $FolderData.ParentID ) "
@@ -315,13 +314,13 @@ Function Export-LTScript {
315314 }
316315 }
317316
318- # Create Folder Structure
319- If ($ ($ScriptXML .FolderId ) -eq 0 ) {
317+ # Create Folder Structure. Check for parent folder
318+ If ($ ($FolderData .ParentId ) -eq 0 ) {
320319 # Create folder
321320 New-Item - ItemType Directory - Force - Path $BackupRoot \$ ($FolderData.Name ) | Out-Null
322321
323322 # Save XML
324- $ExportTemplate.Save (" $BackupRoot \$ ( $FolderData .Name ) \$ ( $FileName ) .xml" )
323+ $ExportTemplate.Save (" $BackupRoot \$FolderName \$ ( $FileName ) .xml" )
325324 }
326325 Else {
327326 # Query info for parent folder
@@ -330,12 +329,12 @@ Function Export-LTScript {
330329 # Format parent folder name
331330 # Remove special characters
332331 $ParentFolderName = $ParentFolderName.Replace (' *' , ' ' )
333- $ParentFolderName = $ParentFolderName.Replace (' /' , ' ' )
332+ $ParentFolderName = $ParentFolderName.Replace (' /' , ' - ' )
334333 $ParentFolderName = $ParentFolderName.Replace (' <' , ' ' )
335334 $ParentFolderName = $ParentFolderName.Replace (' >' , ' ' )
336335 $ParentFolderName = $ParentFolderName.Replace (' :' , ' ' )
337336 $ParentFolderName = $ParentFolderName.Replace (' "' , ' ' )
338- $ParentFolderName = $ParentFolderName.Replace (' \' , ' ' )
337+ $ParentFolderName = $ParentFolderName.Replace (' \' , ' - ' )
339338 $ParentFolderName = $ParentFolderName.Replace (' |' , ' ' )
340339 $ParentFolderName = $ParentFolderName.Replace (' ?' , ' ' )
341340
@@ -356,29 +355,40 @@ Function Export-LTScript {
356355
357356 Start-Transcript - Path $sTranscriptFile - Force - Append
358357
358+ if ((Test-Path $BackupRoot ) -eq $false ){New-Item - ItemType Directory - Force - Path $BackupRoot | Out-Null }
359+
359360 Write-Output " Getting list of all scripts."
360361
362+ $ScriptIDs = @ {}
361363 # Query list of all ScriptID's
362- $ScriptIDs = Get-LTData " SELECT ScriptID FROM lt_scripts"
363-
364- Write-Output " $ ( $ScriptIDs.count ) scripts to process."
364+ if ($ ($Config.Settings.LastExport ) -eq 0 ) {
365+ $ScriptIDs = Get-LTData " SELECT ScriptID FROM lt_scripts"
366+ }
367+ else {
368+ $Query = $ (" SELECT ScriptID FROM lt_scripts WHERE Last_Date > " + " '" + $ ($Config.Settings.LastExport ) + " '" )
369+ $ScriptIDs = Get-LTData $Query
370+ }
371+
372+ Write-Output " $ ( @ ($ScriptIDs ).count) scripts to process."
365373
366374 # Process each ScriptID
367375 $n = 0
368376 foreach ($ScriptID in $ScriptIDs ) {
369377 # Progress bar
370378 $n ++
371- Write-Progress - Activity " Backing up LT scripts to $BackupRoot " - Status " Processing ScriptID $ ( $ScriptID.ScriptID ) " - PercentComplete ($n / $ScriptIDs.Count * 100 )
379+ Write-Progress - Activity " Backing up LT scripts to $BackupRoot " - Status " Processing ScriptID $ ( $ScriptID.ScriptID ) " - PercentComplete ($n / @ ( $ScriptIDs ).count * 100 )
372380
373381 # Export current script
374- Export-LTScript - ScriptID $ ($ScriptID.ScriptID ) - FilePath $FilePath
382+ Export-LTScript - ScriptID $ ($ScriptID.ScriptID )
375383 }
376384
377385 Write-Output " Export finished."
378-
386+ $Config.Settings.LastExport = " $ ( $Date.ToString (" yyy-MM-dd HH:mm:ss" )) "
387+ $Config.Save (" $PSScriptRoot \LT-ScriptExport-Config.xml" )
388+
379389 Stop-Transcript
380390
381- # Limit Log file to 1000 lines
382- (Get-Content $sTranscriptFile - tail 1000 - readcount 0 ) | Set-Content $sTranscriptFile - Force
391+ # Limit Log file to 50000 lines
392+ (Get-Content $sTranscriptFile - tail 50000 - readcount 0 ) | Set-Content $sTranscriptFile - Force - Encoding Ascii
383393
384394# endregion
0 commit comments