2016-07-05 4 views
0

내 NetApp에 ​​액세스해야하는 기본 스크립트를 만들고 두 명령을 실행하고 결과를 csv 파일로 출력 한 다음 해당 csv 파일을 전자 메일로 보냅니다. 마지막으로 파일을 삭제해야합니다.Powershell 스크립트 제거 후 항목 잠금 파일

읽는 동안 내가 배웠던 것부터 이전 단계가 완료 될 때까지 powershell이 ​​대기하지 않아야하고 보낸 후 자동으로 파일을 닫지 않아야합니까? Send-MailMessage 명령까지 파일을 삭제할 수 있습니다. 바로 다음 명령은 파일을 삭제하는 것이며 오류가 생성됩니다. 이것은 스크립트에서 각 행을 강조 표시하고 명령 간 몇 분 동안 기다리면서 수동으로 실행하는 경우에도 발생합니다. 지금까지 파일 잠금을 해제하는 유일한 방법은 Powershell을 닫는 것입니다.

Powershell이 ​​열린 파일을 강제로 삭제하도록하려면 어떻게해야합니까? 다른 프로세스에서이 프로세스를 사용하고 있지 않으며 전자 메일을 받았으며 더 이상 파일을 유지하지 않으려합니다. 이 작업을 수행하는 데 타사 도구를 사용하지 않는 것이 좋습니다

파일을 삭제하려고하면 오류가 발생합니다.

삭제 명령 :

If (Test-Path $attachment){ 
    Remove-Item $attachment -Force 
} 

오류 :

Remove-Item : Cannot remove item C:\perfstat\NcVol.csv: The process cannot access the file 'C:\perfstat\NcVol.csv' because it is being used by another process. 
At line:1 char:1 
+ Remove-Item $attachment -Force 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : WriteError: (C:\perfstat\NcVol.csv:FileInfo) [Remove-Item], IOException 
    + FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand 

다른 비트 :이 파일이 얼마나 큰 몰라,하지만 난 그것을 그렇게 생각 할 수

$attachment = "c:\perfstat\NcVol.csv" 
# Get the stats from the NetApp 
Get-NcVol | Export-Csv -LiteralPath $attachment -Force -NoTypeInformation -Verbose 
Get-NcAggr | Export-Csv -LiteralPath $attachment -Force -NoTypeInformation -Verbose -Append 
#Send the message 
Send-MailMessage -To $emailTo -From $emailFrom -Subject $emailSubject -Body $emailBody -BodyAsHTML -Attachments $attachment -SmtpServer $emailSmtpServer 
#Now delete the file 

답변

0

이메일이 전송되는 동안 약간의 시간이 필요합니다.

Start-Sleep -s 10 

Start-Sleep -s(for seconds) <seconds> 

이 작동하는지 알려 주시기 바랍니다 : 당신이 메일을 보낸 후

그냥 대기를 통합하려고합니다.