class getVar {
static [string]pdConfigVarGetMethod($var = "") {
$configFilePath = "$Env:PD_SCRIPTS\pdConfigurationFile.ps1"
$configFileContent = Get-Content -Path $configFilePath
foreach ($line in $configFileContent) {
$lineVar = $line -split (':::')
if ($lineVar[0] -eq $var) {
return $lineVar[1]
}
}
return ""
}
}
$path = [getVar]::pdConfigVarGetMethod("PD_SCRIPTS")
Set-Location -Path $path
The above code works. What I want to understand is why this won't work:
Set-Location -Path [getVar]::pdConfigVarGetMethod("PD_SCRIPTS")
I used a class method because I learned that PS function return values have no contract. A class method's return value is loosly typed and can be relied upon to return only what I asked it to return with the proper type.
So why then doesn't the method call resolve into a string value that Set-Content -Path can understand???
I get the error:
Set-Location : A positional parameter cannot be found that accepts argument 'PD_SCRIPTS'.
At C:\Users\Paul.Defina\Documents\PowershellScripts\ScriptDir.ps1:11 char:1
+ Set-Location -Path [getVar]::pdConfigVarGetMethod("PD_SCRIPTS")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-Location], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
I expected:
Set-Location -Path [getVar]::pdConfigVarGetMethod("PD_SCRIPTS")
to work the same as:
$path = [getVar]::pdConfigVarGetMethod("PD_SCRIPTS")
Set-Location -Path $path