Welcome › Forums › General PowerShell Q&A › Strange character error when running native cmdline app
- This topic has 2 replies, 2 voices, and was last updated 5 years, 12 months ago by
Participant.
-
AuthorPosts
-
-
January 22, 2015 at 5:58 am #22057
Howdy,
I’ve been trying to produce just a simple script (pasted at bottom) which will run a native cmdline application (psftp) and which that native app will run using its own command script – a script also created by the powershell script. However I’m having a lot of difficulty with this and I don’t understand why. I even put the native app and args in its own batch script (rsynctemp.bat) and had Powershell run that instead, to no avail. Every time I run the PS script it errors with the following:
PowerShell1234567891011PS C:\Scripts> Download-Log TestC:\Scripts>þpC:\Users\ThisUser\rsynctemp.bat : 'þp' is not recognized as an internal or external command,At line:1 char:1+ C:\Users\ThisUser\rsynctemp.bat+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : NotSpecified: ('þp' is not rec...ternal command,:String) [], RemoteException+ FullyQualifiedErrorId : NativeCommandErroroperable program or batch file.It looks like when it is trying to execute the batch script (or the command by itself) Powershell is feeding it erroneous characters. Unless I missing some here-string gotchas that I’m not aware of, I don’t really see anything particularly wrong with them.
What’s particularly interesting is that I can pop open the Powershell shell and test out the native app by running the rsynctemp.bat straight from the prompt and it’ll work great. It’s only when trying to do it from this script does it bug out.
Script:
PowerShell12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758function Download-Log{[CmdletBinding()][OutputType([int])]Param(# Param1 help description[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,Position=0)][string[]]$Device,[Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true)][string]$Directory="$ENV:USERPROFILE\My Documents\Logs\")Begin{#Filenames for the scripts$TempCmdFile = "$ENV:USERPROFILE\rsync.tmp"$TempBatFile = "$ENV:USERPROFILE\rsynctemp.bat"#DO NOT INDENT HERE-STRING CONTENTS/TERMINATOR#Create command script for psftp$TempCmd = @"lcd "$Directory"mget -r $($Device.ToUpper())"@#Create batch script to run psftp command$TempBat = @"psftp -pw APassword AServer -batch -bc -b $TempCmdFile"@#Slap the scripts into temporary filesOut-File -FilePath $TempCmdFile -Force -InputObject $($TempCmd -join ' ') #replace newlines w/ spacesOut-File -FilePath $TempBatFile -Force -InputObject $TempBat}Process{#$ErrorActionPreference = "SilentlyContinue"Invoke-Expression -Command $TempBatFile#$ErrorActionPreference = "Stop"}End{#Open window to logs directoryexplorer $Directory#Delete temp filesRemove-Item $TempCmdFileRemove-Item $TempBatFile}} -
January 22, 2015 at 6:01 am #22058
-
January 26, 2015 at 2:02 am #22130
Wonderful. I totally did not recognize the Unicode output. Thank you!
-
-
AuthorPosts
- The topic ‘Strange character error when running native cmdline app’ is closed to new replies.
Pingback: PowerShell – PowerShell generated batch file results in error þ is not recognized as an internal or external command | blog.bjornhouben.com