I’m trying to write a script in PowerShell that opens a connection to a remote host with plink but fails to execute a command when a path with spaces is specified.
First I declared a variable with the path of the executable
$plinkPath = "C:Program FilesPuTTYplink.exe"
then another variable with the hostname
$remoteHost = "whatever"
then I have another variable which I think is causing me troubles
$remotePath = 'del "C:Test foldertest.txt"
then I try to connect to the remote host
$credential = Get-Credential
$remoteUser = $credential.UserName
$remotePassword = $credential.GetNetworkCredential().Password
and this is where the script fails
$plinkCommand = "& '$plinkPath' $remoteUser@$remoteHost -pw $remotePassword -P 22 $remotePath"
$result = Invoke-Expression $plinkCommand
Write-Output $result
This is the text returned:
-- Keyboard-interactive authentication prompts
-- End of keyboard-interactive prompts from ser
The system cannot find the file specified.
Access granted. Press Return to begin session.
I don’t think there’s an issue related with the user because in other paths it works, it gives problem only when a path with spaces is specified.
I expect the script to work and execute the command in every occasion.
Sandro1K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.