![Powershell 指令無法透過批次檔正常運作](https://rvso.com/image/1671514/Powershell%20%E6%8C%87%E4%BB%A4%E7%84%A1%E6%B3%95%E9%80%8F%E9%81%8E%E6%89%B9%E6%AC%A1%E6%AA%94%E6%AD%A3%E5%B8%B8%E9%81%8B%E4%BD%9C.png)
該腳本接收批次檔發送的參數,讀取音樂日誌檔案並根據 LUFS 值範圍產生一個列表,該列表具有標題和兩列,其中包含音樂名稱和每首音樂的 LUFS 值。
當我在批次檔之外運行命令時,腳本可以正常工作,但是當我透過批次檔運行它時,列表會產生不正確。
腳本:
Param(
[decimal]$env:_vLUF
)
[decimal]$vLUFps = $env:_vLUF
$files = "C:\Users\$env:username\Desktop\Logs_LUFS\List Music FOR NORMALIZATION.txt"
$logs = "C:\Users\$env:username\Desktop\Logs_Musics"
# READ LOG files of Musics and generates the List Music FOR NORMALIZATION.txt ($files)
$logMatches = Select-String -Path "C:\Users\$env:username\Desktop\Logs_Musics\*.*" -Pattern '(?<I>^ +I:) +(?<LUFS>.+)|(?<I>^Input Integrated:) +(?<LUFS>.+)' -List | Select-Object -Property FileName -ExpandProperty Matches
$results = foreach ($log in $logMatches) {
$pos = $log.Filename.IndexOf("_")
$leftPart = $log.Filename.Substring(0, $pos)
$rightPart = $log.Filename.Substring($pos+1)
$LUFS = $log.Groups | Where-Object { $_.Name -eq "LUFS" }
[PSCustomObject]@{
Música = $rightPart
LUFS = [decimal]$($LUFS.Value -replace " .*")
}
}
$vLUFpsLess= ($vLUFps)+ (-0.9)
$vLUFpsGreat= ($vLUFps)+ (-0.5)
$results | Where-Object {($_.LUFS -lt $vLUFpsLess) -or ($_.LUFS -gt $vLUFpsGreat) } | Out-file $files
在批次檔中執行命令來運行腳本:
powershell.exe -executionpolicy remotesigned -File "D:\z_Batchs and Scripts\Batchs\LUFS_Normalize\ArqsNorms_LUFS_pass.ps1" %_vLUF%
該%_vLUF%
變數是傳遞給腳本的參數。
注意:
透過批次文件,腳本剪切了包含更多字元的音樂名稱,並剪切了 LUFS 列。
透過批次檔執行腳本時,Out-file 指令無法正確產生 .txt 檔案。我是否需要為我的腳本添加一些其他命令,以便它可以透過批次檔正確運行?
答案1
這是腳本在沒有管理員身份的情況下透過批次檔執行時錯誤產生的清單:
這是批次檔中將產生日誌檔案並執行 powershell 腳本的段落:
REM ============================================== PARAGRAPH GENERATES LOGS ============================================
:LOGFILES
cls
mode 111,08
FOR /F "delims=" %%a in ('where .:*.* ^|findstr /vi "_LOUDNORM _EBU"') DO (
ffmpeg -hide_banner -i "%%a" -af ebur128 -f null NUL 2> "C:\Users\%username%\Desktop\Logs_LUFS\LOG_%%~na%%~xa"
)
REM Run the Powershell script in normal mode:
REM powershell.exe -executionpolicy remotesigned -File "D:\z_Batchs e Scripts\Batchs\Normaliza_LUFS\ArqsNorms_LUFS_pass.ps1" %_vLUF%
REM Run the Powershell script in administrator mode:
REM powershell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -Verb RunAs powershell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File \"D:\z_Batchs e Scripts\Batchs\Normaliza_LUFS\ArqsNorms_LUFS_pass.ps1\" -_vLUF %_vLUF%'"
腳本或腳本的任何命令都沒有問題,我們必須分析腳本在透過批次檔執行時的執行優先順序。
批次檔中的指令mode
定義執行批次時將向使用者顯示的螢幕尺寸(寬度 x 高度)。
當我們在管理員模式下運行腳本時,根據我所做的測試,它控制了處理,在這種情況下,Out-file 命令根據腳本中定義的內容生成訊息,從而正確生成文件文字。
當我們在正常模式下執行腳本時,它不會控制處理,Out-file 命令會產生文字文件,其大小與批次文件段落中定義的螢幕寬度大小相同。由於有一首音樂的名稱超過 111 個字元(這是mode
批次檔命令中定義的值),因此該命令將剪切其餘的檔案訊息,並且 111 個字元之後的所有內容都不會寫入文字檔案。
我不知道這是否是一個錯誤,但我們應該分析透過批次檔執行的每個 powershell 腳本在執行其命令時都必須具有完全優先級,無論是在管理模式還是在正常模式下呼叫。