2017-10-23 5 views
1

を参照して、私はその後、私はPowerShellは、現在のディレクトリとサブフォルダ

try { 
WriteLog("Installing...") 
$installresult = (Start-Process msiexec.exe -ArgumentList "/i 
$PSScriptRoot\InstallPrism6.msi /qn /norestart" -Wait -PassThru).ExitCode 
WriteLog("Installation finished with return code: $installresult") 
} 
catch { 
    WriteLog($_.Exception.Message) 
} 

これは正常に動作しますようにそれを呼び出すことができ、現在のスクリプトディレクトリ

$PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition 

内のファイルを参照するために、次のコードを使用します。私はこれが動作しない場合は、エラーコード1639 で失敗したので、

try { 
WriteLog("Installing...") 
$installresult = (Start-Process msiexec.exe -ArgumentList "/i $PSScriptRoot 
+ \test\InstallPrism6.msi /qn /norestart" -Wait -PassThru).ExitCode 
WriteLog("Installation finished with return code: $installresult") 
} 
catch { 
    WriteLog($_.Exception.Message) 
} 

のようなサブディレクトリ内のファイルを参照したい場合は$ PSScriptRootを使用している場合しかし、どのように私はサブディレクトリを参照することができますか?

+0

カッコでPowerShell関数を呼び出すのではなく、 'WriteLog" Installing ... "'なし '()'と言ってください。 –

答えて

0

がまだ定義されていない場合にのみ、その変数を必要とするので、$PSScriptRoot変数は、PowerShellの3.0以降で事前定義されていることに注意してください。

if (-not $PSScriptRoot) { 
    $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent 
} 

try { 
    WriteLog "Installing..." 
    $installresult = (Start-Process msiexec.exe -ArgumentList "/i","`"$PSScriptRoot\test\InstallPrism6.msi`"","/qn","/norestart" -Wait -PassThru).ExitCode 
    WriteLog "Installation finished with return code: $installresult" 
} 
catch { 
    WriteLog $_.Exception.Message 
} 

-ArgumentListは技術的には配列で、私はパスが空白を含む場合には"を埋め込まれた:私はあなたが何をしたいのための適切な構文は次のようになりますと信じています。

0

パスに空白を入れないでください。変数を使用して文字列を作成する場合、連結する必要はありません。サブフォルダを参照することではありません。

try { 
WriteLog("Installing...") 
$installresult = (Start-Process msiexec.exe -ArgumentList "/i ${PSScriptRoot}\test\InstallPrism6.msi /qn /norestart" -Wait -PassThru).ExitCode 
WriteLog("Installation finished with return code: $installresult") 
} 
catch { 
    WriteLog($_.Exception.Message) 
} 
+0

残念ながら、これは動作しません。 – Pickle

+0

'$ {PSScriptRoot} \ test \ InstallPrism6.msi'文字列を印刷してみてください...スペースを含んでいますか?また、「これはうまくいかない」というのは、あなたは同じエラーを持っているということですか? – Alfabravo

+0

"\ test \ InstallPrism6.msi"をスペースなしで返します。エラーコード1619. – Pickle

関連する問題