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
259 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
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

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

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
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 …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

792 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