<#

Sample script to copy logs from a directory to a centralized backup location

Path can only contain folders under c:\


#>

[cmdletbinding()]
param(
    [string[]] $Path = @( #No longer backing these up during CompatScan TS on Fails.

        "$env:Systemdrive\`$WINDOWS.~BT\Sources\Panther"
        "$env:Systemdrive\`$WINDOWS.~BT\Sources\Rollback"
        "$env:SystemRoot\Panther"
        "$env:SystemRoot\SysWOW64\PKG_LOGS"
        "$env:SystemRoot\CCM\Logs"
<#
        "$env:SystemRoot\System32\winevt\Logs"
        "$env:SystemRoot\Logs\CBS\CBS.log"
        "$env:SystemRoot\inf\setupapi.upgrade.log"
        "$env:SystemRoot\Logs\MoSetup\BlueBox.log"
#>
    ),
    [string[]] $Path1 = @( #Only Backing these up on Fails Now


        "$env:SystemRoot\CCM\Logs"
        "$env:Systemdrive\ProgramData\WaaS\Logs"
    ),

    [string] $TargetRoot = '\\src\logs$',
    [string] $LogID = "IPU\$env:ComputerName",
    [string[]] $Exclude = @( '*.exe','*.wim','*.dll','*.ttf','*.mui' ),
    [switch] $recurse,
    [switch] $SkipZip
)

#Setup TS Environment
try
{
    $tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
    $FailedStep = $TSEnv.Value('FailedStepName')
    $tsBuild = $tsenv.Value("SMSTS_Build") #Get Build Number from TS Variable.
    $RegistryVar = "HKLM:\$($tsenv.Value('RegistryPath'))"
    $registryPath = "$RegistryVar\$tsbuild"
}
catch
{
	Write-Verbose "Not running in a task sequence."
}

#region Prepare Target

write-verbose "Log Archive Tool  1.0.<Version>" 

write-verbose "Create Target $TargetRoot\$LogID"
new-item -itemtype Directory -Path $TargetRoot\$LogID -force -erroraction SilentlyContinue | out-null 

$TagFile = "$TargetRoot\$LogID\$($LogID.Replace('\','_'))"

#endregion



#region Create temporary Store

$TempPath = [System.IO.Path]::GetTempFileName()
remove-item $TempPath
new-item -type directory -path $TempPath -force | out-null

<#
#First Zip File for Panther & Windows Logs - NOT USING DURING COMPAT SCAN... didn't find much need after enabling SetupDiag
foreach ( $Item in $Path ) { 

    $TmpTarget = (join-path $TempPath ( split-path -NoQualifier $Item ))
    write-Verbose "COPy $Item to $TmpTarget"
    copy-item -path $Item -Destination $TmpTarget -Force -Recurse -exclude $Exclude -ErrorAction SilentlyContinue

}

Compress-Archive -path "$TempPath\*" -DestinationPath "$TargetRoot\$LogID\$($LogID.Replace('\','_'))-$([datetime]::now.Tostring('s').Replace(':','-'))-WindowsLogs.zip" -Force

#Reset for 2nd Group of Logs
remove-item $tempPath -Recurse -Force
new-item -type directory -path $TempPath -force | out-null
#>

#Second Zip File for CCM Logs & WaaS logs & SetupDiag
foreach ( $Item1 in $Path1 ) { 

    $TmpTarget = (join-path $TempPath ( split-path -NoQualifier $Item1 ))
    write-Verbose "COPy $Item1 to $TmpTarget"
    copy-item -path $Item1 -Destination $TmpTarget -Force -Recurse -exclude $Exclude -ErrorAction SilentlyContinue

}

Compress-Archive -path "$TempPath\*" -DestinationPath "$TargetRoot\$LogID\$($LogID.Replace('\','_'))-$([datetime]::now.Tostring('s').Replace(':','-'))-WaaSLogs.zip" -Force
remove-item $tempPath -Recurse -Force


#Add Log File to Folder that Contains WaaS Registry Key Info & Failed Step Name
get-item $registryPath | Out-File "$($TargetRoot)\$($LogID)\$($FailedStep).log"
#grab Path info and write out as well, since we've had issues with machines having messed up Path.
Write-Output ($env:path).Split(";") | Out-File "$($TargetRoot)\$($LogID)\$($FailedStep).log" -Append


#endregion
