Our App packaging team want a standard naming convention for Log Files. We’d like to change the default log filename without editing the AppDeployToolkitMain.ps1 file so as to make it easier to migrate to new versions of the toolkit in the future. Is there any way to do this?
I’ve tried adding a $logName line at the end of the Variable Declaration section, before the
If ($deploymentType -ine ‘Uninstall’) {
line. This doesn’t seem to do anything.
You should set the log file name before the toolkit is dot sourced. So do it right after $scriptDirectory is defined. Also, if that doesn’t work, then try setting the scope of the variable like so: $script:logName
I’m no PowerShell guru so please bear with me.
Just to be clear: Is this for the MSI log files or for the PSAD generated log files?
The below assumes the latter but I just want to be sure,
First Issue: Assigning $logName
For what its worth, $logName is:
If (-not $logName) { [string]$logName = $installName + '' + $appDeployToolkitName + '' + $deploymentType + '.log' }
Which is built during the dot-sourcing of AppDeployToolkitMain.ps1.
You have your $logName code after the dot-sourcesing which means:
- a log file will initially get created with the original stock PSAD naming convention
- a new log file will then get created after you set $logName with your desired naming convention.
Fortunately it seems the PSAD creators had some foresight here and I think you just need to add $logName BEFORE AppDeployToolkitMain.ps1 is dot-sourced.
Second Issue: Testing $logName
So based on your original statement, you have your $logName after the catch statement where AppDeployToolkitMain.ps1 is dot-sourced but it “doesn’t seem to do anything.”
Right after the Catch statement, I have a block of code that looks like this:
write-log "logName before is '$logName'"
write-log "script:logName before is '$script:logName'"
write-log "global:logName before is '$global:logName'"
$logName = "My_Fake_Log_Name_Here.log"
write-log "logName after is $logName"
write-log "script:logName after is $script:logName"
write-log "$global:logName after is $global:logName"
break
When I execute via ISE, I see the following in the console:
[...]
[07-14-2015 11:36:52.121] [Initialization] [PSAppDeployToolkit] :: Session 0 not detected.
[07-14-2015 11:36:52.136] [Initialization] [PSAppDeployToolkit] :: Installation is running in [Interactive] mode.
[07-14-2015 11:36:52.152] [Initialization] [PSAppDeployToolkit] :: Deployment type is [Installation].
[07-14-2015 11:36:52.168] [Initialization] :: $logName before is 'Oracle_Java_7u79_EN_01_PSAppDeployToolkit_Install.log'
[07-14-2015 11:36:52.183] [Initialization] :: $script:logName before is 'Oracle_Java_7u79_EN_01_PSAppDeployToolkit_Install.log'
[07-14-2015 11:36:52.199] [Initialization] :: $global:logName before is 'Oracle_Java_7u79_EN_01_PSAppDeployToolkit_Install.log'
[07-14-2015 11:36:52.199] [Initialization] :: $logName after is My_Fake_Log_Name_Here.log
[07-14-2015 11:36:52.214] [Initialization] :: $script:logName after is My_Fake_Log_Name_Here.log
[07-14-2015 11:36:52.230] [Initialization] :: $global:logName after is My_Fake_Log_Name_Here.log
You should see similar results.
But you achieve what you want, and that’s a custom log file name, you want to set $logName above the try/catch statement so that it looks like this:
## Variables: Environment
[string]$scriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
write-log "logName before is '$logName'"
write-log "script:logName before is '$script:logName'"
write-log "global:logName before is '$global:logName'"
$logName = "My_New_Fake_Log_Name_Here.log"
## Dot source the required App Deploy Toolkit Functions
Try {
[string]$moduleAppDeployToolkitMain = "$scriptDirectory\AppDeployToolkit\AppDeployToolkitMain.ps1"
If (-not (Test-Path -Path $moduleAppDeployToolkitMain -PathType Leaf)) { Throw "Module does not exist at the specified location [$moduleAppDeployToolkitMain]." }
If ($DisableLogging) { . $moduleAppDeployToolkitMain -DisableLogging } Else { . $moduleAppDeployToolkitMain }
}
Catch {
[int32]$mainExitCode = 60008
Write-Error -Message "Module [$moduleAppDeployToolkitMain] failed to load: 'n$($_.Exception.Message)'n 'n$($_.InvocationInfo.PositionMessage)" -ErrorAction 'Continue'
Exit $mainExitCode
}
write-log "$logName after is $logName"
write-log "$script:logName after is $script:logName"
write-log "$global:logName after is $global:logName"
You should end up with this in the console:
[07-14-2015 11:54:17.338] [Initialization] :: $logName before is ''
[07-14-2015 11:54:17.416] [Initialization] :: $script:logName before is ''
[07-14-2015 11:54:17.416] [Initialization] :: $global:logName before is ''
...
[07-14-2015 11:54:26.789] [Initialization] [PSAppDeployToolkit] :: Session 0 not detected.
[07-14-2015 11:54:26.789] [Initialization] [PSAppDeployToolkit] :: Installation is running in [Interactive] mode.
[07-14-2015 11:54:26.805] [Initialization] [PSAppDeployToolkit] :: Deployment type is [Installation].
[07-14-2015 11:54:26.821] [Initialization] :: $logName after is My_New_Fake_Log_Name_Here.log
[07-14-2015 11:54:26.821] [Initialization] :: $script:logName after is My_New_Fake_Log_Name_Here.log
[07-14-2015 11:54:26.836] [Initialization] :: $global:logName after is My_New_Fake_Log_Name_Here.log
And you’ll have only one log file named the way you want.
Thank you very much. Now I actually understand what I was looking for. It works fine now, and I don’t have to mess with the main script file. Bonus.