【问题标题】:PowerShell: Pass local variable to functionPowerShell:将局部变量传递给函数
【发布时间】:2016-05-09 13:25:38
【问题描述】:

我有以下 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

我应该如何声明可以传递给函数Test-Path的局部变量$file。 我的局部变量 $file 被填充,但是当我将它作为其他函数的参数放置时,它就像它超出了范围。

我阅读了about scopes 的文章,但无法弄清楚。

目前我收到错误:

INIFILE: D:\Projects\scripts\Configs\HBOX.ini 测试路径:无法将参数绑定到 参数 'Path' 因为它是一个空字符串。在 D:\Projects\freelancer.com\nero2000\cmd 脚本到 powershell\script.ps1:141 字符:27 + if (!(Test-Path -Path $file)) + ~~~~~ + CategoryInfo : InvalidData: (:) [Test-Path], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand

【问题讨论】:

    标签: powershell


    【解决方案1】:
    if (!$file -or ($file = ""))
    

    应该替换为

    if (!$file -or ($file -eq ""))
    

    您在第一个 if 子句中将 $file 分配给一个空字符串,因此您的变量在 Test-Path 调用中为空。

    编辑:还有一些替代方案:How can I check if a string is null or empty in PowerShell?

    你可以使用

    if([string]::IsNullOrEmpty($file))
    

    甚至只是

    if(!$file)
    

    【讨论】:

    • Powershell 使用 -eq 进行等式比较。不是 ==
    • 此外,虽然最初为 $file 分配了一个空字符串是正确的,但随后根据 $Global:USE_LOCAL_SERVER 变量,立即为 $file 分配了两个不同的值之一。所以在调用 Test-Path 的时候不为空。
    • 但它在第一个 if 子句中再次分配给一个空字符串。但我能理解你的困惑,我的回答写得不够清楚
    【解决方案2】:

    正如其他人所提到的,您无意中在第一个 if (!$file ... 语句中为 $file 分配了一个空白字符串。这才是你问题的根源。

    但是,而不是:

    if (!$file -or ($file = ""))
    

    您可以使用这个论坛,我发现它更能说明问题:

    if([String]::IsNullOrEmpty($file))
    

    【讨论】:

      【解决方案3】:

      我会定义一个函数Get-ConfigFile 来检索配置并为本地服务器添加一个switch

      function Get-ConfigFile
      {
          Param(
              [switch]$UseLocalServer
          )
      
          $workingDir = (Get-Location).Path
          if ($UseLocalServer.IsPresent)
          {
               Join-Path $workingDir '\Configs\Localhost.ini'
          }
          else
          {
               Join-Path $workingDir ('\Configs\{0}.ini' -f $env:COMPUTERNAME)
          }
      }
      

      我还将使用Join-Path cmdlet 来加入路径而不是字符串连接。

      现在您可以使用以下命令检索配置文件路径:

      $configFile = Get-ConfigFile -UseLocalServer:$Global:USE_LOCAL_SERVER
      

      如果需要,请确保文件存在:

      if (-not(Test-Path -Path $configFile))
      {
          throw [System.Exception] "Kan inte hitta ini fil."
      }
      

      注意: Get-Location 将为您提供当前的 powershell 路径(工作位置),如果您想获得 您的脚本所在的路径,请改用它:

      $workingDir = split-path -parent $MyInvocation.MyCommand.Definitio
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-07
        相关资源
        最近更新 更多