Everyone.
I'm not sure why this is happening, but in my PowerShell script I am converting a string (server name entered by the end user) to uppercase. My trouble is that when I run my script it throws the error "You cannot call a method on a null-valued expression." on the open/close parentheses of the *.ToUpper()
. As far as I know using ToUpper it in this case should just shift the case.
Here's an example:
$Server = $Server.ToUpper()
And the output:
You cannot call a method on a null-valued expression.
At C:\TEMP\Servers.ps1:705 char:2
+ $Server = $Server.ToUpper()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Now, I have tried to just convert to uppercase on whatever action I am calling, for instance:
$GP = Get-printer -ComputerName $Server.ToUpper()
The above also fails on the "()".
But it doesn't seem to be happening all the time. I am converting in numerous places and it's working as expected, but sporadically in the script it's throwing errors.
I've tried $Server = $Server.ToUpper()
and $Server = $Server.ToUpper
-- and I get mixed results of success and errors.
I'm betting I'm missing something small with this. Any advice on how to resolve?
Thanks, Dale