Author Posts

October 6, 2014 at 3:13 pm

I have several questions about Lab 5.6 in Learning Powershell in a Month of Lunches. The book gives a function that doesn't work because of a scope issue. The solution that is given to solve it, is to make the scope global when creating the PSDrives. Yet, in the chapter, it says "We get particularly irritated when we see people writing scripts that dump things into some $global:variable in order to pass information from one function to another." Seems to be contradictory.

Also, twice now, I've noticed that the book says when the function ends, the variables are no longer available. That is not true on my system. In the lab that we're discussing, for instance, I can access the Appdata: and Temp: drives. Wondering what's different about my system that I can access the drives after the script ends.

October 6, 2014 at 3:14 pm

Instead of renaming the ps1, I just copied the code from the book (that we're supposed to fix in the lab). It's below.

Function New-Drives {

Param()

New-PSDrive -Name AppData -PSProvider FileSystem -Root $env:Appdata 
New-PSDrive -Name Temp -PSProvider FileSystem -Root $env:TEMP 

$mydocs=Join-Path -Path $env:userprofile -ChildPath Documents
New-PSDrive -Name Docs -PSProvider FileSystem -Root $mydocs

}

New-Drives
DIR temp: | measure-object –property length -sum

October 6, 2014 at 5:40 pm

Hi TJ,

PSDrives and Variables are two different things. PSDrives are essentially always global. I don't have the book in front of me to know what you're referring to when you say "The book gives a function that doesn't work because of a scope issue. The solution that is given to solve it, is to make the scope global when creating the PSDrives", and the attachment would need to be renamed from .ps1 to .txt.