Win11 中的自訂排程任務會導致 PowerShell 和 CMD.exe 彈出視窗 - 如何讓它們靜音/背景任務?

Win11 中的自訂排程任務會導致 PowerShell 和 CMD.exe 彈出視窗 - 如何讓它們靜音/背景任務?

我怎麼才能讓它們在後台默默運行?

這是我的 PowerShell 腳本:

rm C:\*.jpg
rm C:\*.doc
rm C:\*.pptx
rm C:\*.xls

rm C:\Users\*.jpg
rm C:\Users\*.doc
rm C:\Users\*.pptx
rm C:\Users\*.xls

# set var for username
$yourUser = $env:USERNAME 

rm C:\users\$yourUser\*.jpg
rm c:\users\$yourUser\*.doc
rm c:\users\$yourUser\*.pptx
rm c:\users\$yourUser\*.xls

並且,如果需要,這裡是 PS 腳本的 XML(匯出的排程任務):

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2022-06-23T15:11:00.2977928</Date>
    <Author>MyDomain\MyUser</Author>
    <Description>Nuke annoying AV junk files</Description>
    <URI>\PS_Nuke_Annoying_files.ps1</URI>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <Repetition>
        <Interval>PT1H</Interval>
        <StopAtDurationEnd>false</StopAtDurationEnd>
      </Repetition>
      <StartBoundary>2022-06-23T15:08:58</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>S-1-12-1-679272179-1229781794-976310157-3256474971</UserId>
      <LogonType>InteractiveToken</LogonType>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    <UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT1H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>C:\Temp\PS_Nuke_annoying_files.ps1</Command>
    </Exec>
  </Actions>
</Task>

答案1

若要避免從排程任務中看到 PowerShell 指令視窗彈出:

  • 將計劃任務帳戶變更為在SYSTEM上下文中運行,然後將變數硬編碼$yourUser為已知的使用者帳戶字串;或 (更危險),加入到腳本中 (Remove-Item) 行-Recurse的末尾以影響所有 C:\Users 資料夾和子資料夾。rm C:\Users
  • 編輯您的計劃任務行動。改變程式/腳本C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe。改變 新增參數(可選)-NoLogo -NonInteractive -WindowStyle Hidden -File C:\Temp\PS_Nuke_annoying_files.ps1
  • -Confirm:$false -Force -ErrorAction SilentlyContinue(可選)在腳本中每個 rm(刪除項目)行的末尾添加,以確保永遠不會出現提示。

相關內容