私は次のPowershellコードを持っています:
function readConfigData
{
$workingDir = (Get-Location).Path
$file = ""
if ($Global:USE_LOCAL_SERVER)
{
$file = $workingDir + '\Configs\Localhost.ini'
}
else
{
$file = $workingDir + '\Configs\' + $env:COMPUTERNAME + '.ini'
}
Write-Host 'INIFILE: ' $file
if (!$file -or ($file = ""))
{
throw [System.Exception] "Ini fil är inte satt."
}
if (!(Test-Path -Path $file))
{
throw [System.Exception] "Kan inte hitta ini fil."
}
}
readConfigData
$file
関数に渡すことができるローカル変数をどのように宣言すればよいですかTest-Path
。ローカル変数$ fileにデータが入力されますが、他の関数の引数として配置すると、スコープ外のようになります。
スコープについての記事を読みましたが、理解できませんでした。
現在、エラーが発生します:
INIFILE:D:\ Projects \ scripts \ Configs \ HBOX.iniテストパス:空の文字列であるため、引数をパラメーター 'Path'にバインドできません。D:\ Projects \ freelancer.com \ nero2000 \ cmdスクリプトからpowershell \ script.ps1:141 char:27 + if(!(Test-Path -Path $ file))+ ~~~~~ + CategoryInfo:InvalidData: (:) [Test-Path]、ParameterBindingValidationException + FullyQualifiedErrorId:ParameterArgumentValidationErrorEmptyStringNotAllowed、Microsoft.PowerShell.Commands.TestPathCommand
if (!$file -or ($file = ""))
に置き換える必要があります
if (!$file -or ($file -eq ""))
最初のif句で$ fileを空の文字列に割り当てるため、Test-Path呼び出しで変数が空になります。
編集:いくつかの選択肢もあります:PowerShellで文字列がnullまたは空かどうかを確認するにはどうすればよいですか?
あなたはどちらかを使うことができます
if([string]::IsNullOrEmpty($file))
またはただ
if(!$file)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加