3
votes

I tried different ways to escape the space in "Program Files" but this is not working. I receive the following error in Jenkins after this part is executed:

powershell.exe : FileStream was asked to open a device that was not a file. For support for devices like 'com1:' or 'lpt1:', call At C:\web\JenkinsMaster\workspace\XXX@tmp\durable-d3011838\powershellWrapper.ps1:5 char:3 + & powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Fi ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (FileStream was ... 'lpt1:', call :String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError CreateFile, then use the FileStream constructors that take an OS handle as an IntPtr.

  • CategoryInfo : OpenError: (:) [Out-File], NotSupportedException

  • FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand

  • PSComputerName : XXXXX

powershell script: '''
$pass = ConvertTo-SecureString -AsPlainText "XXXX" -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList "XXXX",$pass
$sessionOption = New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck
$session = New-PSSession -ComputerName XXXXXXXX -UseSSL -Credential $cred -SessionOption $sessionOption
Copy-Item $env:WORKSPACE\\* -Destination "C:\\data\\install\\" -Filter *TEST* -Recurse -Force -Verbose -ToSession $session
$filename = $env:JOB_NAME + "_" + $env:BUILD_DISPLAY_NAME + "_wwwroot.7z"
Invoke-Command -Session $session -ScriptBlock {cmd /c "C:\\Program Files\\7-Zip\\7z.exe\\" x C:\\Data\\Install\\$filename -oC:\\data\\install\\test -aoa >NUL}
Remove-PSSession $session
Exit-PSSession
'''

If I change the Invoke-Command to the following, the Program Files directory seems to be resolved correctly, but then the variable $filename is not resolved anymore.

Invoke-Command -Session $session -ScriptBlock {cmd /c \'"C:\\Program Files\\7-Zip\\7z.exe" x C:\\Data\\Install\\$filename -oC:\\data\\install\\test -aoa >NUL'}

powershell.exe : NotSpecified: (:String) [], RemoteException At C:\web\Jenkins\workspace\XXX@tmp\durable-53dbead2\powershellWrapper.ps1:5 char:3 + & powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Fi ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (NotSpecified: (...RemoteException:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError

  • CategoryInfo : NotSpecified: (:String) [], RemoteException
  • FullyQualifiedErrorId : NativeCommandError
  • PSComputerName : XXXXX

ERROR: The system cannot find the file specified.

C:\Data\Install\$filename

System ERROR:

The system cannot find the file specified.

Hopefully you can assist me in this case! The rest of the commands is working fine.

Thanks!

2

2 Answers

2
votes

The 7z.exe path in your first command has an extraneous trailing \, which causes problems:

cmd /c "C:\\Program Files\\7-Zip\\7z.exe\\"  # <- trailing \\ shouldn't be there

In your 2nd command, you're using single quotes around the command passed to cmd /c ('...'), but the contents of '...' strings in PowerShell are treated as literals, which explains why $fileName was not expanded (interpolated);
only double-quoted ("...") strings and, within limits, unquoted command arguments are expanded in PowerShell; e.g., compare the output from Write-Output '$HOME' to the output from Write-Output "$HOME" / Write-Output $HOME.

As iRon mentions, there's no need to involve cmd at all - PowerShell is perfectly capable of executing command-line programs directly, so this should work:

Invoke-Command -Session $session -ScriptBlock { & "C:\\Program Files\\7-Zip\\7z.exe" x C:\\Data\\Install\\$using:filename -oC:\\data\\install\\test -aoa >$null }
  • Due to invoking 7z.exe directly, now there's no outer quoting needed anymore, and $fileName should be expanded.

    • Note, however, that $fileName was replaced with $using:fileName, which is necessary in order for the target session to know about the local $fileName variable - see Get-Help about_Remote_Variables.
  • Since the 7z.exe file path is quoted (of necessity, due to containing spaces), you must use &, the call operator, to invoke it.

  • Since the > redirection is now performed by PowerShell itself, the cmd-style >NUL output suppression was replaced with its PowerShell analog, >$null.

0
votes

I wonder if it necessarily at all to invoke a CMD shell for this. I guess it would be simpler to directly invoke the 7z.exe with its parameters.

Nevertheless, you can build you own script block like this:

[ScriptBlock]::Create('cmd /c "C:\\Program Files\\7-Zip\\7z.exe" x C:\\Data\\Install\\' + $filename + ' -oC:\\data\\install\\test -aoa >NUL')