1

该脚本是不言自明的,但我不知道如何让它工作。我想弄清楚如何将变量传递$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>
4

3 回答 3

2

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

CheckPath $ComputersPath

$在单引号中,powershell 将其视为文字“美元符号”,而不是变量限定符

并更改这些行:

$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现在您可以使用此函数中的代码并根据您的目标逻辑放入您的代码

于 2012-10-31T09:38:58.113 回答
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
于 2012-10-31T10:07:05.437 回答
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
}
于 2012-10-31T18:16:44.367 回答