Even though the $logDir
variable contains the value "C:\Users\DEVELO~1\AppData\Local\Temp\logs"
(obtained from a text file), this code does not behave as I'd expect. It returns an array of strings, not a single string.
$LogFileName = "NoIP-update.log"
$ConfigSection = "NoIp"
function Log-Message ($MSG) {
$script:Logger += "$(get-date -format u) $MSG`n"
Write-Output $MSG
}
function Get-LogFileName ($config) {
$Local:logDir = $config["Section"]["LOGDIR"]
if ($Local:logDir -ne "") {
Log-Message "Testing for directory '$Local:logDir'"
if(!(Test-Path -Path $Local:logDir )){
New-Item -ItemType directory -Path $Local:logDir
Log-Message "Created directory '$Local:logDir'"
}
return "$Local:logDir\$LogFileName"
}
return $LogFileName
}
I'm probably missing something very very basic, as I expect the function to return 1 string, not an array of strings and log one or two lines.
However, it doesn't log, and: if $Local:logDir
exists, the function returns an array of 2 strings, otherwise it returns an array of 4 strings.
- Why?
- How can I make it return 1 string and still log?
I think it has to do with thinking using the pipelining metaphor versus my background in procedural and object oriented programming languages. Help is much appreciated (:
2-string array:
{Testing for directory 'C:\Users\DEVELO~1\AppData\Local\Temp\logs', C:\Users\DEVELO~1\AppData\Local\Temp\logs\NoIP-update.log}
4-string array:
{Testing for directory 'C:\Users\DEVELO~1\AppData\Local\Temp\logs', C:\Users\Developer\AppData\Local\Temp\logs, Created directory 'C:\Users\DEVELO~1\AppData\Local\Temp\logs', C:\Users\DEVELO~1\AppData\Local\Temp\logs\NoIP-update.log}
Notes:
- I run from within PowerGUI, but from the PowerShell console it has the same results.
$PSVersionTable.PSVersion
returns4.0
.