无法从 powershell 执行 ps1 文件中的某些命令
Unable to execute some commands in ps1 file from powershell
我在其中一个 Azure VM 上有一个 windows 服务 运行。
因此,每当必须完成部署时,我们都会手动复制二进制文件。所以现在,我正在编写一个脚本来做到这一点。
基本上,二进制文件的形式是 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
我哪里漏了这个终结符。我想不通。非常感谢任何帮助。
原来其中一个命令中的 - 是错误的。
我已经替换了这一行
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
路径的from中的连字符略有different.I可以从this答案中找出
我在其中一个 Azure VM 上有一个 windows 服务 运行。
因此,每当必须完成部署时,我们都会手动复制二进制文件。所以现在,我正在编写一个脚本来做到这一点。
基本上,二进制文件的形式是 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
我哪里漏了这个终结符。我想不通。非常感谢任何帮助。
原来其中一个命令中的 - 是错误的。
我已经替换了这一行
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
路径的from中的连字符略有different.I可以从this答案中找出