path of exe from Json file and run exe from powershell script

Spunny 366 Reputation points
2022-04-01T21:35:56.75+00:00

Hi,
In powershell script I am calling a service directly from shared network drive and passing variable values to it like this:

\sharenetworkdrive\files\file.exe \anothernetworkdrive\apps\dev\ "" "" "somedata" (this works)

I would like to move both paths to json file and retrieve them in powershell script and run exe. When I do that, it complains that it is not a statement

I did like this:
json file (config.json)
{
"ignoreExtensions": [
".txt",
".log",
".xls",
".dat"
],
"sourcePath": "\\anothernetworkdrive\apps\dev\",
"moveFilesExePath": "\\sharenetworkdrive\files\"
}

test.ps1
$configFile = 'C:\temp\config.json'
$configJSON = Get-Content $configFile

$sourcePath = ($configJSON | ConvertFrom-JSON).sourcePath
#get move files exe path
$moveFilesExePath =  ($configJSON | ConvertFrom-JSON).moveFilesExePath 

$moveFilesExe  = $moveFilesExePath + "File.exe"

Write-Host $moveFilesExe

$moveFilesExe $sourcePath " " " " "MoveFiles"

I am getting unexpected token or expression in statement. How can I run exe by using config json file value.

Thanks

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2022-04-01T23:15:01.953+00:00

    Your sourcepath value only has 3 back slashes, it needs 4.

    {
    "ignoreExtensions": [
        ".txt",
        ".log",
        ".xls",
        ".dat"
    ],
    "sourcePath": "\\\\anothernetworkdrive\\apps\\dev\\",
    "moveFilesExePath": "\\\\sharenetworkdrive\\files\\"
    }
    

    Is this what you want to do?

    cls
    $configFile = 'C:\temp\config.json'
    $configJSON = Get-Content $configFile
    $sourcePath = ($configJSON | ConvertFrom-JSON).sourcePath
    #get move files exe path
    $moveFilesExePath =  ($configJSON | ConvertFrom-JSON).moveFilesExePath 
    $moveFilesExe  = $moveFilesExePath + "File.exe"
    $cmd = '{0} {1} " " " " "Movefiles"' -f $moveFilesExe, $sourcePath
    "Here is the command that I'm going to execute."
    $cmd 
    Invoke-Expression $cmd 
    

  2. Rich Matheisen 47,901 Reputation points
    2022-04-05T19:14:30.5+00:00

    See here: about_quoting_rules
    and here: about_parsing

    I built the exe from the code here: TestExe.cs

    I ran it with different depths of quotes:

    With a depth of 1 (the quotes disappear, which your "file.exe" ignores, I think) even though the spaces are passed a Arg2 and Arg3:
    C:\Users\richm\SourceCode\ConsoleApp1\bin\Debug\ConsoleApp1.exe -echoargs \sharenetworkdrive\files\File.exe \anothernetworkdrive\apps\dev\ " " " " "Movefiles"
    Arg 0 is <\sharenetworkdrive\files\File.exe>
    Arg 1 is <\anothernetworkdrive\apps\dev\>
    Arg 2 is < >
    Arg 3 is < >
    Arg 4 is <Movefiles>

    With a depth of 2 (the values between the quotes are passed, but now there are 4 empty parameters, and the quotes are removed from around the ""Movefiles""):
    C:\Users\richm\SourceCode\ConsoleApp1\bin\Debug\ConsoleApp1.exe -echoargs \sharenetworkdrive\files\File.exe \anothernetworkdrive\apps\dev\ "" "" "" "" ""Movefiles""
    Arg 0 is <\sharenetworkdrive\files\File.exe>
    Arg 1 is <\anothernetworkdrive\apps\dev\>
    Arg 2 is <>
    Arg 3 is <>
    Arg 4 is <>
    Arg 5 is <>
    Arg 6 is <Movefiles>

    With a depth of 4 (now quotes are passes as arguments, but the quotes are properly placed around the 'Movefiles' argument):
    C:\Users\richm\SourceCode\ConsoleApp1\bin\Debug\ConsoleApp1.exe -echoargs \sharenetworkdrive\files\File.exe \anothernetworkdrive\apps\dev\ """" """" """" """" """"Movefiles""""
    Arg 0 is <\sharenetworkdrive\files\File.exe>
    Arg 1 is <\anothernetworkdrive\apps\dev\>
    Arg 2 is <">
    Arg 3 is <">
    Arg 4 is <">
    Arg 5 is <">
    Arg 6 is <"Movefiles">

    With a depth of 3 (there are quotes passed around the spaces and the 'movefiles'):
    C:\Users\richm\SourceCode\ConsoleApp1\bin\Debug\ConsoleApp1.exe -echoargs \sharenetworkdrive\files\File.exe \anothernetworkdrive\apps\dev\ """ """ """ """ """Movefiles"""
    Arg 0 is <\sharenetworkdrive\files\File.exe>
    Arg 1 is <\anothernetworkdrive\apps\dev\>
    Arg 2 is <" ">
    Arg 3 is <" ">
    Arg 4 is <"Movefiles">

    So . . . try passing those arguments with a quoting level of 3 and see if that works for you.

    0 comments No comments

  3. MotoX80 36,291 Reputation points
    2022-04-05T20:52:55.493+00:00

    write host says it is passing above line, but it doesn't move files.
    None of the above worked.

    We can't help you based on a description of "it didn't work". We have no way of knowing what is wrong. The 2 most obvious problems are: 1) The parameters passed to the files.exe program are incorrect, or 2) The account that executes the SQL Job does not have access to the network shares that are referenced.

    I asked that you add a PS transcript in the expectation that some error message would be captured. Did it?

    Does the files.exe program write error or successful messages to stdout/stderr? Does it create it's own log file? If you pass obviously bad parameters to the program, does it produce some error output so that you will know that it doesn't like your input? If it doesn't, have the developer fix the program so that it displays error messages.

    Does the account that execute the script have access to the shares? Add Test-Path commands or Get-Childitem statements on any network path to verify that the account has access.

    You need to find the root error. You can't focus on "it didn't work".


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.