在我的分析過程中,爲什麼我的腳本無法正常工作,我可以找到一些解決方案來解決一些基本的部分(在Stackoverflow的幫助下),但仍然存在一個問題:我找不到解決方案。PowerShell測試路徑不適用於參數參數
當我創建一個臨時目錄變量$ PARAM_TEMP,並用Test-Path檢查這一切都工作正常(目錄是否存在)。但在下一部分中,當我使用相同的技術來處理參數參數$ PARAM_DESTINATION時,語句結果是錯誤的(我已經發現添加單引號的提示 - 在此之後,至少腳本運行時沒有錯誤)。 當我測試的測試輸出在Windows的資源管理器控制檯,它找到的目錄(因此它肯定存在)
這裏是我的腳本的內容BeforeInstallationScript2.ps1:
#
# BeforeInstallationScript2.ps1
#
param(
[Parameter(
Mandatory=$true,
Position=0,
HelpMessage='Path to targetDir')]
[string] $PARAM_DESTINATION
)
"1st Argument: $PARAM_DESTINATION"
# create temporary directory if not exists
$PARAM_TEMP=$env:TEMP+"\MyApp"
"temporary Directory: $PARAM_TEMP"
if(!(Test-Path -Path "$PARAM_TEMP")){
"temp-dir does NOT exist and will be created"
New-Item -ItemType directory -Path $PARAM_TEMP
} else {
"temp-dir exist"
}
# create Timestamp-variable for saving configs
$a = Get-Date
$DATETIME= "" + $a.Year + $a.Month + $a.Day + $a.Hour + $a.Minute
"Timestamp: $DATETIME"
# if there exists already a myApp-Installation, copy config-files
"Parameter-Path=: $PARAM_DESTINATION"
"exists? = " + (Test-Path "'$PARAM_DESTINATION'")
if((Test-Path -Path "'$PARAM_DESTINATION'")) {
"param-path exists"
if((Test-Path -Path "'$PARAM_DESTINATION\configuration\MyApp.conf'")) {
"copy file to $PARAM_TEMP\$DATETIME-MyApp.conf"
Copy-Item "$PARAM_DESTINATION\configuration\MyApp.conf" "$PARAM_TEMP\$DATETIME-MyApp.conf"
}
} else {
"not existing, no files to copy/save"
}
我此腳本在PowerShell中得到的輸出如下:
PS X:\MyApp-Setup> C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -Version 2.0 -NoProfile -NonInteractive -InputFormat None -ExecutionPolicy Bypass -File ".\BeforeInstallationScript2.ps1" "C:\Program Files (x86)\Internet Explorer"
1st Argument: C:\Program Files (x86)\Internet Explorer
temporary Directory: C:\Users\ixmid\AppData\Local\Temp\MyApp
temp-dir does NOT exist and will be created
Verzeichnis: C:\Users\USER\AppData\Local\Temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 16.11.2016 11:01 MyApp
Timestamp: 20161116111
Parameter-Path=: C:\Program Files (x86)\Internet Explorer
exists? = False
not existing, no files to copy/save
PS X:\MyApp-Setup>
正如你所看到的,第一個測試路徑正常工作,並創建缺少的目錄。但在第二部分它不能正常工作。 任何建議,爲什麼第二個(和第三個)測試路徑語句工作錯誤?
完成:執行腳本(當MyApp的目錄現在存在)的第二輸出如下:
1st Argument: C:\Program Files (x86)\Internet Explorer
temporary Directory: C:\Users\USER\AppData\Local\Temp\MyApp
temp-dir exist
Timestamp: 201611161113
Parameter-Path=: C:\Program Files (x86)\Internet Explorer
exists? = False
not existing, no files to copy/save
我敢肯定,我用雙引號沒有單引號嘗試了已經......反正它現在的工作,謝謝 –