我怎样才能让这些在后台静默运行?
这是我的 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>
为避免从计划任务中看到 PowerShell 命令窗口弹出窗口:
SYSTEM
上下文中运行,然后将您的变量硬编码$yourUser
为已知的用户帐户字符串;或者(更危险的是),添加-Recurse
到rm C:\Users
脚本中 (Remove-Item) 行的末尾以影响所有 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 (Remove-Item) 行的末尾添加,以确保永远不会提示您。