• 取硬盘大小及可用空间


    参考:http://www.mssqltips.com/tip.asp?tip=2444

    取硬盘大小及可用空间

    declare @svrName varchar(255)
    declare @sql varchar(400)
    --by default it will take the current server name, we can the set the server name as well
    set @svrName = case charindex('\',@@servernamewhen 0 then @@servername else 
    left(@@servername,charindex('\',@@servername)-1end
    set @sql = 'powershell.exe -c "Get-WmiObject -ComputerName ' + QUOTENAME(@svrName,''''+ ' -Class Win32_Volume -Filter ''DriveType = 3'' | select name,capacity,freespace | foreach{$_.name+''|''+$_.capacity/1048576+''%''+$_.freespace/1048576+''*''}"'
    --creating a temporary table
    print(@sql)
    CREATE TABLE #output
    (line 
    varchar(255))
    --inserting disk name, total space and free space value in to temporary table
    insert #output
    EXEC xp_cmdshell @sql
    --script to retrieve the values in MB from PS Script output
    select rtrim(ltrim(SUBSTRING(line,1,CHARINDEX('|',line) -1))) as drivename
          ,
    round(cast(rtrim(ltrim(SUBSTRING(line,CHARINDEX('|',line)+1,
          (
    CHARINDEX('%',line) -1)-CHARINDEX('|',line)) )) as Float),0as 'capacity(MB)'
          ,
    round(cast(rtrim(ltrim(SUBSTRING(line,CHARINDEX('%',line)+1,
          (
    CHARINDEX('*',line) -1)-CHARINDEX('%',line)) )) as Float),0as 'freespace(MB)'
    from #output
    where line like '[A-Z][:]%'
    order by drivename
    --script to retrieve the values in GB from PS Script output
    select rtrim(ltrim(SUBSTRING(line,1,CHARINDEX('|',line) -1))) as drivename
          ,
    round(cast(rtrim(ltrim(SUBSTRING(line,CHARINDEX('|',line)+1,
          (
    CHARINDEX('%',line) -1)-CHARINDEX('|',line)) )) as Float)/1024,0as 'capacity(GB)'
          ,
    round(cast(rtrim(ltrim(SUBSTRING(line,CHARINDEX('%',line)+1,
          (
    CHARINDEX('*',line) -1)-CHARINDEX('%',line)) )) as Float/1024 ,0)as 'freespace(GB)'
    from #output
    where line like '[A-Z][:]%'
    order by drivename
    --script to drop the temporary table
    drop table #output


    作者:nzperfect
    出处:http://www.cnblogs.com/nzperfect/
    引用或者转载本BLOG的文章请注明原作者和出处,并保留原文章中的版权信息。

  • 相关阅读:
    Qt之根据扩展名获取文件图标、类型
    C++根据扩展名获取文件图标、类型
    Qt之QFileIconProvider(根据扩展名获取文件图标、类型)
    Qt之QTemporaryFile
    Qt之QFileIconProvider
    Qt之字典划词
    Qt之滚动字幕
    Qt之QThread
    Python 安装 httplib2
    Qt之QTimer
  • 原文地址:https://www.cnblogs.com/nzperfect/p/2119659.html
Copyright © 2020-2023  润新知