如何使用PowerShell在函数中传递多个参数

时间:2019-03-22 08:39:18

标签: powershell

我有一个带有两个参数的函数,用于测试检查输出目录。似乎第一个参数获取了数据,但是第二个参数传入了空值。功能如下:

function check_directory ([String]$outputpath , [String]$logoutputpath){
    $testpath = Test-Path -Path $outputpath -IsValid

    if ( $testpath -eq $True) { 
        Write-Warning "File Directory Path Already Exist.."
    } else {
        Write-Host "File Directory Creating $($outputpath).."  -BackgroundColor Black -ForegroundColor Yellow
        New-Item -ItemType Directory -Path $outputpath
        Write-Host "File Directory Created $($outputpath)" -BackgroundColor Black -ForegroundColor Green
    }

    $logtestpath = Test-Path  -Path $logoutputpath -IsValid

    if ($logtestpath -eq $True) {
        Write-Warning "Logs Directory Path Already Exist.."
    } else {
        Write-Host "Logs Directory Creating $($logoutputpath).." -BackgroundColor Black -ForegroundColor Yellow
        New-Item -ItemType Directory -Path $logoutputpath
        Write-Host "Logs Directory Created $($logoutputpath)" -BackgroundColor Black -ForegroundColor Green
    } 
}

错误是:

WARNING: File Directory Path Already Exist..
Test-Path : Cannot bind argument to parameter 'Path' because it is an empty string.
At D:\Users\mdelikan\Desktop\StepUp2019\Proje_20032019 .ps1:37 char:38
+      $logtestpath = Test-Path  -Path $logoutputpath -IsValid
+                                      ~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Test-Path], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand

Logs Directory Creating ..

New-Item : Cannot bind argument to parameter 'Path' because it is an empty string.
At D:\Users\mdelikan\Desktop\StepUp2019\Proje_20032019 .ps1:49 char:53
+                  New-Item -ItemType Directory -Path $logoutputpath
+                                                     ~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [New-Item], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.NewItemCommand

我做错了。谁能指出确切的含义?

0 个答案:

没有答案