Powershell format output.

Hello everyone.

Hope someone could offer some advice on the issue I am trying to solve. I have a PowerShell script that calls a sql script. The SQL scripts checks disk size on the list of servers in the .txt file.

This is the script.

#script accepts 1 input paramters i.e a generic sql file 
#iterates through the list of instances
#this is a potentially dangerous script be careful!!!
#Assumptions – SQLCMD available , security on SQL Server
#input parameters

param ([string] $inputfile =$(throw "input file name is required"))
#global variables

#to add to the outputfilename
$isodate=Get-Date -format s
$isodate=$isodate -replace(":","")
#variable for outputfile name , based on inputfile and isodate
$outputfile=$inputfile + $isodate + ".csv"
#list of sql server instances 1 on each line
#iterate through the list and invoke a SQLCMD , with some format information
foreach ($svr in get-content "C:\SQLSkills\PowerShell\instances.txt"){
$rep = $svr -replace("\\","_")
invoke-expression "SQLCMD -E -S $svr -i $inputfile  -s ',' -W -h-1 >> $outputfile"

May question that I hope someone can help me with is how do I format the output into a table format with the column headers that are created in the .sql file ?

I call the script by : ./general.ps1 -inputfile "Server_Disk_Space.sql"

Any help appreciated as I am really stuck here and been trying to fix it for a few days with no success. If the script needs changeing or could work better please let me know as I am keen to learn.

Thanks everyone.



No Data
  • Is it just generic disk size you are looking at, or actual SQL type information that specifically needs you to execute a SQL command? If it's the first, I don't see the point of using SQL.  If not, can you give the SQL script so we can getter a better idea what's actually happening?

    Also, you can use PowerShell & .NET for invoking SQL commands, and you can then fill a dataset with this information, which makes it a lot easier to format as required.


No Data