0

我在 vbscript 中执行 sql 查询,查询如下,它提供了有关每个服务器可用驱动器的信息

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;

我正在执行这个查询,它在 .vbs 文件中运行时从管理工作室运行时给了我输出,它给我的记录计数为空,如何查找查询是否成功执行。是不是由于powershell命令它没有在vbscript中运行。

请帮忙

4

1 回答 1

0

是否要#Output使用 PowerShell 命令的结果更新临时表?如果是这样,我认为您的指示不会满足您的要求。

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

INSERT INTO #Output;不是有效的语句,并且您不会对 PowerShell 命令的输出执行任何操作。

于 2012-09-26T17:42:47.607 回答