Powershell function with Parameters throwing null exception

Question:

This script is throwing a null exception and I am not certain why that is the case…

the write-host writes the variable. The Get-Service -ComputerName method throws this exception:

I am wondering what they are talking about, Neither is empty…

Neither of those are empty. Why is it throwing that exception?

Answer:

Unlike most languages, PowerShell does not use parenthesis to call a function.

This means three things:

  1. ("DUMMY","VALUES") is actually being interpreted as an array. In other words, you are only giving StopServices one argument instead of the two that it requires.
  2. This array is being assigned to $ServiceName.
  3. Due to the lack of arguments, $Remoteserver is assigned to null.

To fix the problem, you need to call StopServices like this:

Source:

Powershell function with Parameters throwing null exception by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply