【发布时间】:2017-12-21 18:31:50
【问题描述】:
我编写了这个脚本,当我在 ISE 或其他方式中手动运行它时,它可以 100% 完美运行,但从计划任务启动时它不能正常运行。
它不会从本地机器上删除文件或将文件复制到库服务器以进行长时间存储......但它会在本地机器上创建 perfmon 文件就好了。
我最初使用 PSDrive 来映射路径,但在阅读到您似乎无法通过任务计划程序使用 PSDrive 后,我只是对其进行了硬编码。
# Setting variables and creating directories
$FileDate = Get-Date -Format "MM-dd-yyyy-hh-mm-ss"
$EmailDateStart = Get-Date -Format "MM/dd-hh:mm:ss"
$Counters = '\Memory\% Committed Bytes In Use','\Processor Information(_Total)\% Processor Time'
$Interval = '5'
$Samples = '60' # There are 720 samples at 5 seconds per sample in an hour, 60 in 5
$FileDir = "C:\Utilities\CPU_RAM_Stats\Files"
$BLGPath = "$FileDir\$env:COMPUTERNAME-CPU_RAM_Stats_$FileDate.blg"
$BLGFiles = dir $FileDir # Checking local server directory for list of .blg files
$LibPath = "\\<IP>\d$\CPU_RAM_Stats\$env:COMPUTERNAME" # Destination path of .blg files on Library server
# Creating destination folder on Library server if it doesn't exist
if (!(Test-Path $LibPath))
{
New-Item "\\<IP>\d$\CPU_RAM_Stats\$env:COMPUTERNAME" -type Directory
}
<# ========================= DO NOT CHANGE ANYTHING BELOW THIS POINT ========================= #>
# Checking existing log file to see if it's over whatever size was specified above (in KB). If so, deleting file and recreating.
Write-Host "Cleaning up BLG files on Library server to maintain folder size..."
Get-ChildItem $LibPath -Recurse -Include *.blg | Where{-not $_.PsIsContainer} | Sort CreationTime -desc | Select -Skip 500 | Remove-Item -Force
# Checking for existence of files in the library server and deleting from PRD server to keep directories clean, otherwise uploading
foreach ($BLGFile in $BLGFiles)
{
Write-Host ""
Write-Host $(Get-Date) "[ACTION][CHECKING BACKUP] Checking if" $BLGFile.name "has been previously uploaded to the library server" -ForegroundColor Gray
if (Test-Path "$LibPath\$BLGFile")
{
Write-Host $(Get-Date) "[ACTION][BACKED UP]" $BLGFile.name "has been previously uploaded to the library server" -ForegroundColor Yellow
$Removal = Remove-Item "$FileDir\$BLGFile"
$Removal
Write-Host $(Get-Date) "[ACTION][DELETED]" $BLGFile.name "has been deleted from the PRD server" -ForegroundColor Magenta
Write-Host ""
}
else
{
Write-Host $(Get-Date) "[ACTION][NOT BACKED UP]" $BLGFile.name "has NOT been previously uploaded to the library server...continuing to upload" -ForegroundColor Green
# Starting main process of checking for locked files and uploading them
$FilePath = "$FileDir\$BLGFile"
Write-Host $(Get-Date) "[ACTION][FILECHECK] Checking if" $BLGFile.name "is locked" -ForegroundColor Yellow
$FileInfo = New-Object System.IO.FileInfo $FilePath
try
{
$fileStream = $fileInfo.Open( [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read )
Write-Host $(Get-Date) "[ACTION][FILEAVAILABLE]" $BLGFile.name "is not locked and available for upload" -ForegroundColor Green
if ($true)
{
Write-Host $(Get-Date) "Uploading:" $BLGFile.name -ForegroundColor Cyan
Write-Host ""
Write-Host "Uploading to library server..." -ForegroundColor Cyan
Write-Host ""
Copy-Item "$FileDir\$BLGFile" "$LibPath"
Write-Host $(Get-Date) "[ACTION][COMPLETE] Upload complete!" -ForegroundColor Green
}
Write-Host ""
}
catch
{
Write-Host $(Get-Date) "[ACTION][FILELOCKED]" $BLGFile.name " is locked, not uploaded" -ForegroundColor Red
}
}
}
# Running PerfMon to gather CPU/RAM stats
$Results = Get-Counter -Counter $Counters -SampleInterval $Interval -MaxSamples $Samples | Export-Counter -Path $BLGPath -Force -FileFormat "BLG"
编辑/更新:2017 年 12 月 21 日
看起来这是因为我以“SYSTEM”身份运行计划任务...当我将其更改为我的用户帐户时,它运行良好。我怎样才能让它在“系统”下运行任务,这样我就不需要为我的任务提供我的帐户密码?
【问题讨论】:
-
SYSTEM 帐户无权访问您尝试访问的网络共享,您的用户帐户显然有。建议不要再使用 SYSTEM 了。如果任务只需要访问本地资源,请使用 LOCAL SERVICE。如果任务需要访问可公开访问的网络资源(或使用存储在脚本或配置文件中的凭据),请使用 NETWORK SERVICE。如果任务需要通过隐式身份验证(Windows 自动使用当前用户的凭据进行身份验证)访问网络资源,请使用您自己(或专用)的用户帐户。
-
感谢@AnsgarWiechers,我最终只使用了标准的“网络使用”并将其保留为系统。
标签: powershell scheduled-tasks