我在powershell上有一个脚本,当它运行时,它会发送一封包含服务器状态详细信息的电子邮件,并在屏幕上打印信息
我现在正在寻找添加到脚本中,除了发送电子邮件并在屏幕上打印信息外,它还将结果保存在记事本上并将其保存在共享驱动器上的文件夹中
我是Powershell的新手,所以不确定我该怎么做。如果需要,我可以添加我的代码
谢谢
操作系统
代码是
$DirectoryPath = Split-Path $MyInvocation.MyCommand.Path
$ConfigurationPath = $DirectoryPath + 'Config file.xml'
Function FormatCell
{
param($cellValue)
$cell = "<td>" + $cellValue + "</td>"
return $cell
}
Function Getservicechecker
{
[xml]$ConfigFile = Get-Content $ConfigurationPath
$Servers = $ConfigFile.SelectNodes('/Configs/Servers/Server')
$ServString = "<tr><th>Server</th><th>IP Address</th><th>Process</th> <th>Status</th></tr>"
foreach($Server in $Servers)
{
[string]$serverName = $Server.ServerName
$OutputText += "Server: " + $serverName + $NL
$Process = $Server.ProcessesToMonitor
foreach($Processtomonitor in $Process.ChildNodes)
{
$ServString += "<tr>"
$ServString += FormatCell -cellValue $serverName
$ipaddress = Test-Connection $serverName -count 1 | select Ipv4Address
$ipAddressValue = $ipaddress.IPV4Address.IPAddressToString
$servString += FormatCell -cellValue $ipAddressValue
[string]$processName = $Processtomonitor.InnerText
$servicestatus = Get-service -ComputerName $serverName -Name $processName | select status
$ServString += FormatCell -cellValue $processName
$FormatedStatus = 'status'
[string]$statusString = -join $servicestatus
$statusString = $statusString.Remove(0,"@{Status=".Length)
$statusString = $statusString.Remove($statusString.IndexOf("}"))
$FormatedServiceStatus = FormatCell -cellValue $servicestatus
If ($FormatedServiceStatus –eq “<td>@{Status=Running}</td>”)
{
$Formatedstatus = 'Running'
}
elseif ($FormatedServiceStatus –eq “<td>@{Status=Stopped}</td>”)
{
$Formatedstatus = "<p style='color:red'>Service stopped investigation required</p>"
}
else
{
$FormatedStatus = "$servicestatus potential investigation"
}
Write-host "server: $serverName `t ipaddress: $ipAddressValue `t process: $processName `t status: $statusString"
$ServString += FormatCell -cellValue $Formatedstatus
$ServString += "</tr>"
}
}
return "<table class=""gridtable"">" + $ServString + "</table>"
}
function SendMail
{
param($bodyText, $subject )
$SmtpServer = "164.134.84.81"
$emailMessage = New-Object System.Net.Mail.MailMessage
$fromaddress = "osman.test.farooq@gmail.com"
$recipients = ("osman.farooq@atos.net")
$Subject = "BOXI Servers Report for " +$dateTimeOfServiceCheck
$body = "<HTML><HEAD><META http-equiv=""Content-Type"" content=""text/html; charset=iso-8859-1"" /><TITLE></TITLE>"
$body += "<style type=""text/css"">table.gridtable {font-family: verdana,arial,sans-serif;font-size:11px;color:#333333;border-width: 1px;border-color: #666666;border-collapse: collapse;}"
$body += "table.gridtable th {border-width: 1px;padding: 8px;border-style: solid;border-color: #666666; background-color: #dedede;}"
$body += "table.gridtable td {border-width: 1px;padding: 8px;border-style: solid;border-color: #666666; background-color: #ffffff;}</style></HEAD>"
$body += "<BODY bgcolor=""#FFFFFF"" style=""font-size: Small; font-family: TAHOMA; color: #000000"">"
$body += $bodyText
Send-MailMessage -to $recipients -subject $subject -bodyashtml -body $body - from $fromAddress -SmtpServer $smtpServer -Port 25
}
function main
{
$dateTimeOfServiceCheck = Get-Date -Format F
$outputText = "Service Checker :" + $dateTimeOfServiceCheck
$emailBody = "<h3>"+$outputText +"</h3>" #take out if dont want datetime above table
$emailBody += Getservicechecker
SendMail $emailBody "BOXI Servers Report for" +$dateTimeOfServiceCheck
}
main
Trevor的回答涵盖了写得很好的文件。写入文件后,您可以使用Invoke-Item
在默认应用程序中打开它:
$myPath = 'C:myfile.txt'
$information | Set-Content -Path $myPath
$mypath | Invoke-Item
在PowerShell中写入文件的方法有很多。您使用哪一个完全取决于您!
- 使用
Set-Content
命令 - 使用
Out-File
命令 - 使用
[System.IO.File]::WriteAllText()
方法
正如 Mathias 在注释中提到的,Tee-Object
命令可用于写入文件并同时将其发送到管道,或将内容写入变量。可能还有更多的方法。