-3

I declare all variable in var.ps1 like this:

$a = "aa"
$b = "bb"

In the second script check.ps1 I try to create a function to check if the argument passed exist in var.ps1 something like this:

check "$a" "$b" "$c"

I need to use args in my function.

Could you please give me any suggestion?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
h.s
  • 115
  • 3
  • 11
  • like this exemple i need to have a message erreur ==> $c does not exist – h.s Apr 17 '17 at 08:46
  • Dot source can do that followed by if else – Ranadip Dutta Apr 17 '17 at 08:48
  • Use `get-Variable` inside that script. Also use single quotes when passing variable names inside your function, or else they will expand into their values. – Vesper Apr 17 '17 at 08:48
  • i try thid $a = Get-Variable "$args" but i have this erreur message Get-Variable : Cannot find a variable with the name '-Source $destinationFolder'. – h.s Apr 17 '17 at 08:56
  • 3
    This looks a lot like the two questions you posted on Tuesday. As the duplicate link on the other answers explain, you can't infer what the original variable name was that was passed to the function. Please explain what you're trying to accomplish and why if you want help. – Mathias R. Jessen Apr 17 '17 at 10:34
  • Thx mathias for your help i try to use Get-Variable $arg also i use variable directly when i call function and it work foreach ($arg in $args) {$d = Get-Variable $arg -ErrorAction SilentlyContinue if (!$d.Value) {(Write-Host "[ERROR] Variable $arg is unset"}} – h.s Apr 17 '17 at 10:52
  • 1
    Please answer my question: What are you trying to accomplish, and *why*? – Mathias R. Jessen Apr 17 '17 at 11:14
  • How is this question different from the [last](http://stackoverflow.com/q/43346883/1630171) [two](http://stackoverflow.com/q/43341573/1630171) questions you asked? – Ansgar Wiechers Apr 17 '17 at 11:55
  • thx Ansgar Wiechers and Mathias R. Jessen for your answers i flow the exemple of Ansgar and my script work fine – h.s Apr 17 '17 at 15:33

1 Answers1

1

It's unclear why you want this and I'd probably solve it another way, but to answer your question, you can try the sample below.

Remember to use literal strings (single quotes) for values with a leading $ to avoid them being treated as a variable. If not, PowerShell will try to replace the variables with it's value (or nothing if it's not defined), which means that Check-Variables won't get the variable names. The following solution accepts both 'a' and '$a'.

Var.ps1

$a = "aa"
$b = "bb"

Check.ps1

#Dot-sourcing var.ps1 which is located in the same folder as Check.ps1
#Loads variables into the current scope so every function in Check.ps1 can access them
. "$PSScriptRoot\var.ps1"

function Check-Variables {

    foreach ($name in $args) {
        #Remove leading $
        $trimmedname = $name -replace '^\$'

        if(-not (Get-Variable -Name $trimmedname -ErrorAction SilentlyContinue)) {
            Write-Host "ERROR: Variable `$$trimmedname is not defined"
        }
    }
}

Check-Variables '$a' "b" '$c'

Demo:

PS> C:\Users\frode\Desktop\Check.ps1
ERROR: Variable $c is not defined
Frode F.
  • 52,376
  • 9
  • 98
  • 114