任务计划程序 Powershell 不会写入文件
Task Scheduler Powershell won't write to file
我有一个 powershell 脚本,用于通过计划任务触发和 运行 日志记录。问题是脚本在 运行 时不会以这种方式写入文件。如果我 运行 它通过 cmd 它完美地工作。我已尝试为脚本提供最高权限,并且在 运行 脚本时将日志文件的确切位置作为参数传递。都没有解决问题。请问有人可以帮忙吗?见代码:
Param([string]$inputPath, [string]$logfilelocation)
$i=0
$paths = Get-Content $inputPath; #Text file with list of file paths to monitor
$global:Logfile = $logfilelocation
foreach ($path in $paths)
{
$filter = '*.*' # You can enter a wildcard filter here.
# In the following line, you can change 'IncludeSubdirectories to $false if
# required to disable subfolder monitoring.
$fsw = New-Object IO.FileSystemWatcher $path, $filter -Property @{IncludeSubdirectories = $false; NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
# Here, all four events are registerd. You need only subscribe to events that you need:
#File Created
Register-ObjectEvent $fsw Created -SourceIdentifier "$i+FileCreated" -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
$username = F:\FTU-E2\PsLoggedon.exe -l
Write-Host "The file '$name' was $changeType by $username" -fore green
Out-File -FilePath $global:Logfile -Append -InputObject "The file '$name' was $changeType at $timeStamp by $username" -Force
}
++$i
}
while($true){
start-sleep -second 5
}
命令:
Powershell.exe Add arguments: -ExecutionPolicy Bypass F:\FTU-E2\Scripts\monitor.ps1 -inputPath F:\FTU-E2\RejectPaths.txt -logfilelocation "F:\FTU-E2\Log.txt"
如果任务设置为 SYSTEM 帐户下的 运行,或者实际上是您帐户以外的任何帐户,它会在 $username = F:\FTU-E2\PsLoggedon.exe -l
处挂起,因为 PsLoggedon 正在等待用户接受EULA,它在用户第一次 运行 将它放在机器上时出现。
您可以使用 -accepteula
命令绕过 EULA:
$username = C:\temp\PsLoggedon.exe -l -accepteula
进行此更改后,运行无论是使用我的帐户还是 SYSTEM 帐户将任务设置为 运行 都没问题。
此外,您可以删除脚本底部的无限循环,并在 PowerShell 命令上使用 -NoExit
开关,这样进程就不会结束。
-ExecutionPolicy Bypass -NoExit ...
我有一个 powershell 脚本,用于通过计划任务触发和 运行 日志记录。问题是脚本在 运行 时不会以这种方式写入文件。如果我 运行 它通过 cmd 它完美地工作。我已尝试为脚本提供最高权限,并且在 运行 脚本时将日志文件的确切位置作为参数传递。都没有解决问题。请问有人可以帮忙吗?见代码:
Param([string]$inputPath, [string]$logfilelocation)
$i=0
$paths = Get-Content $inputPath; #Text file with list of file paths to monitor
$global:Logfile = $logfilelocation
foreach ($path in $paths)
{
$filter = '*.*' # You can enter a wildcard filter here.
# In the following line, you can change 'IncludeSubdirectories to $false if
# required to disable subfolder monitoring.
$fsw = New-Object IO.FileSystemWatcher $path, $filter -Property @{IncludeSubdirectories = $false; NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
# Here, all four events are registerd. You need only subscribe to events that you need:
#File Created
Register-ObjectEvent $fsw Created -SourceIdentifier "$i+FileCreated" -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
$username = F:\FTU-E2\PsLoggedon.exe -l
Write-Host "The file '$name' was $changeType by $username" -fore green
Out-File -FilePath $global:Logfile -Append -InputObject "The file '$name' was $changeType at $timeStamp by $username" -Force
}
++$i
}
while($true){
start-sleep -second 5
}
命令:
Powershell.exe Add arguments: -ExecutionPolicy Bypass F:\FTU-E2\Scripts\monitor.ps1 -inputPath F:\FTU-E2\RejectPaths.txt -logfilelocation "F:\FTU-E2\Log.txt"
如果任务设置为 SYSTEM 帐户下的 运行,或者实际上是您帐户以外的任何帐户,它会在 $username = F:\FTU-E2\PsLoggedon.exe -l
处挂起,因为 PsLoggedon 正在等待用户接受EULA,它在用户第一次 运行 将它放在机器上时出现。
您可以使用 -accepteula
命令绕过 EULA:
$username = C:\temp\PsLoggedon.exe -l -accepteula
进行此更改后,运行无论是使用我的帐户还是 SYSTEM 帐户将任务设置为 运行 都没问题。
此外,您可以删除脚本底部的无限循环,并在 PowerShell 命令上使用 -NoExit
开关,这样进程就不会结束。
-ExecutionPolicy Bypass -NoExit ...