0

So I'm running the following commands:

Write-Host $RxArr[0].SubString(12)
$RxArr[0].SubString(12) | Out-File "Code\RunCode.ps1"
start powershell (".\Code\RunCode | Out-File 'Code\OutCode.log'")
try
{
    . ./DM '47333409' (cat "Code\OutCode.log").replace('`r','<cr>').replace('`n', '<br>') -custom 'Sent Console Log'
}
catch
{
Write-Host "Unable to send Direct Message."
}

My problem is that the code inside the Try Block is giving me "

Cannot find Code\OutCode.log

", then I go to "Code" and I DO find it there. Then I run it AGAIN, and it works. I tried running 2 different codes (2 different codes inside RunCode.ps1) where the result of code 1 is A and result of code 2 is B. When I run it first time, I get

Error OutCode.log not found

. When I run it again I get A. I realized that the code is NOT waiting for Out-File to finish. I tried replacing Out-File with '>' and I still get the same issue. Any way to make this possible? I'm trying to run a script from a string.

Edit: I tried Sleep 1, 2, 3 and all the same, the command is waiting until the sleep ends then it writes to the file.

thethiny
  • 1,125
  • 1
  • 10
  • 26
  • 2
    Your code confuses me a little bit but if I'm not wrong `start` is an alias for `Start-Process` and this cmdlet has the parameter `-Wait` to tell it to wait for the end of the execution of whatever you started. Try this. But I think you don't need that at all. Simply run the script by calling it. – Olaf Jul 22 '18 at 23:36
  • @Olaf Well, that works! Thanks! I need to call the script in a different environment that's why I'm using "Start". I don't wanna interrupt my running sequence. – thethiny Jul 22 '18 at 23:44

0 Answers0