• I am pretty much new to powershell.

    I saw this script is quite useful for my environment. If someone let me know how to use this.

    I tried couple of time. But everytime i ran the below script, it terminates automatically but i have the text file in D drive.

    Please correct me if i am wrong...

    I created a file in D:\sqlList.txt

    then,

    Run> Powershell> enter

    PS D:\ document and setting\abc cd PS D:\gci

    -a --- _________________

    -a --- 6/21/2010 10.20am 11 sqlList.txt

    #copy the code and paste it in the window via right click

    PS D:\$file = get-content "sqlList.txt"

    # checks file if not found then terminates

    if (-not($file))

    {

    write-error($file+ " not found. Terminating .... ");

    exit

    }

    # matches the string ie type in any string for yout instance

    $match = $file | Select-String (Read-Host "search string")

    # if the match is found then this will run the select

    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 ("Logging on to the SQLSERVER you requested: "+$inst[$inp-1])

    if ($inst[$inp-1] -ne "") {

    $inst[$inp-1] + ""; sqlcmd -S $inst[$inp-1] -E

    }

    }

    --If i try to type all the code in powershell window then at the end how to execute this code...

    I think i am doing some silly mistake, but please help me to get it over...

    Thanks in advance....

    A Singh