代码之家  ›  专栏  ›  技术社区  ›  CrazyCoder

无法从PowerShell执行PS1文件中的某些命令

  •  0
  • CrazyCoder  · 技术社区  · 6 年前

    我在其中一个Azure虚拟机上运行了一个Windows服务。
    因此,每当必须进行部署时,我们都要手动复制二进制文件。现在,我正在写一个脚本来完成这个任务。
    二进制文件通常是以zip文件夹的形式存在于 MachineA . 该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"} 
    

    当我在中打开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
    

    我在哪里找不到这个终结者?我搞不清楚。任何帮助都非常感谢。

    1 回复  |  直到 6 年前
        1
  •  1
  •   CrazyCoder    6 年前

    结果发现其中一个命令是错误的。
    我换了这条线

    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
    

    路径中的连字符略有不同。我可以从 this 回答

    推荐文章