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
255 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

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.

Question has a verified solution.

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

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
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 …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 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

27 Experts available now in Live!

Get 1:1 Help Now