直接运行ps1脚本邮件无法发送,复制到命令框可以发送


PowerShell交流中心分类: Powershell基础直接运行ps1脚本邮件无法发送,复制到命令框可以发送
0
jtndnlm asked 7 年 ago

# 说明:此脚本检测当前目录list.txt文档内的服务器
# 报错也继续
$ErrorActionPreference = “stop”;
 

 
# 设置警告和紧急的阈值
$percentWarning = 70;
$percentCritcal = 10;
 

 # 邮件接收人
  $users = “107426@aa.com”
    
  #$users = “you@company.com”, “manager@company.com”, “etc@company.com”;  # 更多接收人用这格式
 

 # 报告路径
  $reportPath = “C:\Reports\”;
 
 # 报告名称
  $reportName = “磁盘警告_$(get-date -format yyyyddMM).html”;
 
# 路径名称组合
$diskReport = $reportPath + $reportName
 
#设置颜色和背景
$redColor = “#FF0000”
$orangeColor = “#FBB917”
$whiteColor = “#FFFFFF”
 
# 小于1不发送报告
$i = 0;
 
# 从txt里加载需要检测的主机名
$computers = Get-Content “C:\list.txt”;
$datetime = Get-Date -Format “MM-dd-yyyy_HHmmss”;

# 有新数据出现,删除原报告
If (Test-Path $diskReport)
    {
        Remove-Item $diskReport
    }
 
# 删除旧文件
$Daysback = “-7”
$CurrentDate = Get-Date;
$DateToDelete = $CurrentDate.AddDays($Daysback);
Get-ChildItem $reportPath | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item;
 
# 创建HTML报告
$titleDate = get-date -uformat “%Y-%m-%d- – %A”
$header = ”
  <html>
  <head>
  <meta http-equiv=’Content-Type’ content=’text/html; charset=iso-8859-1′>
  <title>DiskSpace Report</title>
  <STYLE TYPE=’text/css’>
  <!–
  td {
   font-family: Tahoma;
   font-size: 11px;
   border-top: 1px solid #999999;
   border-right: 1px solid #999999;
   border-bottom: 1px solid #999999;
   border-left: 1px solid #999999;
   padding-top: 0px;
   padding-right: 0px;
   padding-bottom: 0px;
   padding-left: 0px;
  }
  body {
   margin-left: 5px;
   margin-top: 5px;
   margin-right: 0px;
   margin-bottom: 10px;
   table {
   border: thin solid #000000;
  }
  –>
  </style>
  </head>
  <body>
  <table width=’100%’>
  <tr bgcolor=’#CCCCCC’>
  <td colspan=’7′ height=’25’ align=’center’>
  <font face=’tahoma’ color=’#003399′ size=’4′><strong>Warning $titledate</strong></font>
  </td>
  </tr>
  </table>

 Add-Content $diskReport $header
 
# 创建报告表格属性
 $tableHeader = ”
 <table width=’100%’><tbody>
 <tr bgcolor=#CCCCCC>
    <td width=’10%’ align=’center’>server name</td>
 <td width=’5%’ align=’center’>ID</td>
 <td width=’10%’ align=’center’>ALL(GB)</td>
 <td width=’10%’ align=’center’>Used(GB)</td>
 <td width=’10%’ align=’center’>Free(GB)</td>
 <td width=’5%’ align=’center’>Percent %</td>
 </tr>

Add-Content $diskReport $tableHeader
 
# 检测剩余空间
  foreach($computer in $computers)
 {  
 $disks = Get-WmiObject -ComputerName $computer -Class Win32_LogicalDisk -Filter “DriveType = 3”
 $computer = $computer.toupper()
  foreach($disk in $disks)
 {         
  $deviceID = $disk.DeviceID;
        $volName = $disk.VolumeName;
  [float]$size = $disk.Size;
  [float]$freespace = $disk.FreeSpace;  
  $percentFree = [Math]::Round(($freespace / $size) * 100, 2);
  $sizeGB = [Math]::Round($size / 1073741824, 2);
  $freeSpaceGB = [Math]::Round($freespace / 1073741824, 2);
        $usedSpaceGB = $sizeGB – $freeSpaceGB;
        $color = $whiteColor;
 
# 设置警告背景颜色
 if($percentFree -lt $percentWarning)       
  {
    $color = $orangeColor  
 
# 设置紧急的背景颜色
      if($percentFree -lt $percentCritcal)
        {
        $color = $redColor
       }         
 
 # 创建日期
    $dataRow = ”
  <tr>
        <td width=’10%’>$computer</td>
  <td width=’5%’ align=’center’>$deviceID</td>
  <td width=’10%’ align=’center’>$sizeGB</td>
  <td width=’10%’ align=’center’>$usedSpaceGB</td>
  <td width=’10%’ align=’center’>$freeSpaceGB</td>
  <td width=’5%’ bgcolor=`’$color`’ align=’center’>$percentFree</td>
  </tr>

Add-Content $diskReport $dataRow;
Write-Host -ForegroundColor DarkYellow “$computer $deviceID percentage free space = $percentFree”;
    $i++   
  }
 }
}
 
# 报告末尾总结颜色设置
 $tableDescription = ”
 </table><br><table width=’20%’>
 <tr bgcolor=’White’>
    <td width=’10%’ align=’center’ bgcolor=’#FBB917′>Warning 15% </td>
 <td width=’10%’ align=’center’ bgcolor=’#FF0000′>MUST 10% </td>
 </tr>

  Add-Content $diskReport $tableDescription
 
 Add-Content $diskReport “</body></html>”

# 设置发送属性
# Send Notification if alert $i is greater then 0
if ($i -gt 0)
{
    foreach ($user in $users)
{
        Write-Host “Sending Email notification to $user”
   
  $smtpServer = “192.168.1.4”
  $username = “107426@aa.com”
  $password = “******”  
  $SmtpPort = “25”
  $smtp = New-Object Net.Mail.SmtpClient($smtpServer, $SmtpPort)
  $smtp.Credentials = New-Object System.Net.NetworkCredential($username, $password)
  $msg = New-Object Net.Mail.MailMessage
  $msg.To.Add($user)
  $msg.From = “107426@aa.com”
  $msg.Subject = “磁盘警告 $computer $titledate”
  $msg.IsBodyHTML = $true
  $msg.Body = get-content $diskReport
  $smtp.Send($msg)
        $body = “”
    }
  }
 
 

Mooser Lee 管理员 replied 7 年 ago

具体报什么错误?
发送邮件的代码有没有被执行?
能否把发送邮件代码直接保存成ps1运行一下,比如给某个账号发送一封test邮件。

Mooser Lee 管理员 replied 7 年 ago

具体报什么错误?
发送邮件的代码有没有被执行?
能否把发送邮件代码直接保存成ps1运行一下,比如给某个账号发送一封test邮件。

jtndnlm replied 7 年 ago

直接运行 和 保存成PS1 运行都不报错, 但是运行ps1的时候 邮箱里面接收不到邮件, 但是直接运行代码就可以

jtndnlm replied 7 年 ago

直接运行 和 保存成PS1 运行都不报错, 但是运行ps1的时候 邮箱里面接收不到邮件, 但是直接运行代码就可以

jtndnlm replied 7 年 ago

我把邮件部分的代码 保存成ps1 直接运行 也不保存, 但是邮件还是无法发出去, 但是直接贴到powershell 窗口中 或者在窗口中运行ps1 邮件可以发送,双击运行ps1 就不行

wu_dtc replied 7 年 ago

如果你的$smtpserver = 192.168.1.4就是@company.com域的邮件服务器的话,给@company.com域的邮箱发邮件是不用提供用户名和密码的。如果是这种情况,把用户名和密码去掉试试。

jtndnlm replied 7 年 ago

去掉密码也试过了 , 直接运行不生效, 贴到命令框里生效, 我邮箱 jtndnlm@163.com, 哪位大神能联系下我?

jtndnlm replied 7 年 ago

去掉密码也试过了 , 直接运行不生效, 贴到命令框里生效, 我邮箱 jtndnlm@163.com, 哪位大神能联系下我?

1 Answers
1
jtndnlm answered 7 年 ago

脚本内容复制到powershell 中可以运行, 我默认把ps1 文件用powershell打开,  执行磁盘检测的部分可以运行,邮件无法发送,求助高人指点。