Author Posts

October 27, 2015 at 11:18 am

So, I am rebuilding all my scripts with the use of a library of functions. I call my library using the . DOT notation.
All "migrated" functions run fine except for this one (part of it):
{
...
....
# Create LogFolder for present run
$LogFolder = $SwFolder + "\" + [string]$(get-date -f yyMMddHHmm)
Write-Host "LogFolderInFunc = "$LogFolder
New-Item -ItemType Directory -Force -Path $LogFolder
}
$LogFolder is created, so this part is working.

But returning to the main script $LogFolder only holds the value of:
"\" + [string]$(get-date -f yyMMddHHmm).
Strangly $SwFolder is vanished

Where am I going wrong?

October 27, 2015 at 1:33 pm

You have a variable scoping issue. Please read up on about_scoping. Solution is to create a function that returns a folder object (directoryinfo) or a string. Create an example for you:

Alternative is to use scoping when you declare variables like $global:VariableName however global declarations like this is not best practice. Better to use $script:VariableName.

Cheers

Tore

October 27, 2015 at 8:33 pm

Did not know I was looking for this 😉
It does make sense and I now know all about it!
And it is working like a charm
Thx Tore...