We currently on each SQL server we have a SQL agent job setup that is using a SQL stored procedure to check disk space, and raise a SQL alert to notify DBA, the stored procedure is like below:
I wonder if in Powershell, if it can raise a sql alert. The alerts has been already setup.
Basically I would like to use powershell to do something similar in the below stored procedures by Jasper simith. The reason we want to replace it is we would like to eliminate the use of sp_OACreate, which could be a security issue.
Thanks,
The stored procedure is like below:
CREATE PROCEDURE [dbo].[usp_diskspace] @PercentFreeThreshold_Low int,
@PercentFreeThreshold_Critical int
AS
/***************************************************************************************/
/* Original Code was posted on:
http://www.sqldbatips.com/showcode.asp?ID=4 */
/* AUTHOR DATE PURPOSE
*/
/*
*/
/* Jasper Smith 5/14/2003 This procedure returns the amount of free */
/* (SQL Server MVP) space (in MB) on all fixed disks on the */
/* SQL Server using xp_fixeddrives
but builds on */
/* that by using the FileSystemObject
to get the */
/* total drive space and calculate
the free */
/* space percentage.
*/
/*
*/
/************************CHANGE HISTORY*************************************************/
SET NOCOUNT ON
DECLARE @hr int
DECLARE @fso int
DECLARE @drive char(1)
DECLARE @odrive int
DECLARE @TotalSize varchar(20)
DECLARE @PercentFree int
DECLARE @GB bigint ;
SET @GB = 1073741824 -- conversion factor for bytes to gigs
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
DECLARE dcur CURSOR LOCAL FAST_FORWARD
FOR SELECT drive from #drives
ORDER by drive
OPEN dcur
FETCH NEXT FROM dcur INTO @drive
WHILE @@FETCH_STATUS=0
BEGIN
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/@GB
WHERE drive=@drive
SELECT @PercentFree = CAST(((FreeSpace*0.00098 )/TOTALSIZE)*100 AS int)
FROM #drives
WHERE drive=@drive
IF @PercentFree < @PercentFreeThreshold_Critical
RAISERROR (50001, -- Message ID.
10, -- Severity,
1, -- State,
@drive,-- First argument (%s).
@PercentFreeThreshold_Critical)-- Second argument %i.
WITH Log
ELSE
--Only trigger this alert once for any day so we don't flood email inbox
IF @PercentFree < @PercentFreeThreshold_Low
AND NOT EXISTS
(SELECT * FROM msdb..sysalerts WHERE message_id =50002 AND
SUBSTRING(CONVERT(char(10),last_occurrence_date),5,2)+ '/'
+ RIGHT(last_occurrence_date,2)+ '/'
+ LEFT(last_occurrence_date,4)
= CONVERT(varchar(10),GetDate(),101))
RAISERROR (50002, -- Message ID.
10, -- Severity,
1, -- State,
@drive,-- First argument (%s).
@PercentFreeThreshold_Low)-- Second argument %i.
WITH Log
FETCH NEXT FROM dcur INTO @drive
END
CLOSE dcur
DEALLOCATE dcur
EXEC @hr=sp_OADestroy @fso
IF @hr= 0 EXEC sp_OAGetErrorInfo @fso
SELECT drive as 'Drive',
CAST(FreeSpace*0.00098 as int) as 'Free(GB)',
TotalSize as 'Total(GB)',
CAST(((FreeSpace*0.00098 )/TOTALSIZE)*100 AS int) as 'Free(%)'
FROM #drives
ORDER BY drive
/****** END Create Object: Stored Procedure usp_diskspace ******/
GO