1

I try to make this script working

Function test_var
{$return = 0
foreach ($arg in $args)
{ 
if (!$arg) {(Write-Host "ERROR : $arg.Name Missing variable" -ForegroundColor Red -BackgroundColor Black)
$return = 1}
}
return $return
}

When I call the function with arguments

test_var "c" "$b" "$a" 

The function work correctly but I can't display the name of the actual $arg because the value was empty .

Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
h.s
  • 115
  • 3
  • 11
  • 1
    Your question is a bit unclear. What is the expected output and what are you actually seeing? – Mathias R. Jessen Apr 11 '17 at 09:16
  • $a = "test1" $b="test2" #$c="test3" – h.s Apr 11 '17 at 09:41
  • i need to see ERROR : $C Missing variable i have those varriable declared in anather text file but actuly i can juste see ERROR : .Name Missing variable – h.s Apr 11 '17 at 09:45
  • 1
    The function can't "know" the variable names that you passed to it. Declare your parameters like suggested by Martin Brandl – Mathias R. Jessen Apr 11 '17 at 09:49
  • like i said i have all my variable in one text file and i need to check if those variable existe really in my file. i try this function test_var "$c" "$b" "$a" and i nedd to cheack all those varriable with a simple script – h.s Apr 11 '17 at 09:52
  • So the function know all those varriable – h.s Apr 11 '17 at 10:26
  • It knows the variable *values*, not the name you use for them. – Mathias R. Jessen Apr 11 '17 at 13:26
  • OK so there is no solution for this issue i need to display the name of the arg in message erreur – h.s Apr 11 '17 at 13:44
  • No. If you explain what practical/actual problem you're trying to solve we might be able to help you – Mathias R. Jessen Apr 11 '17 at 13:45
  • i need to create a function to check if a predefined variable exist in external text file the text file contain $a = "abd" $b = "cdf" $c="aze" In my script i call the text file with . (Join-Path $PSScriptRoot $I ) and i create this function – h.s Apr 11 '17 at 13:48
  • the function work fine when i make a modification like $a = "abd" $b = "cdf" #$c="aze" and i have my error message ERROR : Missing variable ==> but the name of empty variable is missing – h.s Apr 11 '17 at 13:48
  • I'd suggest you ask a new question *with those details*, and please describe exactly what you expect, what happens and what you mean when you say things like "works fine". I'd suggest reading [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) first. – Mathias R. Jessen Apr 11 '17 at 13:51
  • thx you really help me with your commant – h.s Apr 11 '17 at 13:58

1 Answers1

2

Like Mathias mentioned in his comment, your question is a bit unclear. However:

PowerShell already has a built-in mechanism to validate arguments. Instead of using $args consider using predefined arguments for your function. In the following example, the argument $a is mandatory whereas $b is optional:

function Test-Var
{
    Param
    (
        [Parameter(Mandatory=$true)]
        $a,

        [Parameter(Mandatory=$false)]
        $b
    )

}
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172