Reputation: 33
How can I convert this cmd to powershell
cd C:\Program Files (x86)\Image Instruments\abcClient
“XYZ.exe” -PatId=”11111” -Firstname=“John” -Lastname=“Do” -Birthday=”19971101” -Sex=“M” -City=“Frankfurt”
I tried something like this, but does not work. It opens the .exe without arguments:
$pathExe = Set-Location “C:\Program Files (x86)\Image Instruments\abcClient”
$openExe = Start-Process “XYZ.exe”
$PatId = ”11111”
$Firstname = “John”
$Lastname = “Doe”
$Birthday = ”19971101”
$Sex = “M”
$City = “Frankfurt”
$pathExe; & $openExe@('-PatID='+$PatId, '-Firstname=’+$Firstname, '-Lastname='+$Lastname, ’-Birthday=’+$Birthday, ‘-Sex=’+$Sex, '-City='+$City)
As I have no experience with programming, how I could bring this to work?
Thank you for any advice
Upvotes: 3
Views: 10831
Reputation: 171
Get / Convert / Access / Translate Cmd COMMAND Variables in PowerShell:
You may want to try the following which I have 'found' works in a single PC environment:
#- Set up Cmd Variable -----
$zCmdVariable = "%UserProfile%"
#- Get Contents from Cmd -----
$zConvertedVariable = (cmd /c "echo $zCmdVariable")
#- Use contents in Powershell -----
Get-Item -Path $zConvertedVariable
I hope this helps; Pass It forward.
Upvotes: 0
Reputation: 437062
Just like cmd, PowerShell is a shell that supports invoking programs directly, with arguments.
Therefore, your command looks very similar in PowerShell:
Set-Location "C:\Program Files (x86)\Image Instruments\abcClient"
XYZ.exe -PatId="11111" -Firstname="John" -Lastname="Do" -Birthday="19971101" -Sex="M" -City="Frankfurt"
cd
in PowerShell is Set-Location
(although cd
works too, because it is defined as an alias for it).Note the need to quote, the target directory passed to Set-Location
, given that its path contains spaces.
Unquoted use of XYZ.exe
allows its direct invocation; if you quote the program name (e.g., as "XYZ.exe"
), you would have to prepend &
, the call operator.
If you want to use variables in your arguments, simply use them in lieu of the literal strings; e.g. -PatId="11111"
can be replaced with -PatId="$PatId"
, assuming that you've defined $PatId = "11111"
beforehand.
Though they do not come into play here, there are differences between cmd and PowerShell with respect to what characters need quoting when you pass arguments and how special characters are escaped - see Get-Help about_parsing
If XYZ.exe
is a console application (which appears to be the case), PowerShell will execute it synchronously - that is, PowerShell will wait until the application has exited and will reflect its exit code in automatic variable $LASTEXITCODE
.
Upvotes: 2
Reputation: 7465
So, the ProcessStartInfo object defines parameters to pass into a process:
FileName - This is what you'll be executing
WorkingDirectory - The execution path that it will be running from (not always the same as the file's directory but often is). Important if you have things like relative paths
Arguments - This is what is getting passed as the process arguments.
$startInfo = New-Object System.Diagnostics.ProcessStartInfo
$startInfo.FileName = "C:\Program Files (x86)\Image Instruments\abcClient\XYZ.exe"
$startInfo.WorkingDirectory = "C:\Program Files (x86)\Image Instruments\abcClient\"
$startInfo.Arguments = "-PatId=11111 -Firstname=John -Lastname=Do -Birthday=19971101 -Sex=M -City=Frankfurt"
Then you start a process object, and wait for it to complete.
$process = New-Object System.Diagnostics.Process
$process.StartInfo = $startInfo
$process.Start()
$process.WaitForExit()
Then get it's return code (usually EXEs with a 0 mean success).
$exitCode = $process.ExitCode
Upvotes: -1