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
  • hi Tim

    thanks for the reply. This is the script I am calling.  Works fine in SSMS , and wanted to achieve the same across a list of servers via PowerShell.

    DECLARE @hr int
    DECLARE @fso int
    DECLARE @drive char(1)
    DECLARE @odrive int
    DECLARE @TotalSize varchar(20)
    DECLARE @MB bigint ; SET @MB = 1048576
    CREATE TABLE #drives (drive char(1) PRIMARY KEY,
    FreeSpace int NULL,
    TotalSize int NULL)
    INSERT #drives(drive,FreeSpace)
    EXEC master.dbo.xp_fixeddrives
    EXEC @hr=sp_OACreate 'Scripting.FileSystemObject',@fso OUT
    IF @hr <> 0 EXEC sp_OAGetErrorInfo @fso
    FOR SELECT drive from #drives
    ORDER by drive
    OPEN dcur
    FETCH NEXT FROM dcur INTO @drive
    EXEC @hr = sp_OAMethod @fso,'GetDrive', @odrive OUT, @drive
    IF @hr <> 0 EXEC sp_OAGetErrorInfo @fso
    EXEC @hr = sp_OAGetProperty @odrive,'TotalSize', @TotalSize OUT
    IF @hr <> 0 EXEC sp_OAGetErrorInfo @odrive
    UPDATE #drives
    SET TotalSize=@TotalSize/@MB
    WHERE drive=@drive
    FETCH NEXT FROM dcur INTO @drive
    CLOSE dcur
    EXEC @hr=sp_OADestroy @fso
    IF @hr <> 0 EXEC sp_OAGetErrorInfo @fso
    SELECT @@servername as ServerName, drive,
    FreeSpace as 'Free(MB)',
    TotalSize as 'Total(MB)',
    CAST((FreeSpace/(TotalSize*1.0))*100.0 as int) as 'Free(%)',
    GETDATE() as Date_Entered
    FROM #drives

No Data