سؤال

I am trying to run the following command through powershell:

schtasks /query /s $compname /v /fo csv | ConvertFrom-Csv

$compname is set in the first part of the powershell script, but cannot be passed into the command like this. For example, when I try passing in $localhost, I get the following error:

ERROR: Invalid syntax. Value expected for '/s'.
Type "SCHTASKS /QUERY /?" for usage.

Is there a way to pass the variable into this command? I don't plan to change to powershell's native way to access scheduled tasks (through a COM object).

هل كانت مفيدة؟

المحلول

Working for me, tested in v2. Does it help if you enclose the variable in double quotes (e.g "$compname") ?

$compname = 'localhost'
schtasks /query /s $compname /v /fo csv | ConvertFrom-Csv

نصائح أخرى

Try this:

Invoke-Expression -Command "schtasks /query /s $compname /v /fo csv" | ConvertFrom-Csv
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top