2017-12-21 21 views
1

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" 

편집/업데이트 : "SYSTEM"... 내 사용자 계정을 변경할 때와 예약 된 작업을 실행 되었기 때문에이처럼 12/21/17

이 보이는 , 괜찮 았어. "SYSTEM"아래에서 작업을 실행하도록하려면 어떻게해야합니까? 이렇게하면 작업에 대한 내 계정에 암호를 제공 할 필요가 없습니다.

+1

SYSTEM 계정에 액세스하려는 네트워크 공유에 대한 액세스 권한이 없으므로 사용자 계정에 분명히 있습니다. 시스템을 더 이상 사용하지 않는 것이 좋습니다. 태스크가 로컬 자원에만 액세스 할 필요가있는 경우 LOCAL SERVICE를 사용하십시오. 작업에 공개적으로 액세스 할 수있는 네트워크 리소스에 대한 액세스가 필요하거나 스크립트 또는 구성 파일에 저장된 자격 증명을 사용하는 경우 네트워크 서비스를 사용하십시오. 암시 적 인증 (Windows가 현재 사용자의 자격 증명으로 자동 인증)에서 네트워크 리소스에 대한 액세스가 필요한 경우 자신의 (또는 전용) 사용자 계정을 사용하십시오. –

+0

감사합니다 @AnsgarWiechers, 나는 표준 'net use'를 사용하여 시스템으로 남겨 두었습니다. – Ilya

답변

0

덕분에 @AnsgarWiechers 덕분에 계정으로 표준 'net use'를 사용하고 작업 스케줄러에 시스템으로 남겨 두었습니다. 그렇게하는 것을 피하려고했지만 그것이 그것이 무엇인지를 설명합니다.