![]() |
|
|
#1 |
|
Newbie
Join Date: Aug 2008
Age: 31
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
![]() |
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} } |
|
|
|
|
|
#2 |
|
ƒ(ψ)=ΘΊΧφ
![]() |
Re: I need help with my powershell script
can you attach the txt file with some dummy instances ??
__________________
Please don't click here |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Aug 2008
Age: 31
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
![]() |
Re: I need help with my powershell script
Instances will be IBTKYPSP32A034 IBTKYPSP32B034\HOSTED_P1 IBTKYPSP32A001, IBTKYPSP32A002.
I would like this listed. T |
|
|
|
|
|
#4 |
|
ƒ(ψ)=ΘΊΧφ
![]() |
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])
}
Code:
IBTKYPSP32A034 IBTKYPSP32B034\HOSTED_P1 IBTKYPSP32A001 IBTKYPSP32A002 |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Aug 2008
Age: 31
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#6 |
|
ƒ(ψ)=ΘΊΧφ
![]() |
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. |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|