我有一个Windows服务在其中一个Azure VM上运行,
因此无论何时必须完成部署,我们都会手动复制二进制文件。所以现在,我正在编写一个脚本来做到这一点。
从本质上讲,二进制文件在MachineA
中采用 zip 文件夹的形式。该 zip 文件夹将复制到MachineB
(Windows 服务正在运行的位置(。复制后,提取文件,然后删除zip文件夹。然后在服务启动后。
为此,我有以下脚本。
#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"}
当我在MachineA
中打开 Windows powershell 并逐个执行这些命令时,这工作正常。
但是当我将完全相同的命令放入 ps1 文件中并执行该文件时,我收到以下错误:
At C:ScriptTesttest.ps1:13 char:95
+ ... -ScriptBlock {Start-Service -Name "ServiceName"}
+ ~~
The string is missing the terminator: ".
At C:ScriptTesttest.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
我在哪里错过了这个终结者。我无法弄清楚。任何帮助都非常感谢。
事实证明,其中一个命令中的 - 是错误的。
我已经替换了这条线
Invoke-Command -Session $s -ScriptBlock {param($zipFilePath)Remove-Item –path $zipFilePath}-ArgumentList $zipFilePath
用这条线
Invoke-Command -Session $s -ScriptBlock {param($zipFilePath)Remove-Item -path $zipFilePath}-ArgumentList $zipFilePath
路径中的连字符略有不同。我能够从这个答案中弄清楚