ITguy565
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.
Here was my attempt that was less than successful :
Where did I go wrong with this?
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
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
Where did I go wrong with this?
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
Can you specify more better your target?
ASKER
oBdA that was exactly what I was looking for. @Ermanno, thanks for presenting options on this
I'm glad you managed to solve!
try
{
$null = Get-Variable -Scope Global -Name VariableName -ErrorAction Stop
$variableExists = $true
}
catch
{
$variableExists = $false
}