嵌套变量的问题

时间:2012-10-31 09:37:06

标签: powershell

脚本是自我解释但我不知道如何让它工作。我想弄清楚如何将变量$ComputerPath传递给函数并在该脚本集中$ComputerPath

Function CheckPath {
    While (!$args[0]) {
        Write-Host "`nVariable Undefined"
        $Args[0] = Read-Host "Enter Valid Path"
    } 
    while (!(test-path $Args[0])) {
        Write-Host "Unable To Location Files, Please Check Again."
        $args[0] = Read-Host "Enter Valid Path"
    }
}

$ComputersPath = "missingfile.txt"

$ComputersPath
CheckPath $ComputersPath
$ComputersPath

我的结果

PS Z:\Powershell Scripting\Test Lab> .\TestFunction.ps1
missingfile.txt
Unable To Location Files, Please Check Again.
Enter Valid Path: log.txt
missingfile.txt
PS Z:\Powershell Scripting\Test Lab>

3 个答案:

答案 0 :(得分:2)

尝试将可变的函数传递给这样的函数:

CheckPath $ComputersPath
单引号中的

$被powershell视为literar'美元符号'而不是变量限定符

并改变这些界限:

$args[0] = Read-Host "Enter Valid Path"
CheckPath

CheckPath (Read-Host "Enter Valid Path")

编辑:

试试这个:

Function CheckPath {    
    IF (!$args[0]) {    
        Write-Host
        Write-Host "Variable Undefined"
        $args[0] = Read-Host "Enter Valid Path"
    }
    else
    {
      "Found"
      $global:ComputersPath  = $args[0]
    }

IF (!(Test-Path ($Args[0]))) {
    Write-Host
    Write-Host "Unable To Location Files, Please Check Again."
    CheckPath (Read-Host "Enter Valid Path")
}

}

编辑:

要设置你在函数中使用的任何变量,我给你一个例子:

function test 
{   
    $myvar = $MyInvocation.line.split(' ')[1].replace('$','')    
    "`$$myvar value now is $($args[0])"

    Invoke-Expression  "`$global:$myvar = 'yahoo'"    

    "{0} value now is {1}" -f "`$$myvar", (invoke-expression "`$$myvar")
}

你可以试试这个:

PS C:\ps> $a="google" #or whatever variable you want...
PS C:\ps> test $a
$a value now is google
$a value now is yahoo
PS C:\ps> $a
yahoo

现在您可以使用此功能中的代码,并根据目标逻辑输入CheckPath function

答案 1 :(得分:1)

首先,没有必要将$ args [0]强制转换为字符串。第二,你在单引号内传递$ ComputersPath。单引号阻止变量扩展,值按字面顺序传递。

尝试一下:

Function CheckPath {

    IF (!$args[0]) {
        Write-Host
        Write-Host "Variable Undefined"
        $args[0] = Read-Host "Enter Valid Path"
    } 

    IF (!(Test-Path $Args[0])) 
    {
        Write-Host "`nUnable To Location Files, Please Check Again."
        $args[0] = Read-Host "Enter Valid Path"
        CheckPath
    }

    $args[0]
}



CheckPath $ComputersPath

如果提供的路径不存在,这是一种更有效的方式来无限提示用户:

do{
    [string]$path = Read-Host "Enter a valid path"
} while ( -not (test-path $path) )

$path

答案 2 :(得分:0)

我会在函数中定义参数,并将其声明为必需参数。

Function CheckPath {
    param (
        [parameter(Mandatory=$true)]
        [string]$Path 
    )#End Param

    while (!(test-path $Path)) {
        Write-Host "Unable To Location Files, Please Check Again."
        $Path = Read-Host "Enter Valid Path"
    } #End While
    write-output $path
} #End Function

这仍然会强制用户输入路径文件,但会使代码看起来更清晰。当我测试这个时,我得到了你要求的结果。我喜欢Shay Levi提示用户的方法。用他的例子看起来像这样:

Function CheckPath {
    param (
        [parameter(Mandatory=$true)]
        [string]$Path 
    )#End Param

    do{
        [string]$path = Read-Host "Enter a valid path"
    } while ( -not (test-path $path) )
    write-output $path
}