Powershell I need help

  • tedo

    SSCrazy

    Points: 2229

    Here is my code

    $a = get-content "C:\CheckSQLServer\sql\SQLTokyo.txt"

    do

    {

    $a = Read-Host "Please enter the SQL Server Instance name"

    if ($a -ne "")

    {$a + ""; osql -S $a -E}

    }

    while ($a -ne "q")

    break

    end

    However, I would like it so it gives me a list of servers if say I type in odb as the instance i want it to bring back any thing with that in it and then log me in to the number I choose.

    This one below does it for my sybase servers but I cant login to my mssql servers through unix so I would like to do the same thing in powershell.

    Regards.

    Terry

    select SERVER in `grep -i $1 server_list.txt | nawk 'OFS=":" {print $2,$1,$3,$7,$8}'`

    do

    RHOST=`echo $SERVER | nawk -F: '{print $3}'`

    DB=`echo $SERVER | nawk -F: '{print $2}'`

    RDBMS=`grep $DB /server_list.txt | awk '{print $6}'|tr [:upper:] [:lower:] | uniq`

    echo logging into $RHOST as $RDBMS

    $RDBMS $RHOST

    break

    done

  • terryj30

    SSCommitted

    Points: 1866

    This should be what you need

    $file = get-content "G:\Infrastructure\DBA\NewDocs\Terry Powershell\SQLTokyo.txt

    # checks file if not found then terminates

    if (-not($file))

    {

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

    exit

    }

    # matches the string ie type in IBTKYPSP

    $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

    }

    }

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply