Я пытаюсь создать диск, использующий скрипт-монитор, используя powershell. В моем скрипте я использую команду Get-Volume
для проверки общего размера тома. Я регистрирую этот скрипт в планировщике задач и запускаю его через задачу. Затем я запускаю задачу, мой скрипт fini sh со следующей ошибкой.
ошибка:
TerminatingError(Get-Volume): "The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: No MSFT_Volume objects found with property 'DriveLetter' equal to 'f'. Verify the value of the property and retry."
C:\DIVA\Script\DataDiskUsedChecker\DiskUsed.ps1 : Microsoft.PowerShell.Cmdletization.Cim.CimJobException: No
MSFT_Volume objects found with property 'DriveLetter' equal to 'f'. Verify the value of the property and retry.
At C:\DIVA\Script\DataDiskUsedChecker\DiskUsed.ps1:77 char:5
+ Write-Error $_.Exception
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,DiskUsed.ps1
Но как только я запускаю команду Get-Volume
с моим терминальным терминалом powershell и снова запускаю задачу, тогда задание завершено sh успешно.
будет полезно, если кто-нибудь даст мне совет, как это исправить.
код:
try {
Start-Transcript -Path "$PSScriptRoot\DiskUsed.log"
#set exection preference
$ErrorActionPreference = "Stop"
#Get Total
$length = (Get-Volume -DriveLetter f).Size
$DiskTotal = New-Object -TypeName psobject -Property @{Name = "DiskTotal"; Size = $length ; Rate = "-"; Discription = "Total disk size" }
#Get DiskFree and DiskUsed
$data = Get-PSDrive -Name F
$DiskUsed = $data.Used
$DiskFree = $data.Free
$UsedRate = "{0:0.00}" -f ($DiskUsed / $DiskTotal.Size * 100)
$FreeRate = "{0:0.00}" -f ($DiskFree / $DiskTotal.Size * 100)
$DiskUsed = New-Object PSObject -Property @{ Name = "DiskUsed"; Size = "$DiskUsed" ; Rate = "${UsedRate}%"; Discription = "Total amount of disk space used" }
$DiskFree = New-Object PSObject -Property @{ Name = "DiskFree"; Size = "$DiskFree" ; Rate = "${FreeRate}%"; Discription = "available disk size" }
#Get DataTransfer
$length = (Get-ChildItem F:\10_DataTransfer -Recurse -Force | Measure-Object Length -Sum).sum
$Rate = "{0:0.00}" -f ($length / $DiskTotal.Size * 100)
$DataTransfer = New-Object -TypeName psobject -Property @{Name = "DataTransfer Prod"; Size = $length ; Rate = "${Rate}%" ; Discription = "Total amount of Under DataTransfer folder" }
#Get Data
$length = (Get-ChildItem F:\04_Data -Recurse -Force | Measure-Object Length -Sum).sum
$Rate = "{0:0.00}" -f ($length / $DiskTotal.Size * 100)
$Data = New-Object -TypeName psobject -Property @{Name = "Data"; Size = $length ; Rate = "${Rate}%" ; Discription = "Total amount of Under Data folder" }
#Get WORK
$length = (Get-ChildItem F:\WORK -Recurse -Force | Measure-Object Length -Sum).sum
$Rate = "{0:0.00}" -f ($length / $DiskTotal.Size * 100)
$WORK = New-Object -TypeName psobject -Property @{Name = "WORK"; Size = $length ; Rate = "${Rate}%" ; Discription = "Total amount of Work folder" }
#Get AppUsed
$length = (Get-ChildItem f: | Where-Object { $_.Name -notin "04_Data", "10_DataTransfer", "WORK" } | Get-ChildItem -Recurse -Force | Measure-Object Length -Sum).Sum
$Rate = "{0:0.00}" -f ($length / $DiskTotal.Size * 100)
$AppUsed = New-Object -TypeName psobject -Property @{Name = "AppUsed"; Size = $length ; Rate = "${Rate}%" ; Discription = "Application install size" }
#Output result to CSV file
$Result = @()
$Result += $DiskUsed
$Result += $AppUsed
$Result += $DataTransfer
$Result += $Data
$Result += $WORK
$Result += $DiskFree
$Result += $DiskTotal
$Result | Select-Object Name, Size, Rate, Discription | ConvertTo-Csv -NoTypeInformation | Out-File "$PSScriptRoot\DataDiskUsed.csv"
Stop-Transcript
}
catch {
Write-Error $_.Exception
Stop-Transcript
exit 1
}
спасибо