Share via


Powershell Script error: "Missing expression after unary operator '-'.

Question

Saturday, June 16, 2012 10:52 PM

Hello Community

    I created an Install-Sharepoint.ps1 based on Microsofts example and it works.

    Next I created a New-SharePointFarm.ps1 based on Microsofts example but it
fails  as soon as it gets to around line 9 which is "-DatabaseAccessAccount ...".  So I commented out that line to see if the next line would run which is "-DatabaseServer ...".   That means even though it does just what the Microsoft example does around Line 9 the script fails with the same error message which is:

"Missing expression after unary operator '-'.
c:\SPModule.setup\New-SharePointFarm.ps1 :11 char:6
 -<<<< DatabaseAccessAccount (Get-credential <username>\password>
    + CategoryInfo                      :ParseError: (-String) [], ParseException
    + FullyQualifiedErrorID           :MissingExpressionAfterOperator"

    Below is the abbreviated script why  does it fail at around line 9 ?
  
    1 - Write-Host “Loading SharePoint 2010 PowerShell cmdlets”
    2 - Add-PsSnapin Microsoft.SharePoint.PowerShell

    3 - $username = "<username>"
    4 - $password = "xxxxxxxx"
    5 - $DAACred = New-Object -typename System.Management.Automation.PSCredential-argumentlist $username, $password
 
    6 - $CtlPswd = ConvertTo-SecureString "xxxxxxxx" -AsPlainText -Force
    7 - $CtlCred = New-Object System.Management.Automation.PSCredential("<username>", $CtlPswd)
8 - New-SharePointFarm
9 -     -DatabaseAccessAccount (get-credential "<username>\password)
    -DatabaseServer "<servername>\Server1"
    -DatabaseName "Sharepoint_ConfigDb
    -AdministrationContentDatabase "WSS_ConfigContent"
    -Port 10000 -AdminAuthMethod  NTLM -Paraphrase "xxxxxxxx"
    -FarmCredentials -Credential $CtlCred
 
    -FarmName TestFarm

    Install-SPHelpCollection -All
    Initialize-SPResourceSecurity
    Install-SPService
    Install-SPFeature -AllExistingFeatures
    New-SPCentralAdministration -Port 10001 -WindowsAuthProvider "NTLM"
    Install-SPApplicationContent
 
    param
    (   
        [Parameter(Mandatory=$true)][ValidateNotNull()]
        [System.Management.Automation.PSCredential]$DatabaseAccessAccount,
       
        [Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()]
        [String]$DatabaseServer,
       
        [Parameter(Mandatory=$false)][ValidateScript({$_.Length -ne 0})]
        [System.Security.SecureString]$Passphrase,
       
        [Parameter(Mandatory=$false)][ValidateRange(1024, 65535)]
        [int]$Port = "10000",
       
        [Parameter(Mandatory=$false)][ValidateSet("NTLM", "Kerberos")]
        [String]$AdminAuthMethod = "NTLM",
       
        [Parameter(Mandatory=$false)][ValidateNotNullOrEmpty()]
        [String]$FarmName = $env:COMPUTERNAME
    )

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue" | Out-Null

    etc. etc. etc.

    If the above portion of the script shows enough to explain, why does the script
produce this error?

    Thank you
    Shabeaut

All replies (5)

Sunday, June 17, 2012 2:18 AM ✅Answered

I see a couple of potential issues:

  1. line 9, along with the immediately following lines, seems to be a continuation of line 8. if so, the preceding line should end with a backtick.
  2. there seems to be an unmatched double quote in line 9.

Al Dunbar


Sunday, June 17, 2012 6:09 AM ✅Answered

Line 5:

$DAACred = New-Object -typename System.Management.Automation.PSCredential-argumentlist $username, $password

There should be a space before -argumentlist

Grant Ward, a.k.a. Bigteddy


Monday, June 18, 2012 1:10 AM ✅Answered | 1 vote

the -credential argument of the get-credential cmdlet only needs quoting if it contains a blank character. These are all equivalent and all equally valid:

   $A = Get-Credential  domain\billy
   $A = Get-Credential  'domain\billy'
   $A = Get-Credential  "domain\billy"

But note that get-credential does not accept a password in its -credential parameter. The password is entered by the user when the get-credential command is executed.

Al Dunbar


Sunday, June 17, 2012 9:21 PM

Hello Al Dunbar

    The code that I included in this post was a sample of the actual code.  From
now on I will cut and past the code from the actual script.

    But you comments were very interesting when I think about the actual code.

    In the actual code line 9 is a parameter to line 8 and so are the continuing
lines as you say. 

    Being new to Powershell I didn't know that you had to let Powershell know
that the next line needed to be continued so for each parameter I will now use
the "backtick" as you say.

    The other thing is I didn't know whether the argument after the (Get-Credential)
parameter had to be in double quotes or not.  What I was trying to do was pass
the username and password to Get-Credential.

    I will check the actual code and make sure there is a space before the
argumentlist.

    Thank you
    Shabeaut


Sunday, June 17, 2012 9:21 PM

Hello Bigteddy

    The code that I included in this post was a sample of the actual code.  From
now on I will cut and past the code from the actual script.

    But you comments were very interesting when I think about the actual code.

    In the actual code line 9 is a parameter to line 8 and so are the continuing
lines as you say. 

    Being new to Powershell I didn't know that you had to let Powershell know
that the next line needed to be continued so for each parameter I will now use
the "backtick" as you say.

    The other thing is I didn't know whether the argument after the (Get-Credential)
parameter had to be in double quotes or not.  What I was trying to do was pass
the username and password to Get-Credential.

    I will check the actual code and make sure there is a space before the
argumentlist.

    Thank you
    Shabeaut