0

I'm building out a powershell script that does quite a bit of work extracting csv's from a zip, converting OS->lat/lon, stuffing the contents robustly into a DB, and then emailing a distribution list with stats on the whole process.

Most of this is now complete, but to make the whole thing a little more portable I'm providing paths to input/working/output folders as parameters of the powershell call from a batch file.

This is all working fantatstically until I need to call python scripts to do the lat/lon work, as passing in the variable parameter paths doesn't seem to work with any permutation/combination.

The following is a simplified version of the python path within the .PS1 script which works from both command prompt and from within the .PS1 file if called directly (where -i -o are input/output path parameters).

c:\python27\python.exe D:\PythonPPC\subs.py -i D:\PPC\subs_export.csv -o D:\PPC\subs_export_lat_lon.csv

In my script I would like to replace the two path parameters -i/-o with variables something like:

c:\python27\python.exe D:\PythonPPC\subs.py -i $inputPathsubs_export.csv -o $outputPathsubs_export_lat_lon.csv

Does anyone have any idea on how to invoke this command as I've tried the &$exe method described on stack and a few other places, but this simply results in the error shown below:

CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

Any help would be greatly appreciated.

2 Answers 2

3

I'd recommend using the Join-Path cmdlet for building paths. It will save you the headache of keeping track of leading/trailing path separators and also canonicalize / to \.

$workingFolder = 'C:\some/where\'
$extractFolder = '\extract\folder'
$infileName    = '/subs_export.csv'
$outfileName   = '\subs_export_lat_lon.csv'

$infile  = Join-Path (Join-Path $workingFolder $extractFolder) $infileName
$outfile = Join-Path (Join-Path $workingFolder $extractFolder) $outfileName

Result:

PS C:\> $infile
C:\some\where\extract\folder\subs_export.csv
PS C:\> $outfile
C:\some\where\extract\folder\subs_export_lat_lon.csv

If you still want to simply concatenate strings to a path, you can separate variables from trailing strings by putting the variable name between curly brackets. In your example:

python.exe D:\PythonPPC\subs.py -i "${inputPath}subs_export.csv" ...

or

python.exe D:\PythonPPC\subs.py -i "${rootFolder}${subFolder}subs_export.csv" ...
Sign up to request clarification or add additional context in comments.

1 Comment

Cheers for the comment @Ansgar. The script has been getting quite messy with paths concatenated all over the place so I may go back and use this. Thanks!
0

After attacking the problem from a different direction and a colleague pointing out my own abject stupidity, it seems the following is a fairly straightforward solution to the complicated problem above.

$inputFilePath = $pathToWorkingFolder + $pathToExtractFolder + 'subs_export.csv'
$outputFilePath = $pathToWorkingFolder + $pathToExtractFolder +'subs_export_lat_lon.csv'    
c:\python27\python.exe D:\PythonPPC\subs.py -i $inputFilePath -o $outputFilePath

Just needed some peer review I guess...

2 Comments

Didn't see the context of the script. But one problem I can see is that the $inputPathsubs_export.csv will be taken as a variable with name "$inputPathsubs_export.csv" which doesn't exist at all. You might add one back slash to make it $inputPath\subs_export.csv.
Use the Join-Path cmdlet to create your paths. Don't concatenate by hand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.