我有一個從命令列呼叫的 Powershell 腳本。
script.ps1 "\\testfolder" "testinput" "xml" "xml2html.xsl" "testfile" "css"
該腳本使用這些命令列引數:
param([string]$publish_folder, [string]$input_filename, [string]$input_ext, [string]$transformation_filename, [string]$output_filename, [string]$output_ext)
$input_filename 和 $output_filename 可以是完整路徑 檔案名,只有檔案名或沒有擴展名的檔案名。
$inputFileNameOnly = [System.IO.Path]::GetFileNameWithoutExtension($input_filename)
$inputPath=$publish_folder "\" $inputFileNameOnly "." $input_ext
$outputFileNameOnly = [System.IO.Path]::GetFileNameWithoutExtension($output_filename)
$outputPath=$publish_folder "\" $outputFileNameOnly "." $output_ext
當我在本地運行它時,它可以作業。輸出路徑:
\\testfolder\testfile.css
當我在 AWS 實體中運行相同的腳本時,它失敗了。$inputpath 計算正確,但輸出路徑變為:
\\testfolder\.
所以 $output_filename 和 $output_ext 都是空的。
路徑長于\\testfolder\,但不足以引起問題(大約 150 個字符)。引數的總長度似乎也不是問題。
什么可能導致這個問題?
uj5u.com熱心網友回復:
$outputPath="$publish_folder "\"
看起來你在這里打破了你的串聯。$publish_folder 前的雙引號。
編輯:如果你不想連接( )也可以這樣寫
$outputPath="$publish_folder\$outputFileNameOnly.$output_ext"
雙引號中的任何內容都應正確展開。
uj5u.com熱心網友回復:
我將您的腳本設定為函式并傳遞了這些引數。
function rename {
param(
[string]$publish_folder,
[string]$input_filename,
[string]$input_ext,
[string]$transformation_filename,
[string]$output_filename,
[string]$output_ext
)
$inputFileNameOnly = [System.IO.Path]::GetFileNameWithoutExtension($input_filename)
$inputPath = $publish_folder "\" $inputFileNameOnly "." $input_ext
$outputFileNameOnly = [System.IO.Path]::GetFileNameWithoutExtension($output_filename)
$outputPath = $publish_folder "\" $outputFileNameOnly "." $output_ext
return $outputPath
}
輸出是這個...
\\\\testfolder\\\\testfile.css
嘗試通過$publish_folder而不結束反斜杠
編輯:抱歉格式化。需要一些論壇練習。=)
uj5u.com熱心網友回復:
通過更多的測驗,我們找到了問題的原因。
$outputFileNameOnly = [System.IO.Path]::GetFileNameWithoutExtension($output_filename)
當$output_filename不包含擴展名時,GetFileNameWithoutExtension 失敗(在 AWS 實體中)。在我的本地機器上,這作業正常。
所以我們為命令列引數添加了一個擴展,腳本可以正常作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/393823.html
標籤:电源外壳
