我编写了这个脚本,当我在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"
编辑/更新:12/21/17
看起来像是因为我正在运行计划任务,因为#34; SYSTEM&#34; ...当我将其更改为我的用户帐户时,它运行正常。我怎样才能在#34; SYSTEM&#34;下运行任务?所以我不需要为我的帐户提供密码来完成任务吗?
答案 0 :(得分:0)
感谢@AnsgarWiechers,我最终只使用了一个标准的网络使用&#39;使用帐户并将其作为任务计划程序中的系统。试图避免这样做,但事实就是如此。
答案 1 :(得分:0)
如何为此流程创建特定用户?
或者您可以将PowerShell转换为exe来隐藏密码。