Not getting output of power shell script

  • Hi,

    I am new to PowerShell, I found one script from internet to find out the version of SQL Server. I am I saved that script as a .ps1 extension, and executing from PowerShell prompt. The execution happens but no file output is created. Below is script code please sugges..Please avoid my basic mistakes since I am starting learning PowerShell for SQL. Thanks in advance.

    --------------------------------------------------------------------------------------------------

    # Check SQL version

    foreach ($svr in get-content "C:\data\AllServers.txt")

    {

    $con = "server=$svr;database=master;Integrated Security=sspi"

    $cmd = "SELECT SERVERPROPERTY('ProductVersion') AS Version, SERVERPROPERTY('ProductLevel') as SP"

    $da = new-object System.Data.SqlClient.SqlDataAdapter ($cmd, $con)

    $dt = new-object System.Data.DataTable

    $da.fill($dt) | out-null

    $svr

    $dt | Format-Table -autosize

    }

    --------------------------------------------------------------------------------------------------

  • Nothing in that script is directed to an output file. Modify the script to send the results to a file if that's what you want. (change C:\data\AllServersResults.txt to whatever and wherever you want)

    $svr | Out-File "C:\data\AllServersResults.txt"

    $dt | Format-Table -autosize | Out-File "C:\data\AllServersResults.txt" -Append

  • Hi Bruce

    I modified the script and tried to execute but no output file is being created in c:\ drive. Apologize if I am doing any silly mistake since I am new learner of PowerShell.

    foreach ($svr in get-content "C:\data\AllServers.txt")

    {

    $con = "server=$svr;database=master;Integrated Security=sspi"

    $cmd = "SELECT SERVERPROPERTY('ProductVersion') AS Version, SERVERPROPERTY('ProductLevel') as SP"

    $da = new-object System.Data.SqlClient.SqlDataAdapter ($cmd, $con)

    $dt = new-object System.Data.DataTable

    $da.fill($dt) | out-null

    $svr | Out-File "C:\Test.txt"

    $dt | Format-Table -autosize | Out-File "C:\Test.txt" -Append

    }

  • Sorry Bruce I was committing one mistake I tried like [PS C:\> .\filename] and it worked. Thanks for your help. 🙂

  • I'm not sure I understand what you changed or why your previous code didn't work. If you want to send any error messages from your prior version maybe we can work out the issues.

    Either way, I'm glad it's working for you now.

Viewing 5 posts - 1 through 4 (of 4 total)

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