2017-10-12 10 views
0

첫 번째 글. 내가 뭘하는지 모르겠다. 그냥 맨손으로 ..mkv의 creationtime -powershell을 (를) 비교

우리가 설정 한 사무실 카메라가 Windows 2016 스토리지 서버에있는 "카메라"공유에 피드 업로드를 중지했는지 확인하는 스크립트를 작성해야했습니다. . 최신 .mkv가 현재 시간 (get-date)에 비해 한 시간 이상 지난 경우 "문제"카메라를 수동으로 다시 시작해야합니다. (. 스크립트에 필요 부분)

은 여기 내 이사가 지금까지 쓴 것 없다 :

#Variable Definitions start here 

$numhours = 1 

Get-ChildItem "d:\Shares\Cameras" | Foreach {  

$folderToLookAt = ($_.FullName + "\*.mkv") 
$result = Get-ChildItem -Recurse $folderToLookAt | Sort-Object CreationTime -Descending 


echo $result[0].FullName 
echo $result[0].CreationTime 

} 

이 첫 번째 변수는 정말 아직 사용되지 않은,하지만 난 종류의 무엇으로 벙어리 - 강타 해요 할 일. 위의 내용은 최신 .mkv의 전체 이름과 생성 시간을 반환합니다.

다음 부분에 대한 제안 사항? 제안에 대해 미리 감사드립니다.

+0

와우! 신사 숙녀 여러분 께 감사드립니다. 나는 앞으로 더 많은 것을 올릴 것입니다. 지역 사회가 매우 민감합니다. – Roachmen

답변

0

당신의 경로가 같이 : 그렇다면

D:\Shares\Cameras\Camera1\file1.mkv 
D:\Shares\Cameras\Camera1\file2.mkv 
D:\Shares\Cameras\Camera2\file1.mkv 
D:\Shares\Cameras\Camera2\file2.mkv 
D:\Shares\Cameras\Camera3\file1.mkv 
. 
. 
. 

, 내가 뭔가를 할 것 이렇게하면 :

# The path to your files 
$CameraShareRoot = 'D:\Shares\Cameras'; 

# Number of Hours 
$NumberOfHours = 1; 

# Date and time of significance. It's $NumberOfHours in the past. 
$MinFileAge = (Get-Date).AddHours(- $NumberOfHours); 

# Get all the folders at the camera share root 
Get-ChildItem -Path $CameraShareRoot -Directory | ForEach-Object { 
    # Get the most recently created file in each folder 
    $_ | Get-ChildItem -Recurse -Filter '*.mkv' -File | Sort-Object -Property CreationTime -Descending | Select-Object -First 1 
} | Where-Object { 
    # Remove any files that were created after our datetime 
    $_.CreationTime -lt $MinFileAge; 
} | Select-Object -Property FullName, CreationTime 

오래된 카메라의 전체 파일 이름과 생성 시간을 출력합니다.

결과는 모든 파일이있을 때 당신은 자신에게 보고서를 이메일로 이런 일을 할 수있는 :

# The path to your files 
$CameraShareRoot = 'D:\Shares\Cameras'; 

# Number of Hours 
$NumberOfHours = 1; 

# Date and time of significance. It's $NumberOfHours in the past. 
$MinFileAge = (Get-Date).AddHours(- $NumberOfHours); 

# Get all the folders at the camera share root, save the results to $StaleCameraFiles 
$StaleCameraFiles = Get-ChildItem -Path $CameraShareRoot -Directory | ForEach-Object { 
    # Get the most recently created file in each folder 
    $_ | Get-ChildItem -Recurse -Filter '*.mkv' -File | Sort-Object -Property CreationTime -Descending | Select-Object -First 1; 
} | Where-Object { 
    # Remove any files that were created after our datetime 
    $_.CreationTime -lt $MinFileAge; 
} 

# If there are any stale camera files 
if ($StaleCameraFiles) { 
    # Send an email 
    $MailMessage = @{ 
     SmtpServer = 'mail.example.com'; 
     To = '[email protected]'; 
     From = '[email protected]'; 
     Subject = 'Stale Camera Files'; 
     Body = $StaleCameraFiles | Select-Object -Property FullName, CreationTime | ConvertTo-Html -Fragment | Out-String; 
     BodyAsHtml = $true; 
    } 
    Send-MailMessage @MailMessage; 
} 

는 일반적으로 후자는 파일 이동 업데이트 할 수 있기 때문에 LastWriteTime 대신 CreationTime을 사용하기를 원할 것 또는 복사하지만, 아마도 당신이 여기에서 원하는 것입니다.

0

CreationTime 날짜를 (Get-Date).AddHours(-1)으로 비교해야합니다. AddHours 메서드를 사용하면 DateTime에 시간을 추가 할 수 있지만 빼기도 가능합니다. 그것은 또한 당신의 코드를 약간 최적화

$Path = 'd:\Shares\Cameras' 
$CreationTime = Get-ChildItem -Path $Path -Filter *.mkv | 
    Sort-Object -Property CreationTime -Descending | 
    Select-Object -First 1 -ExpandProperty CreationTime 
if ($CreationTime -lt (Get-Date).AddHours(-1)) { 
    # your action here (restart, send mail, write output, ...) 
} 

:

는 다음과 같은 예를 사용할 수 있습니다. ;)

+0

또한 같은 일을하지 않습니다. 원래 코드는 카메라 서브 디렉토리 당 한 번 검색하고, 루트 디렉토리에서 하나의 mkv 파일을 찾습니다. – TessellatingHeckler

+0

이것이 필요한 경우,'$ Path'에 와일드 카드를 추가 할 수 있습니다. 시나리오/폴더 구조에 따라'Get-ChildItem'에'd : \ Shares \ Cameras \ * \'또는'-Recurse'가 추가되었습니다. – vrdse

0
$LatestFile = Get-ChildItem C:\Users\Connor\Desktop\ | Sort CreationTime | Select -Last 1 
if ($LatestFile.CreationTime -gt (Get-Date).AddHours(-1)){ 
    #It's Currently Working 
} else { 
    #Do Other Stuff 
} 
0

이 시도 :

Get-ChildItem "c:\temp" -Filter *.mkv -File | sort CreationTime -Descending | 
    select -First 1 | where CreationTime -lt (Get-Date).AddHours(-1) | 
     %{Write-Host "Alert !!" -ForegroundColor Red} 
1

반전을 논리를 - 대신, 그들을 정렬 모든 파일을 검색 가장 최근을 발견하고, 날짜를 확인, 그것은에게 라운드 다른 방법을한다. 하나도 발견되지 않은 경우

컷오프 이후 생성 된 파일을보고, 경고 : 나는 믿고있어

$cutOffTime = [datetime]::Now.AddHours(-1) 

Get-ChildItem "d:\Shares\Cameras" | Foreach {  

    $folderToLookAt = ($_.FullName + "\*.mkv") 
    $result = Get-ChildItem -Recurse $folderToLookAt | Where-Object { $_.CreationTime -gt $cuttoffTime } 

    if (-not $result) 
    { 
     "$($_.Name) has no files since the cutoff time" 
    } 
} 
+1

완벽합니다. 정말 고맙습니다!이제 "컷오프 이후 파일 없음"사건이있는 경우 g-apps를 사용하여 전자 메일로 IF 문을 얻어야합니다. 제 능력 수준에 있다고 생각합니다. 건배! – Roachmen