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
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

729 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