I have a simple script to return values from a hash table:
param
(
[Parameter(Mandatory = $true)]
[string]$Name
)
function getvalues ($Name) {
$nameList= @{"CFT"=@{"name1"="text1"; "name2"="text2"}}
#return $nameList[$Name]
return ,$nameList
}
$Values = getvalues($Name)
Write-Debug "DEBUG: Name1 = "$Values["name1"]
Write-Debug "DEBUG: Name2 = "$Values["name2"]
When I run it, I get the following error:
Write-Debug : A positional parameter cannot be found that accepts argument '$null'.
At C:\MSFT\add-test.ps1:21 char:2
+ write-Debug "DEBUG: Name1 = "$Values["name1"]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Write-Debug], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.WriteDebugCommand
Write-Debug : A positional parameter cannot be found that accepts argument '$null'.
At C:\MSFT\add-test.ps1:22 char:2
+ write-Debug "DEBUG: Name2 = "$Values["name2"]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Write-Debug], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.WriteDebugCommand
You're terminating your strings and then using the $Values
lookup. Either use a +
or embed it into the string, or use the -f
operator:
write-Debug ("DEBUG: Name1 = " + $Values["name1"])
write-Debug "DEBUG: Name2 = $($Values["name2"])"
write-Debug ("DEBUG: Name3 = {0}" -f $Values["name3"])
Note forms 1 and 3 need parentheses ( )
.
Regarding your comment that there are no more errors and no output:
Are you sure your debug preference is set such that you can see the output? The point of Write-Debug
and Write-Verbose
is that you only see the output when the preference is set as such (and you shouldn't add DEBUG:
in your string, it will be added for you). I suspect Write-Verbose
is more appropriate for what you're doing.
To test quickly whether it will work, you can actually add -Debug
or -Verbose
as appropriate.
So for example:
Write-Verbose "Name2 = $($Values["name2"])" -Verbose