0

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.
Besically 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. I'm not able to figure out. Any help is highly appreciated.

CrazyCoder
  • 2,194
  • 10
  • 44
  • 91
  • the script is missing the PSSession $s. is the script provided all content of your `.ps1`-file? if so: add `New-PSSession` at the beginning; if not: please provide all content from the `ps1`-file – Guenther Schmitz Oct 22 '18 at 06:30
  • @GuentherSchmitz, all the variables , including $s has values in it. I just didn't mention those details in the question. I will add it anyways. – CrazyCoder Oct 22 '18 at 06:32

1 Answers1

1

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

CrazyCoder
  • 2,194
  • 10
  • 44
  • 91