I have programmed for years in other languages… new to PowerShell.
I have written a function that takes two strings as parameters.
The function should return a string value (which could be just “”).
When I attempt to return the string and assign it to a variable, the variable suddenly contains the wrong values, and not the string that was assigned to the return variable on the line before the return.
I cannot reproduce it creating a simple function.
However… while writing this, I may have gotten closer to the cause (just by trying to explain myself).
I have another function I wrote that does logging for me. The function takes two required parameters (int, and string). The integer is the debug level (whether or not to log it) and the string is “what to log”.
I instantiate the logger using something like (where outPFn is the full path/filename.ext to the log file):
. “$($scriptpath)\rrLogger.ps1” -outPFn $outPFn -masterlvl 2
Then the logger code looks like:
----------------------------------------------------------
param ([string] $outPFn, [int] $masterlvl)
if(-not($outPFn)) { Throw “. /rrLogger… You must supply a value for -outPFn” }
if(-not($masterlvl)) { $masterlvl=2 }
function rrlog
{
Param ([int] $dbg, $msg)
if ($dbg -le $masterlvl)
{
$lstmp=$(get-date -f ‘yyyyMMdd_HHmmss’)
$outStr=“$($lstmp): $($msg)”
Write-Output “$($outStr)”
Add-Content $outPFn “$($outStr)”
}
}
----------------------------------------------------------
So, a typical call to the logger looks something like:
rrlog 2 “(L# 139): theDriverWeWant: ‘$($theDriverWeWant)’”
Which would produce a line in the log file something like:
20181130_123654: (L# 139): theDriverWeWant: ‘’
Without attempting to paste the entire (right now ugly) code here, the code looks something like:
----------------------------------------------------------
function GetDriver([string]$RegKey, [string]$oracle_home) {
rrlog 2 “(L# 102): RegKey: ‘$($RegKey)’”
$theDriverWeWant=“some string”
return $theDriverWeWant
}
$theDriverWeWant = GetDriver $RegKey $oracle_home
----------------------------------------------------------
After running that, $theDriverWeWant contains something like:
‘20181130_123654: (L# 102): RegKey: hklm:\software.…’
Rather than:
‘some string’
How do I do logging with my function and still return the actual value of $theDriverWeWant, and not the total collection of all of the $outStr’s I wrote to my log file?
Or, maybe I should ask “How should I be doing logging?”
Or, should I go back to the languages I actually know?
Thanks in advance!!