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.

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?

2
  • 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 Commented Oct 22, 2018 at 6: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. Commented Oct 22, 2018 at 6:32

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I'm unable to do it now.I need to wait for 2 days to do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.