Link to home
Start Free TrialLog in
Avatar of ITguy565
ITguy565Flag for United States of America

asked on

Powershell scripting Assistance : Testing Variable Creation

Experts,

What is the easiest way to acomplish the following:

I am looking at turning the following working code into a reusable function.

Function invoke-varcheck2(){
        if ($services.count -gt "200"){
            write-host "Check was successful"
        }else {
            write-host "Variable was not created successfully"
        }
    }
    . invoke-varcheck2

Open in new window



Here was my attempt that was less than successful :

Function invoke-varcheck($varname, $count){
    write-host "Attempting to Test Variable $varname"
        if (($+"$(($varname).count)" -gt $count){ 
            write-host "Check was successful"
        }else {
            write-host "Variable was not Created Successfully"
        }

}
invoke-varcheck -varname services -count 200

Open in new window


Where did I go wrong with this?
Avatar of DevAdmin
DevAdmin
Flag of Italy image

I do not sure if I well understand want you need, but you are already consider to use the cmdlet Get-Variable:

try
{
    $null = Get-Variable -Scope Global -Name VariableName -ErrorAction Stop
    $variableExists = $true
}
catch
{
    $variableExists = $false
}
Avatar of ITguy565

ASKER

@Ermanno,

Thanks for the reply, get-variable would indeed tell me if the variable exists, but I need to go deeper than that and get an exact count of what is inside it in order to verify validity of the data.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Get-Variable can also return an object that match the name of a variabile that you specify... so you can then analize the object

Can you specify more better your target?
oBdA that was exactly what I was looking for.  @Ermanno, thanks for presenting options on this
I'm glad you managed to solve!