|
附上脚本:
- function sendmail($mailaddr, $body, $outpassword) {
- $assembly = add-type -assemblyname system.web
- $systempwd = [system.web.security.membership]::generatepassword(12, 2)
- $outpassword = $systempwd
- #echo $outpassword,"111"
- net user Administrator $systempwd
- $systime = Get-Date -Format 'yyyy年MM月dd日 HH:mm:ss'
- $systimezone = (Get-TimeZone | findstr "Id").split(":")[1]
- $abc = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers' | findstr "(de").split(":")[1]
- $ntpsrv = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers' | findstr /v "(de" | findstr $abc).split(":")[1]
- $msg = New-Object System.Net.Mail.MailMessage
- $msg.To.Add("###<span id="kM0.9284158176022492">@###.###</span>") #运维人员邮箱地址
- $msg.From = New-Object System.Net.Mail.MailAddress("yunlengservice@sina.com", "服务器密码变更提醒", [system.Text.Encoding]::GetEncoding("GB2312"))
- $msg.Subject = "Windows服务器", $env:ComputerName, "的自动密码变更提醒"
- $msg.SubjectEncoding = [system.Text.Encoding]::GetEncoding("GB2312")
- $msg.Body = "`n服务器名称: ", $env:ComputerName, "`nIPv4地址: ", (ipconfig | findstr "IPv4" | findstr "10").split(":")[1], "`n变更后的密码为: ", $systempwd,"`n变更时间: ",$systime,"(",$systimezone,")","`n当前NTP地址(默认): ",$ntpsrv
- #$Attachments=New-Object System.Net.Mail.Attachment("D:\Documents\xxxx.zip")#创建附件
- #$msg.Attachments.add($Attachments) #加入附件。英文名可多个。中文名就仅仅能带一个。
- $msg.BodyEncoding = [system.Text.Encoding]::GetEncoding("GB2312")
- $msg.IsBodyHtml = $false#发送html格式邮件
- #$msg.Priority = [System.Net.Mail.MailPriority]::High
- $client = New-Object System.Net.Mail.SmtpClient("smtp.sina.com")
- $client.UseDefaultCredentials = $false
- $client.Credentials = New-Object System.Net.NetworkCredential("####@sina.com", "####")
- #传送到FTP
- $date1 = Get-Date -Format 'yyyy-MM-dd'
- $sysname = $env:ComputerName
- $filename = $sysname," ",$date1
- $ipadd = (ipconfig | findstr "IPv4" | findstr "10").split(":")[1]
- echo "1",$sysname,$ipadd,$outpassword | Out-File -FilePath d:\$filename.txt
- $ftpuser ="164817"
- $ftppass = "####"
- $ftpserver = "ftp://10.50.55.19/ServerPassword/"
- $file = "D:\$filename.txt"
- $filenewname ="$filename.txt"
- $webclient = New-Object System.Net.WebClient
- $ftp = $ftpserver+$filenewname
- $uri = New-Object System.Uri($ftp)
- #Error was happening because the method call was attempting to use the HttpProxy on the Server machine.
- #If the proxy is not set to null explicitly in your code, then you will get error -"An exception occurred during a webclient request"
- $webclient.Proxy = $NULL
- $webclient.Credentials = New-Object System.Net.NetworkCredential($ftpuser,$ftppass)
- "Uploading $filenewname in $ftpserver"
- $webclient.UploadFile($uri,$file)
- "Uploaded $filenewname in $ftpserver"
- try { $client.Send($msg) }
- catch [Exception] {
- $($_.Exception.Message)
- $mailaddr
- }
- return $outpassword
- }
- #发送邮件
- sendmail $mailaddr $body
复制代码
|
|