2016-11-16 4 views
0

私の解析中に私のスクリプトが動作しないのですが、いくつかの基本的な部分を解決するいくつかの解決法がありますが(Stackoverflowの助けを借りても)私は解決策を見つけることができません。PowerShell Test-Pathが引数パラメータのために機能しない

一時ディレクトリ変数$ PARAM_TEMPを作成し、これをTest-Pathでチェックすると、すべてが正常に動作しています(ディレクトリが存在するかどうか)。しかし、次の部分では、パラメータ$ PARAM_DESTINATIONの引数に同じテクニックを使用すると、statement-resultが間違っています(これは一重引用符を追加するヒントをすでに見つけました。 私は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> 

、最初のテストパスが正常に動作し、不足しているディレクトリを作成します。しかし、2番目の部分では正常に動作しません。 2番目(および3番目)のTest-Pathステートメントが間違っている理由は何ですか?完了するために

次のように(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 

答えて

3

あなたが"引用符と'引用符の両方を使用します。変数が必要な場合は、$PARAM_DESTINATIONを展開し、二重引用符だけを使用する必要があります。だから:"$PARAM_DESTINATION"

私は単一引用符なしで二重引用符ですでにそれを試してみましたかなり確信して...とにかくそれは今働いている、あなたに感謝し、二重と一重引用符でより http://windowsitpro.com/blog/single-quotes-vs-double-quotes-powershell-whats-difference

+0

を読みます –

関連する問題