Wednesday, July 18, 2012

Powershell error: Cannot overwrite variable Host

While I was trying to run a new Powershell script today, I encountered a new error which I didn't expect but which wasn't difficult to solve. I thought that other people might overlook the same thing as I, so I decided to share it here.

The error I got was the following:

Cannot overwrite variable host because it is read-only or constant.
At line:1 char:1
+  <<<< ./remove-share.ps1
    + CategoryInfo          : WriteError: (host:String) [], SessionStateUnauth
    + FullyQualifiedErrorId : VariableNotWritable

After a few seconds trying to focus on the final lines of this error message, the first line drew my attention. I was in fact trying to use the automatic variable $host to store a value as if it was normal variable. Which is not the case.

The solution to this problem consisted in changing the name of this variable to something else, something not reserved by the system I daresay, such as $host_name.

You can have a look here for more information on $host.

1 comment:

Related Posts Plugin for WordPress, Blogger...