0

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

Dalebert
  • 35
  • 4
  • 3
    The `.ToUpper()` call is not the problem, the fact that `$Server` is `$null` is the problem. As to *why* `$Server` is `$null`, that's not possible to tell from the line where it fails. If `$Server` is initially assigned from user input and the user's input is not empty, there should be some other assignment inbetween that inadvertently clears it. Try putting `Set-StrictMode -Version 5` (or whatever your version is) at the top to catch mistakes with non-existent property accesses. – Jeroen Mostert May 21 '21 at 18:00
  • What @JeroenMostert said is on point. I though am curious why you would need to convert the ComputerName argument to upper in the first place. Computer names are not case-sensitive and `Get-Printer` should have no trouble using it in whatever case you supply it. – Daniel May 21 '21 at 18:40
  • This is just to output to the screen to that I use for another menu. It only to be used as an identifier so the end user knows which server they are interrogating. Nothing more. – Dalebert May 21 '21 at 19:43
  • I'm not familiar with the ```Set-StrictMode...``` flag. I'll look that up. Thanks for the help, Jeroen and Daniel. – Dalebert May 21 '21 at 19:44
  • You're welcome. It's good to actually really sit and read error message sometimes. Dissect it. `You cannot call a method on a null-valued expression.` The expression is `$Server`, the method is `ToUpper()`. It is saying that you cannot call or use a method on expression that evaluates to null or nothing which makes sense. If $Server is $null then it is not any type of object. $null does not have any methods because it is not any object. Strings have a method called ToUpper and integers do not for example. – Daniel May 21 '21 at 21:40
  • So, I figured the problem out. I was passing the server name to a function, which then presents a menu of options using a SWITCH block. The reason I was confused was that I was writing the server name to the screen to verify the name was being passed, which it was. Where the problem came in was that on one of the options would call another function, which also worked, but returning to the menu options I wasn't passing the server name back. I have that all squared away now -- thanks to your help. – Dalebert May 24 '21 at 12:14

0 Answers0