Solved

PowerShell: Couple things - Query Multiple Location, Stop once item is found and how to reference the result in other scripts.

Posted on 2013-10-30
9
256 Views
Last Modified: 2013-10-30
Hey Experts,

I have a script that searches several BIG-IP Load Balancers for a specific entry.  The script brings back the correct information, but I want it to stop searching once the entry is found.

The script takes the input from the command line and pings the site, using the IP address later in the script.

If it finds the entry on the first F5, I don't want it to continue further.  Here's what the script looks like:

$server=$args[0]
$ping = new-object System.Net.NetworkInformation.Ping
$rslt = $ping.send($server)
$ip_Address = $rslt.Address.IPAddressToString

$F5List = @("dmz","intranet","webacc","wa1","wa2","wa3")
foreach($F5 in $F5List){
C:\Temp\PowerShell\F5VSDefPool.ps1 $F5 <username> <password> $ip_Address
}

Open in new window


It gets me the default pool name and the F5 it found it on, like this:

IIS_farm_pool, dmz

I will feed that information into a separate script to get members (servers) of the pool.  If I want to use the first column as pool name and second column as F5 name, what would the syntax look like.  I'm stuck because the result doesn't have column labels.
0
Comment
Question by:Hankinater
  • 5
  • 4
9 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 39611681
If I understood you correctly, the following code should work for you.. :-)

$server=$args[0]
$ping = new-object System.Net.NetworkInformation.Ping
$rslt = $ping.send($server)
$ip_Address = $rslt.Address.IPAddressToString

$F5List = @("dmz","intranet","webacc","wa1","wa2","wa3")
foreach($F5 in $F5List){
#save result of the script to a var
$Result = (C:\Temp\PowerShell\F5VSDefPool.ps1 $F5 <username> <password> $ip_Address) -split ","

#Ourput the result with header
New-Object PSObject -Property @{
poolname = $Result[0]
F5name = $Result[1]
}

#Exit the script if poolname is equal to IIS_farm_pool
If ($Result[0] -eq "IIS_farm_pool"){ Break }

}

Open in new window

0
 
LVL 2

Author Comment

by:Hankinater
ID: 39612060
Subsun,

Thanks for your response.

The problem with your solution is that it assumes that the Result[0] is always equal to "IIS_farm_pool", and there hundreds of pools.  So I tried to capture the poolname in a variable, however it only works when it is on the first F5 in the list.

Also, depending on how many of F5s it searches it will insert a blank line for each F5 it doesn't find the entry on.  So if the entry is on the second F5 it writes the following:

poolname            F5name
--------------            -----------

IIS_farm_pool     intranet
Instead of

poolname            F5name
--------------            -----------
IIS_farm_pool     intranet
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39612153
just before line 9 try adding $Result = $null
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 2

Author Comment

by:Hankinater
ID: 39612195
I still get a blank line.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39612525
Blank line in output file? or on the screen?
0
 
LVL 2

Author Comment

by:Hankinater
ID: 39612530
On the screen.  It creates a blank line for each F5 it was unable to find the entry on.
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 39612541
I just added a line to skip the result if the result is null...
Try..
$server=$args[0]
$ping = new-object System.Net.NetworkInformation.Ping
$rslt = $ping.send($server)
$ip_Address = $rslt.Address.IPAddressToString

$F5List = @("dmz","intranet","webacc","wa1","wa2","wa3")
foreach($F5 in $F5List){
#save result of the script to a var
$Result = $null
$Result = (C:\Temp\PowerShell\F5VSDefPool.ps1 $F5 <username> <password> $ip_Address) -split ","

If ($Result -ne $null {
	#Ourput the result with header
	New-Object PSObject -Property @{
	poolname = $Result[0]
	F5name = $Result[1]
	}

	#Exit the script if poolname is equal to IIS_farm_pool
	If ($Result[0] -eq "IIS_farm_pool"){ Break }
}
}

Open in new window

0
 
LVL 2

Author Closing Comment

by:Hankinater
ID: 39612588
That did it!!

Thanks so much for your time on this - if I could give you more points, I would!

Hank
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39612600
No problem, Glad I could help!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question