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.