powershellpowershell-2.0

Unable to execute some commands in ps1 file from PowerShell


I have a Windows service running on one of the Azure VMs. So whenever a deployment has to be done, we copy the binaries manually. So now, I'm writing a script to do that.

Basically the binaries are in the form of a zip folder in MachineA. That zip folder is copied to MachineB (where windows service is running). After copying, the files are extracted and then zip folder is deleted. Then after the service is started.

To do this I have the below script:

#get session details
$UserName = "$IPAddress\$adminUsername"
$Password = ConvertTo-SecureString $adminPassword -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($UserName, $Password)
$s = New-PSSession -ComputerName $IPAddress -Credential $psCred

#stop the service
Invoke-Command -Session $s -ScriptBlock {Stop-Service -Name "ServiceName" -Force} 

#delete existing binaries in destination machine
$tempDestPath = $destinationPath  + "\*"
Invoke-Command -Session $s -ScriptBlock {param($tempDestPath)Remove-Item $tempDestPath -Recurse} -ArgumentList $tempDestPath

#copy binaries zip folder in destination machine
Copy-Item -Path $sourcePath -Destination $destinationPath -ToSession $s -Recurse

#extract zipfolder in destination machine
$zipFilePath = $destinationPath + "\" + $fileName
Invoke-Command -Session $s -ScriptBlock {param($zipFilePath,$destinationPath) Expand-Archive $zipFilePath -DestinationPath $destinationPath}-ArgumentList $zipFilePath,$destinationPath

#delete zipfolder in destination machine after extraction
Invoke-Command -Session $s -ScriptBlock {param($zipFilePath)Remove-Item –path $zipFilePath}-ArgumentList $zipFilePath

#start the service
Invoke-Command -Session $s -ScriptBlock {Start-Service -Name "ServiceName"} 

This is working fine when I open Windows PowerShell in MachineA and execute these commands one by one. But when I put the exact same commands in a ps1 file and execute that file, I'm getting the below error:

At C:\ScriptTest\test.ps1:13 char:95
+ ...  -ScriptBlock {Start-Service -Name "ServiceName"}
+                                                                        ~~
The string is missing the terminator: ".
At C:\ScriptTest\test.ps1:11 char:42
+     Invoke-Command -Session $s -ScriptBlock {param($zipFilePath)Remov ...
+                                             ~
Missing closing '}' in statement block or type definition.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

Where am I missing this terminator?


Solution

  • Turns out a - in one of the commands is wrong.
    I have replaced this line

    Invoke-Command -Session $s -ScriptBlock {param($zipFilePath)Remove-Item –path $zipFilePath}-ArgumentList $zipFilePath
    

    with this line

    Invoke-Command -Session $s -ScriptBlock {param($zipFilePath)Remove-Item -path $zipFilePath}-ArgumentList $zipFilePath
    

    The hyphen in from of the path is slightly different.I was able to figure out from this answer