2
votes

I have two PowerShell scripts. One is for uninstalling and installing a SharePoint 2010 solution on the SharePoint server. The other calls this script with two commands, one for the uninstall and one for the install. These are not scripts I wrote, but I have inherited them.

Here is the script that calls the install / uninstall script (I've removed some parameters to simplify for troubleshooting):

& 'C:\Users\username\Documents\setup.ps1' -InstallOrUninstall '/UNINSTALL'
& 'C:\Users\username\Documents\setup.ps1' -InstallOrUninstall '/INSTALL'

Stripped down to almost nothing for the purpose of testing, here is the "setup.ps1" script:

param ($InstallOrUninstall, 
$SiteURL, 
$WebURL, 
[switch]$ignoreFeatures, 
[switch]$thisAppDomain )

if (-not $thisAppDomain)
{
     Write-Host "Invoking script in a new app domain"  -foregroundcolor yellow
     Write-Host $MyInvocation.Line
     powershell.exe -Version 2 -Command $MyInvocation.Line -thisAppDomain
     return;
}

Write-Host "In Body"
Write-Host $MyInvocation.Line

Running the first script returns an error from the first command, but not from the second. The error is:

powershell.exe : - : Missing expression after unary operator '-'.
At C:\Users\username\Documents\setup.ps1:11 char:6
+      powershell.exe -Version 2 -Command $MyInvocation.Line -thisAppDo ...
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (- : Missing exp...y operator '-'.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
    + CategoryInfo          : ParserError: (-:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingExpressionAfterOperator

I believe the reason the install / uninstall script is resending the command for "Version 2" is because of an issue with using PowerShell past Version 2 with SharePoint 2010 (as outlined here). However, I don't understand why only the first line fails. The second command also enters the if statement, but does not error.

If I remove the second line, and only call the setup.ps1 one time, the script that calls the install / uninstall script succeeds.

2

2 Answers

1
votes

A nice little brain-teaser. Apparently, $MyInvocation.Line contains the full line, including the newline / linebreak at the end. So, -thisAppDomain is not interpreted as a parameter, but the beginning of a new expression starting with -. This is also, why it works if you remove the 2nd line, because then you do not have a line break at the end.

To reproduce this error, try:

powershell.exe -Version 2 -Command "`r`n-thisAppDomain"

Note that in newer versions the parsing algorithm was obviousy modified and the error message is different. Omit the -Version 2 switch and you likely get:

The term "-thisAppDomain" is not recognized as the name of a cmdlet, function, script file, or operable program...

One simple way to resolve this would be to .Trim() (or .TrimEnd()) the command:

powershell.exe -Version 2 -Command $MyInvocation.Line.Trim() -thisAppDomain

I need to add though, that you should reconsider what your actual problem is, and if your solution is actually the best way to solve it. Have a look at jobs for example.

0
votes

As marsze said: It is not interpreted as a parameter, but the beginning of a new expression starting with -. This is also, why it works if you remove the 2nd line, because then you do not have a line break at the end.