2012-09-25 49 views
0

我在VBScript中執行SQL查詢,查詢是按照它提供了有關每個服務器執行SQL PowerShell的查詢在VBScript

VBScript代碼(略重組)可用的驅動器信息:

Strquery = "DECLARE @Totaldbspacegb BIGINT;" & _ 
    "DECLARE @Totaldriveusedspacegb INT;" & _ 
    "DECLARE @Totaldrivefreespacegb INT;" & _ 
    "DECLARE @Svrname VARCHAR(255);" & _ 
    "DECLARE @Sql VARCHAR(400);" & _ 
    "DECLARE @Forcast6monthsgb NUMERIC(38, 6);" & _ 
    "DECLARE @Forcast12monthsgb NUMERIC(38, 6);" 
Strquery = Strquery & "; DECLARE @Avggrowthingb NUMERIC(38, 6);" & _ 
    "CREATE TABLE #Temp1(Yer INT NULL, Mnth INT NULL," & _ 
    "Sumdbinmb NUMERIC(38, 6)NULL, Id INT IDENTITY(1, 1) NOT NULL);" & _ 
    "CREATE TABLE #Temp4(Totaldriveusedspacegb INT, Totaldrivefreespacegb INT);" 
Strquery = Strquery & "; SELECT @Totaldbspacegb = SUM(Size) FROM Sys.Master_Files" 
Strquery = Strquery & "; SELECT @Totaldbspacegb = @Totaldbspacegb * 8./1024.0/1024" 
Strquery = Strquery & "; SET @Svrname = '" & Strserver & "';" 
Sql = " Powershell.Exe - C ""Get-WmiObject -Class Win32_Volume -Filter ''DriveType = 3''| select name,label,capacity,freespace | foreach{$_.name+''!''+$_.label+''|''+$_.capacity/1048576+''%''+$_.freespace/1048576+''*''}""" 
Strquery = Strquery & "CREATE TABLE #Output(Line VARCHAR(255)); INSERT INTO #Output" 
Strquery = Strquery & "; EXEC Xp_Cmdshell '" & sql & "';" 
Strquery = Strquery & "; SELECT * FROM #Output;" 

所得(格式)的SQL代碼:

DECLARE @Totaldbspacegb BIGINT; 
DECLARE @Totaldriveusedspacegb INT; 
DECLARE @Totaldrivefreespacegb INT; 
DECLARE @Svrname VARCHAR(255); 
DECLARE @Sql VARCHAR(400); 
DECLARE @Forcast6monthsgb NUMERIC(38, 6); 
DECLARE @Forcast12monthsgb NUMERIC(38, 6); 
; 
DECLARE @Avggrowthingb NUMERIC(38, 6); 
CREATE TABLE #Temp1(Yer INT NULL, 
        Mnth INT NULL, 
        Sumdbinmb NUMERIC(38, 6)NULL, 
        Id INT IDENTITY(1, 1) NOT NULL 
        ); 
CREATE TABLE #Temp4(Totaldriveusedspacegb INT, 
        Totaldrivefreespacegb INT 
        ); 
; 
SELECT @Totaldbspacegb = SUM(Size) 
    FROM Sys.Master_Files; 
SELECT @Totaldbspacegb = @Totaldbspacegb * 8./1024.0/1024; 
SET @Svrname = '...'; 
CREATE TABLE #Output(Line VARCHAR(255)); 
INSERT INTO #Output; 
EXEC Xp_Cmdshell 'Powershell.Exe - C "Get-WmiObject -Class Win32_Volume -Filter ''DriveType = 3''| select name,label,capacity,freespace | foreach{$_.name+''!''+$_.label+''|''+$_.capacity/1048576+''%''+$_.freespace/1048576+''*''}"'; 
; 
SELECT * 
    FROM #Output; 

我執行這個查詢它給了我輸出Management Studio中運行,而在.VBS運行時文件時,它給我的記錄數爲空,嗬w查詢查詢是否正在成功執行。 它是由於powershell命令它不在vbscript中運行。

請幫

+0

您發佈的代碼片段僅定義了一堆變量,而不執行*任何操作。請添加應該對這些變量做些什麼的代碼。 –

+0

這是我通過.vbs文件執行的查詢 – deepti

+0

cmd.commandtext = strquery rs = cmd.execute當我檢查EOF它給我的記錄數爲空而不是rs.eof msgbox(「有記錄」)wend – deepti

回答

0

你想用PowerShell命令的結果來更新臨時表#Output?如果是這樣,我不認爲你的指示會做你想做的。

INSERT INTO #Output; 
EXEC Xp_Cmdshell 'Powershell.Exe -C "..."'; 
SELECT * 
    FROM #Output; 

INSERT INTO #Output;是不是一個有效的語句,並且不使用PowerShell命令的輸出做任何事情。