zoukankan      html  css  js  c++  java
  • Powershell 磁盘使用空间监控,并使用dbmail发送邮件

    $server = "(local)"
    $uid = "sa"
    $db="master"
    $pwd="fanzhouqi"
    $mailprfname = "sina"
    $recipients = "XXXX@qq.com"
    $subject = 'disk usage monitor'
    function sendmail($message)
    {
        $SqlConnection = New-Object System.Data.SqlClient.SqlConnection 
        $CnnString ="Server = $server; Database = $db;User Id = $uid; Password = $pwd" 
        $SqlConnection.ConnectionString = $CnnString 
        $CC = $SqlConnection.CreateCommand(); 
        if (-not ($SqlConnection.State -like "Open")) { $SqlConnection.Open() } 
        
        $cc.CommandText=" EXEC msdb..sp_send_dbmail 
                @profile_name  = '$mailprfname'
                ,@recipients = '$recipients'
                ,@body = '$message'
                ,@subject = '$subject'
    " 
        $cc.ExecuteNonQuery()|out-null 
        $SqlConnection.Close();
    }
    
    $output = Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" -ComputerName . |  Format-Table deviceid,@{Label="free(GB)"; Expression={$_.freespace/1024/1024/1024}},@{Label="size(GB)"; Expression={$_.size/1024/1024/1024}} -AutoSize | Out-String
    sendmail $output

    并创建计划任务执行:

    bat 脚本 powershell.exe -File "F:\Disk Usage Monitor.ps1" -nointeractive

  • 相关阅读:
    软件工程—附加作业
    软件工程最终总结
    电梯调度(两人结对)
    VS单元测试
    第二周作业(2,3题)
    VS的安装
    补救
    漂亮男孩不说谎
    博客带我成长
    Java后缀数组-求sa数组
  • 原文地址:https://www.cnblogs.com/Amaranthus/p/2536296.html
Copyright © 2011-2022 走看看