1

I'm trying to run an antivirus scan via PowerShell as a background process.

My code:

$arg1 = "/c"
$arg2 = "/ScanAllDrives"
$logFile = "/LOGFILE='C:\Users\user\AppData\Local\Symantec\Symantec Endpoint Protection\Logs\test.log'"

Start-Job -ScriptBlock {"C:\Program Files (x86)\Symantec\Symantec Endpoint Protection\12.1.7004.6500.105\Bin\DoScan.exe"} -ArgumentList "$arg1 $arg2 $logFile

The job runs for a second then stops. Get-Job shows it has completed, but it has not given the runtime and lack of log file.

It works fine when I run it from the PowerShell console as follows:

& "C:\Program Files (x86)\Symantec\Symantec Endpoint Protection\12.1.7004.6500.105\Bin\DoScan.exe" /c /ScanAllDrives

Any idea why this won't run in the background? I've tried adding the args directly into the scriptblock but it doesn't seem to like that at all. I'm not finding it easy to see why it completes as the background job doesn't produce any output.

Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
user1894814
  • 389
  • 1
  • 3
  • 12
  • Ok think I've gotten a little closer. When I use Receive-job I see: The term 'x86' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again – user1894814 Apr 18 '17 at 05:53
  • Thanks, but now the arguments don't work – user1894814 Apr 18 '17 at 06:33
  • You pass arguments to the job, but never refer to them in the script block. – Matthew Wetmore Apr 18 '17 at 06:44
  • You need the `&` in the ScriptBlock. Following works for me `Start-Job -ScriptBlock { & "notepad.exe" -Arguments "test.txt"}` – Lieven Keersmaekers Apr 18 '17 at 06:46
  • I use `Start-Process` for this kind of tasks, this allows to directly use file path instead of scriptblock wrapping. This might be better suited for your task. – Vesper Apr 18 '17 at 07:00

1 Answers1

1

Per your comment, when investigating the result of a PowerShell job, use the Receive-Job cmdlet with the ID of the job to see the resultant output. This will likely help you troubleshoot further.

I think the following revised code will work, but I don't have SEP installed locally so can't perform a complete test (but it did work with a substitute .exe):

$arg1 = '/c'
$arg2 = '/ScanAllDrives'
$logFile = '/LOGFILE="C:\Users\user\AppData\Local\Symantec\Symantec Endpoint Protection\Logs\test.log"'

Start-Job -ScriptBlock {& "C:\Program Files (x86)\Symantec\Symantec Endpoint Protection\12.1.7004.6500.105\Bin\DoScan.exe" $Args[0] $Args[1] $Args[2]} -ArgumentList $arg1, $arg2, $logFile

Explanation:

  • The path to the executable needs to wrapped in quotes as it contains spaces.
  • The variables passed to -ArgumentList need to be referenced within the scriptblock via an automatic array variable called $Args.
  • The variables you pass to -ArgumentList need to separated by commas.
Mark Wragg
  • 22,105
  • 7
  • 39
  • 68