Como fazer SCHtASKS / Query exibir o atributo "múltiplas ações" no resultado da consulta?

2

Eu quero exibir todos os detalhes sobre uma determinada tarefa.

Por exemplo, eu tenho um "MyTask" que tem vários programas para executar como ações , mostrados abaixo:

Euqueroveralistadeprogramasqueatarefaexecutanaversãodelinhadecomandodoagendadordetarefas(ou:schtasks).

Então,quandoeucorro:

SCHTASKS/Query/TN"\Microsoft\Windows\MyTask" /FO list /v

Mas só recebo a frase "várias ações" em vez das ações / programas reais a serem executados, conforme mostrado abaixo:

Eu sei que eu poderia usar o argumento /XML para mostrar a lista dos programas a serem executados, mas e se eu tiver muitas tarefas para gerar resultados? Quero que minha saída seja uma lista, por exemplo, /FO list

Como posso resolver isso?

    
por AK_ 08.03.2017 / 21:06

2 respostas

2

Lista de scripts em lote Todos os nomes e comandos de tarefas do Agendador de tarefas

Eu forneci um script em lote abaixo que será executado a partir do Windows 7 e do Windows 10 .

Essentially this will:

  1. Run schtasks with the /XML switch and pipe all the XML content it reads through the findstr command with the I switch (case insensitive) filtering it to only redirect the lines that contain the <!-- and <command> strings as ouput to a flat file.
  2. The flat file content is then run through a dynamic PowerShell script that will replace the XML tags with more appropriately formatted field names, trim any leading white space from all lines, remove all blank line, and lastly put a new line before each of the Task Name fields but it'll skip the first line as it won't need to have a line before it—the topmost line in the file.

Note: See the Gotchas section below for potential anomaly detail and items to note.

O script em lote

@ECHO ON

SET RptFile=%temp%\TaskSchedReport.txt

:: -- This routine sets temp files
SET RptFileTmp=%temp%\~tmpTaskSchedReport.txt
IF EXIST "%RptFileTmp%" DEL /Q /F "%RptFileTmp%"
SET TmpPSScript=%Temp%\~tmpScheduleTasks.ps1
IF EXIST "%TmpPSScript%" DEL /Q /F "%TmpPSScript%"

:SchTask
schtasks /query /XML | Findstr /I "<!-- <command>">"%RptFileTmp%"

:PowerShell
ECHO $origFile = "%RptFileTmp%"                                                    >> "%TmpPSScript%"
ECHO $NewFile = "%RptFile%"                                                        >> "%TmpPSScript%"
ECHO $BlankLine = "'r'n"                                                           >> "%TmpPSScript%"
ECHO (Get-Content $origFile) ^| Foreach-Object {                                   >> "%TmpPSScript%"
ECHO     $_ -replace "<!-- ", 'Task Name (and path): ' -replace "<Command>", 'Command: ' -replace "<[^>]+>", '' -replace '^^\s+', '' -replace '(?m)^^\s*\r?\n', ''>> "%TmpPSScript%"
ECHO     } ^| Set-Content $NewFile                                                 >> "%TmpPSScript%"
ECHO (Get-Content $NewFile) ^| ? {$_.trim() -ne "" } ^| Set-Content $NewFile       >> "%TmpPSScript%"
ECHO (Get-Content $NewFile) ^| Foreach-Object {                                    >> "%TmpPSScript%"
ECHO     $_ -replace "Task Name ", ($BlankLine + "Task Name ") -replace "-->", ''  >> "%TmpPSScript%"
ECHO     } ^| Set-Content $NewFile                                                 >> "%TmpPSScript%"
ECHO (Get-Content $NewFile ^| Select-Object -Skip 1) ^| Set-Content $NewFile       >> "%TmpPSScript%"

SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%"
Powershell -ExecutionPolicy Bypass -Command "& '%TmpPSScript%'"

:: -- Below will open file to view content with the default text editor
explorer.exe "%RptFile%"

Resultados

Task Name (and path): \Adobe Acrobat Update Task 
Command: C:\Program Files\Common Files\Adobe\ARM.0\AdobeARM.exe

Task Name (and path): \GoogleUpdateTaskMachineCore 
Command: C:\Program Files\Google\Update\GoogleUpdate.exe

Task Name (and path): \GoogleUpdateTaskMachineUA 
Command: C:\Program Files\Google\Update\GoogleUpdate.exe

Task Name (and path): \TopSecret 
Command: C:\Folder\CIA.exe
Command: C:\Folder\FBI.exe

Pegadinhas

Se você observar um item de campo Task Name sem nenhum item de campo Command abaixo, isso parece ser devido a tarefas agendadas do sistema etc. que têm Actions listado como Custom Handler valores que não podem ser editados. exemplos e captura de tela abaixo.

Exemplo (sem comandos)

Task Name (and path): \Microsoft\Windows\Shell\WindowsParentalControls 

Task Name (and path): \Microsoft\Windows\Shell\WindowsParentalControlsMigration 

Task Name (and path): \Microsoft\Windows\SideShow\AutoWake 

Task Name (and path): \Microsoft\Windows\SideShow\GadgetManager 

Task Name (and path): \Microsoft\Windows\SideShow\SessionAgent 

Task Name (and path): \Microsoft\Windows\SideShow\SystemDataProviders 

Configurações do Job Scheduler Job (sem comandos / ações)

Maisrecursos

por 11.03.2017 / 04:51
1

Como você diz: " eu ainda quero ver os nomes das tarefas em relação ao que suas ações são " no comentário, então você pode usar o exemplo de script abaixo para obter a saída esperada com Windows 7 .

Essentially this will:

  1. Run the verbose schtasks query command to dump the output to a csv file
  2. With the Windows native PowerShell convertfrom-csv command, it'll convert the csv output to a list like format.
  3. From there it'll use the PowerShell select command with the -property switch to get only the values from the fields you want to see only.
  4. The PowerShell where command with -notcontains comparison operator will filter out extra TaskName objects that for whatever reason dumps to the csv file with the schtasks commands.

Note: You're more limited with what you can and cannot use natively for this task with Windows 7 than you are with newer Windows OSes such as Windows 10.

Script em lote explícito

Você pode precisar jogar com o parâmetro -Width 256 e usar maior ou menor int.

@ECHO ON

SET RptFile=C:\Folder\Path\TaskSchedReport.txt
SET TmpPSScript=%Temp%\~tmpScheduleTasks.ps1
IF EXIST "%TmpPSScript%" DEL /Q /F "%TmpPSScript%"

ECHO schtasks /query /v /fo csv ^| ConvertFrom-CSV ^| >> "%TmpPSScript%"
ECHO Select -Property "TaskName","Task To Run" ^|     >> "%TmpPSScript%"
ECHO Where {$_.TaskName -notcontains "TaskName"} ^|   >> "%TmpPSScript%"
ECHO Out-File "%RptFile%" -Width 256                  >> "%TmpPSScript%"

SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%"
Powershell -ExecutionPolicy Bypass -Command "& '%TmpPSScript%'"

:: -- Below will open file to view content with the default text editor
explorer.exe "%RptFile%"

EXIT

Snippet de Lote de Saída

TaskName                                                                                                                         Task To Run                                                                                                                    
--------                                                                                                                         -----------                                                                                                                    
\Adobe Acrobat Update Task                                                                                                       C:\Program Files\Common Files\Adobe\ARM.0\AdobeARM.exe                                                                       
\Adobe Acrobat Update Task                                                                                                       C:\Program Files\Common Files\Adobe\ARM.0\AdobeARM.exe                                                                       
\GoogleUpdateTaskMachineCore                                                                                                     C:\Program Files\Google\Update\GoogleUpdate.exe /c                                                                             
\GoogleUpdateTaskMachineCore                                                                                                     C:\Program Files\Google\Update\GoogleUpdate.exe /c                                                                             
\GoogleUpdateTaskMachineUA                                                                                                       C:\Program Files\Google\Update\GoogleUpdate.exe /ua /installsource scheduler                                                   
\Microsoft\Microsoft Antimalware\Microsoft Antimalware Scheduled Scan                                                            c:\Program Files\Microsoft Security Client\MpCmdRun.exe Scan -ScheduleJob -RestrictPrivileges                                 
\Microsoft\Windows\Active Directory Rights Management Services Client\AD RMS Rights Policy Template Management (Automated)       COM handler                                                                                                                    
\Microsoft\Windows\Active Directory Rights Management Services Client\AD RMS Rights Policy Template Management (Automated)       COM handler                                                                                                                    
\Microsoft\Windows\Active Directory Rights Management Services Client\AD RMS Rights Policy Template Management (Manual)          COM handler                                                                                                                    
\Microsoft\Windows\Autochk\Proxy                                                                                                 %windir%\system32\rundll32.exe /d acproxy.dll,PerformAutochkOperations    

Script em lote tudo

@ECHO ON

SET RptFile=C:\Folder\Path\TaskSchedReport.txt
SET TmpPSScript=%Temp%\~tmpScheduleTasks.ps1
IF EXIST "%TmpPSScript%" DEL /Q /F "%TmpPSScript%"

ECHO schtasks /query /v /fo csv ^| ConvertFrom-CSV ^| >> "%TmpPSScript%"
ECHO Where {$_.TaskName -notcontains "TaskName"} ^|   >> "%TmpPSScript%"
ECHO Out-File "%RptFile%" -Width 256                  >> "%TmpPSScript%"

SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%"
Powershell -ExecutionPolicy Bypass -Command "& '%TmpPSScript%'"

:: -- Below will open file to view content with the default text editor
explorer.exe "%RptFile%"

EXIT

PowerShell nativo explícito

Como bônus, incluí a sintaxe direta do PowerShell que você pode usar para ver os resultados. Teste com e sem sem o | FL e compare os resultados.

schtasks /query /v /fo csv | ConvertFrom-CSV |
Select -Property "TaskName","Task To Run" |
Where {$_.TaskName -notcontains "TaskName"} | FL

PowerShell nativo tudo

schtasks /query /v /fo csv | ConvertFrom-CSV |
Where {$_.TaskName -notcontains "TaskName"} | FL

Mais recursos

por 09.03.2017 / 01:37