TechTalkz.com Logo

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech World > Technical Discussions

Reply
 
LinkBack Thread Tools Display Modes
Old 01-08-2008, 11:03 AM   #1
Newbie
 
Join Date: Aug 2008
Age: 31
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0 ted0 is an unknown quantity at this point
I need help with my powershell script

What I am trying to do is search from a file that I have created with a number of my sql server instances. I would like to grep-search this file for say a string. This string might me IBTKYPSP, is so I would like it to bring back all the servers with that string. I would then like it to ask me what server would you like to log in to. I could then press 1 2 or 3 or so on depending on how many servers it shows me. then it would osql or sqlcmd on to that server.
here is my code so far. Thanks if you can help me. regards,

Terry
$file = "C:\CheckSQLServer\sql\SQLTokyo.txt"
$match = $file | Select-String (Read-Host "search string")
if ($match.count -gt 1)
$count=0
{
foreach ($i in $match)
$count = $count + 1
$file = read-host "Please enter a number from the list"
{ $match+ ""; sqlcmd -S $ANS -E}

}
ted0 is offline   Reply With Quote
Old 01-08-2008, 11:15 PM   #2
ƒ(ψ)=ΘΊΧφ
 
bakuryu's Avatar
 
Join Date: May 2006
Location: India
Age: 22
Posts: 4,803
Thanks: 12
Thanked 373 Times in 344 Posts
Rep Power: 67 bakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant future
Send a message via Yahoo to bakuryu
Re: I need help with my powershell script

can you attach the txt file with some dummy instances ??
__________________
Please don't click here
bakuryu is offline   Reply With Quote
Old 02-08-2008, 04:12 AM   #3
Newbie
 
Join Date: Aug 2008
Age: 31
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0 ted0 is an unknown quantity at this point
Re: I need help with my powershell script

Instances will be IBTKYPSP32A034 IBTKYPSP32B034\HOSTED_P1 IBTKYPSP32A001, IBTKYPSP32A002.
I would like this listed.
T
ted0 is offline   Reply With Quote
Old 02-08-2008, 08:40 PM   #4
ƒ(ψ)=ΘΊΧφ
 
bakuryu's Avatar
 
Join Date: May 2006
Location: India
Age: 22
Posts: 4,803
Thanks: 12
Thanked 373 Times in 344 Posts
Rep Power: 67 bakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant future
Send a message via Yahoo to bakuryu
Re: I need help with my powershell script

I don't know much about powershell, so I am not sure whether there is something inbuilt to parse the string, so I just written one -- and I don't like the foreach too i prefer the normal for loop

Code:
$file=read-host "Enter filename : "
if (-not(test-path $file)) 
{
    write-error($file+ " not found. Terminating .... ");
    exit
}
$match = Select-String -path $file -pattern (Read-Host "search string") -AllMatches
if ($match.count -gt 0) 
{
    $inst = @()
    for ($i = 0; $i -lt $match.count; $i++) 
    {
        $temp = ""
        $line = $match[$i].tostring()
        for ($j = $line.length-1; $j -ge 0; $j--) 
        {
            if ($line[$j] -eq ":") 
            {
                break
            }
            $temp = $line[$j] + $temp
        }
        
        $inst = $inst + $temp
        write-output ("Press "+($i+1)+" for : " + $inst[$i])
    }
    
    $inp = ""
    $flag = "true"
    do {
        $inp = read-host("Enter choice : 1 - "+$match.count)
        if ($inp -lt 1 -or $inp -gt $match.count) 
        {
            $flag = "false";
            write-error ("Invalid entry. Enter between 1 - "+$match.count)
        }
        else {
            $flag = "true";
        }
    } while ($flag -eq "false")
    
    write-output ("Do somethign with instance : "+$inst[$inp-1])
    
}
You need to have the instances in separate lines for this work like :
Code:
IBTKYPSP32A034 
IBTKYPSP32B034\HOSTED_P1 
IBTKYPSP32A001
IBTKYPSP32A002
bakuryu is offline   Reply With Quote
Old 04-08-2008, 05:51 PM   #5
Newbie
 
Join Date: Aug 2008
Age: 31
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0 ted0 is an unknown quantity at this point
Re: I need help with my powershell script

cheers for your help, I only had to change a few things, it works good, some times it wont log in but I can try to iron out these problems.
Cheers again ted
ted0 is offline   Reply With Quote
Old 04-08-2008, 08:08 PM   #6
ƒ(ψ)=ΘΊΧφ
 
bakuryu's Avatar
 
Join Date: May 2006
Location: India
Age: 22
Posts: 4,803
Thanks: 12
Thanked 373 Times in 344 Posts
Rep Power: 67 bakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant future
Send a message via Yahoo to bakuryu
Re: I need help with my powershell script

Glad it worked for you Infact that was the very first powershell script I wrote. Looks very much like the Linux Shell scripting.
bakuryu is offline   Reply With Quote
Reply

Thread Tools
Display Modes


Google
 


All times are GMT +5.5. The time now is 04:48 AM.


vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2008, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional