1

The script is self-explanitory but I have no idea how to get it to work. I want to figure out how to pass the variable $ComputerPath to the function and in that script set $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

My Result

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

2

try pass the variabable to function like this:

CheckPath $ComputersPath

$ in single quote is see by powershell as literar 'dollar sign' and not a variable qualifier

And change these lines:

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

in

CheckPath (Read-Host "Enter Valid Path")

EDIT:

Try this:

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")
}

}

Edit:

To set whatever variable you use in the function I give you a example:

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")
}

after you can try this:

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

Now you can use the code in this function and put in your CheckPath function based on the logic of your goal

Sign up to request clarification or add additional context in comments.

7 Comments

Attempted changes but still no avail. Been playing with this for like 4 hours and no luck >.>
Tried both, and they both work except they won't update the variable.
@user1451070 You can add the else like in my answer. And... it'snt necessary modify your original post based on the answer provided to you beacuse make question and answer less readable... thank you.
That contradicts my goal, i intend to call this function for each of the variables that I will be testing via this function. If i add the ComputersPath it will be of a mute point.
Set whatever variable is inputted into the function. I want to set the variable to whatever is entered into the function. I am trying to create a dynamic function so if i call CheckPath $variable1 it will run the function $Variable1 in the function and if need be adjust the value of $Variable1 as needed. The line $global:ComputersPath = $args[0] does to what i need it to, but i want to be able to set any variable i enter into the function.
|
1

First, there's no need to cast $args[0] to a string. second, you're passing $ComputersPath inside single quotes. Single quotes prevent variable expansion and the value is passed as is, literally.

Give this a try:

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

Here's a more robust way to prompt the user indefinitely in case the path provided doesn't exist:

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

$path

Comments

0

I would define the parameter within the function, and declare it as mandatory.

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

This will still force the user to input a path file, but makes the code look cleaner imho. When I tested this, I got the results you were asking for. I do like Shay Levi's method of prompting the user. Using his example it would look like this:

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.