【发布时间】:2018-10-22 14:44:33
【问题描述】:
我有一个 Windows 服务在其中一个 Azure VM 上运行。
因此,每当必须进行部署时,我们都会手动复制二进制文件。所以现在,我正在编写一个脚本来做到这一点。
Besically 二进制文件的形式是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:\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
我在哪里错过了这个终结者。我无法弄清楚。非常感谢任何帮助。
【问题讨论】:
-
脚本缺少 PSSession $s。该脚本是否提供了您的
.ps1-文件的所有内容?如果是:在开头添加New-PSSession;如果不是:请提供ps1-file 中的所有内容 -
@GuentherSchmitz,包括 $s 在内的所有变量都有值。我只是没有在问题中提及这些细节。反正我会加的。