0

How to apply the if statement based on the command response. Following PowerShell script is working fine. But the .imported file should not create when ever the command java -Dc=xyz response throws an error.

foreach($file in (Get-ChildItem 'C:\ghost\rider'  -Recurse)) {
 if ($file -Like '*.json') {
 $file.FullName
 $importedFilename=$newname = $file.DirectoryName+'\'+$file.BaseName+'.imported'
     if(!(Test-Path $importedFilename)){
         java -Dc=xyz -Dtype=application/json -jar post.jar $file.FullName # After this statement execution I need to apply the if statement based on the response.?
         New-Item $importedFilename # This line should not execute always
     }else{
     'Already imported! '+$file.FullName
     }
 }
}

I am getting success message like below when the line java -Dc=.... executed. After that I am trying to creating a file .imported for tracking purpose.

SimplePostTool version 5.0.0

Posting files to [base] url http://localhost:8983/solr/xads/update using content-type application/json...

POSTing file rockdata.json to [base]

1 files indexed.

COMMITting Solr index changes to http://localhost:8983/solr/xads/update...

Time spent: 0:00:33.925

sridharnetha
  • 2,104
  • 8
  • 35
  • 69
  • 2
    Check the `$LastExitCode` before creating the new file? – Theo Aug 21 '22 at 11:28
  • @mklement0 please note that my questions is purely related to `powershell` script not belongs to the `php`. I think you confused with the PowerShell script `syntax` similar to `php` – sridharnetha Aug 22 '22 at 05:30
  • @sridharnetha, just like your question is a PowerShell question, so is the linked duplicate. Its accepted answer tells you that in order to test if the most recently executed external program - such as `java` in your case - succeeded, you need to test whether `$LASTEXITCODE -eq 0`. What made you bring PHP into the conversation? There is no reference to it in the linked question. – mklement0 Aug 22 '22 at 12:36
  • @mklement0 your liked duplicate is belongs to PHP. My questions belongs to powershell. – sridharnetha Aug 23 '22 at 03:48
  • \The linked duplicate is tagged `powerhell` (among other tags), and it _is_ PowerShell code. – mklement0 Aug 23 '22 at 04:09
  • In the words of the OP: "I am trying to write a powershell script [...]". – mklement0 Aug 23 '22 at 04:10
  • To summarize: The linked duplicate is tagged as a PowerShell question. The question's body talks about the code being PowerShell. The code is verifiably valid PowerShell code. The accepted answer is a PowerShell solution. It also contains the solution to your problem, as also summarized in a previous comment. Other than your repeated claim that is backed up neither by evidence nor arguments, the linked duplicate is entirely unrelated to PHP. Insisting on this claim without paying attention to and engaging with the suggested solution will not solve your problem. – mklement0 Aug 23 '22 at 04:28

0 Answers0