Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 699 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
powershell testing a variable that hasnt being assign yet

#1
I want to test to see if a variable has been assigned a variable and if not perform action. How can this be achieve?

I've attempted it with the following code but receive the error: The right operand of '-is' must be a type.

$ProgramName is not assigned at this point.

If ($ProgramName -isnot $null) {
$ProgramName = $ProgramName + ', ' + $cncPrograms
}
Else {
If ($cncPrograms -isnot $null) {
$ProgramName = $cncPrograms
}
}
Reply

#2
Any unassigned variable will have a value of null, not a data type of null. So, just do this:

If ($ProgramName -ne $null)

...that will return `TRUE` if it's been assigned to a non-null value.

An even easier check to make is

IF($ProgramName)

Which will check if that is `$null` or not, though the logic is reversed, so you could use

IF(!$ProgramName)

**Edit:**

Ruffin raises a good point about strictmode in comments. This method will work as well:

`Test-Path variable:ProgramName` or `Test-Path variable:global:ProgramName` if it's explicitly global scoped, for instance. This will return `$true` or `$false` depending on if the variable exists.
Reply

#3
`Test-Path variable:\var` should do what you want, I guess.
Reply

#4
Contrary to answers above

Test-Path variable:ProgramName

Might not be what you are looking for because it only tests for the existence of the variable. If the Variable is set to $null it will still return $true.

Therefore in strictmode you may have to test for it's existence existence and whether it is non-empty.



Set-StrictMode -version Latest
#TODO Add a scope parameter
Function IsEmpty([string]$varname){
if (Test-path "variable:$varname"){
$val=(gi "variable:$varname").value
if ($val -is [bool]) {$false}
else {$val -eq '' -or $val -eq $null} }
else{ $true }
}

#TEST:
if (test-path variable:foobar){remove-variable foobar} ; IsEmpty foobar
$foobar=$null; IsEmpty foobar
$foobar=''; IsEmpty foobar;
$foobar=$false; IsEmpty foobar

#Results:
True
True
True
False

Strict mode kind of takes some of the fun out of scripting...

Reply

#5
To build on the (correct) answers from JNK and David, check out this great blog post from Jeffrey Snover that walks you through all the use cases of Boolean expressions in PoweShell. It concludes with how to test for the existence of a variable.

[To see links please register here]

Reply

#6
Aside from the fact that -isnot is for types, in powershell 7 there's a new operator for this purpose. The assignment will only happen if the left side is $null.

```
$programname ??= 'foo'
```
Reply

#7
-is or -isnot is to check to see the datatype the var is set to. For example:

$Test -is [String] -or $Test -isNot [Int]

An undeclared $var can be evaluated using {$Test -eq $null} will return True. If you nullify the $var anywhere in your code, you'll then need to evaluate differently.

I personally pre-set the $var's I am going to use in the script by nullifying them first.

[Array]$TestArray = @()
[String]$TestString = $null

Note that if you use [String]::Empty, and the var picks up any blank spaces this will not return True, it will be false.

$TestString -eq [String]::Empty

To evaluate a string if its empty and ignore any blank spaces use this method.

[string]::IsNullOrWhiteSpace($TestString)

To Evaluate if an array is empty, you have to use $var.Count.

$TestArray.Count -ge 1

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through