2012-10-31 168 views
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> 

回答

2

嘗試通過variabable這樣的功能:

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放入

+0

試圖改變,但仍然無濟於事。一直玩這樣的4小時,沒有運氣>> – user1451070

+0

@ user1451070在我編輯的答案中嘗試腳本,並看看ShayLevi答案。 –

+0

嘗試過兩種方法,除了它們不會更新變量外,它們都起作用。 – user1451070

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 
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 

這將仍然強制用戶輸入路徑文件,但使代碼看起來更清潔imho。當我測試這個時,我得到了你所要求的結果。我喜歡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 
}