I distilled this from a larger script. The problem I'm having is that the out-file doesn't work to save "sdfsdfad" to a file and instead prints to the console "sdfsdfad".
$ErrorActionPrefence = "Stop"
$s1=@'
for($i=0; $i -lt 10; $i++) {
write-host "sdfsdfad"
}
'@
$s1 > $env:userprofile\Documents\s1.ps1
$sb = {
$ErrorActionPreference = "Stop"
Set-PSDebug -Trace 0
$alog = "$env:userprofile\Documents\a2.txt"
$null > $alog
invoke-expression $env:userprofile\Documents\s1.ps1 | out-file -Append $alog
}
start-job -ScriptBlock $sb | Receive-job -wait
Get-Content $env:userprofile\Documents\a2.txt
Console Output that is suppose to be in "~\Documents\a2.txt" instead:
sdfsdfad
sdfsdfad
sdfsdfad
sdfsdfad
sdfsdfad
sdfsdfad
sdfsdfad
sdfsdfad
sdfsdfad
sdfsdfad