问题描述:

I have trialping.ps1 with below code

param([string]$ip)start-job -ArgumentList $ip -scriptblock {param([string]$ip)& C:\users\pubic\desktop\PingMachine.ps1 -ip $IP}

Above code creates a job which runs pingmachine.ps1 which basically pings a machine every 10 sec or so and writes to output.txt in some folder.

If

1)& ".\trialping.ps1" -ip x.x.x.x is used everything works fine

2)powershell .\trialping.ps1 -ip "x.x.x.x" is used output file is not created.

Why the difference. Thanks in advance

网友答案:

In first case:

  1. You start script .\trialping.ps1.
  2. That script run Start-Job cmdlet.
  3. Start-Job cmdlet create job object and return it.
  4. Script ends.
  5. Any active job continue to run in background.

In second case:

  1. You start new copy of PowerShell.
  2. That PowerShell copy run script .\trialping.ps1.
  3. That script run Start-Job cmdlet.
  4. Start-Job cmdlet create job object and return it.
  5. Script ends.
  6. PowerShell have nothing more to do and -NoExit not provided, so PowerShell close itself and cancel any active jobs in process.
相关阅读:
Top