在我的分析过程中,为什么我的脚本无法正常工作,我可以找到一些解决方案来解决一些基本的部分(在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
我敢肯定,我用双引号没有单引号尝试了已经......反正它现在的工作,谢谢 –