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
254 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
 
LVL 2

Author Comment

by:Hankinater
ID: 39612195
I still get a blank line.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now