在powershell上,如何将脚本保存为共享文件夹中的文本文件,以及如何在屏幕上打印和发送电子邮件
我在powershell上有一个脚本,它运行时会发送一封包含服务器状态详细信息的电子邮件,并在屏幕上打印信息 我现在正在寻找添加到脚本,以及发送电子邮件和打印屏幕上的信息,它还保存在记事本上的结果,并保存在共享驱动器上的文件夹 我是powershell的新手,所以不知道我该怎么做。如果需要,我可以添加代码 谢谢 操作系统 代码是在powershell上,如何将脚本保存为共享文件夹中的文本文件,以及如何在屏幕上打印和发送电子邮件,powershell,powershell-3.0,notepad,network-drive,Powershell,Powershell 3.0,Notepad,Network Drive,我在powershell上有一个脚本,它运行时会发送一封包含服务器状态详细信息的电子邮件,并在屏幕上打印信息 我现在正在寻找添加到脚本,以及发送电子邮件和打印屏幕上的信息,它还保存在记事本上的结果,并保存在共享驱动器上的文件夹 我是powershell的新手,所以不知道我该怎么做。如果需要,我可以添加代码 谢谢 操作系统 代码是 $DirectoryPath=拆分路径$MyInvocation.MyCommand.Path $ConfigurationPath=$DirectoryPath+'
$DirectoryPath=拆分路径$MyInvocation.MyCommand.Path
$ConfigurationPath=$DirectoryPath+'\Config file.xml'
功能格式化单元
{
参数($cellValue)
$cell=”“+$cellValue+“”
返回$cell
}
函数Getservicechecker
{
[xml]$ConfigFile=获取内容$ConfigurationPath
$Servers=$ConfigFile.SelectNodes('/Configs/Servers/Server')
$ServString=“ServerIP地址进程状态”
foreach($服务器中的服务器)
{
[字符串]$serverName=$Server.serverName
$OutputText+=“服务器:”+$serverName+$NL
$Process=$Server.ProcessesToMonitor
foreach($Process.ChildNodes中的Processtomonitor)
{
$ServString+=“”
$ServString+=FormatCell-cellValue$serverName
$ipaddress=测试连接$serverName-计数1 |选择Ipv4Address
$ipAddressValue=$ipaddress.IPV4Address.IPAddressToString
$servString+=FormatCell-cellValue$ipAddressValue
[字符串]$processName=$Processtomonitor.InnerText
$servicestatus=获取服务-ComputerName$serverName-Name$processName |选择状态
$ServString+=FormatCell-cellValue$processName
$FormattedStatus='status'
[string]$statusString=-join$servicestatus
$statusString=$statusString.Remove(0,“@{Status=“.Length”)
$statusString=$statusString.Remove($statusString.IndexOf(“}”))
$FormattedServiceStatus=FormatCell-cellValue$servicestatus
If($FormattedServiceStatus–eq“@{Status=Running}”)
{
$FormattedStatus='Running'
}
elseif($FormattedServiceStatus–eq“@{Status=Stopped}”)
{
$FormattedStatus=“服务已停止,需要调查
”
}
其他的
{
$FormattedStatus=“$servicestatus潜在调查”
}
写入主机“服务器:$serverName`t ipaddress:$ipAddressValue`t进程:$processName`t状态:$statusString”
$ServString+=FormatCell-cellValue$FormattedStatus
$ServString+=“”
}
}
返回“+$ServString+”
}
函数SendMail
{
参数($bodyText$subject)
$SmtpServer=“164.134.84.81”
$emailMessage=新对象System.Net.Mail.MailMessage
$fromaddress=“osman.test”。farooq@gmail.com"
$recipients=(“奥斯曼。farooq@atos.net")
$Subject=“BOXI服务器报告”+$dateTimeOfServiceCheck
$body=“”
$body+=“table.gridtable{font-family:verdana,arial,sans-serif;字体大小:11px;颜色:#333333;边框宽度:1px;边框颜色:#666666;边框折叠:折叠;}”
$body+=“table.gridtable th{边框宽度:1px;填充:8px;边框样式:纯色;边框颜色:#666666;背景颜色:#dedede;}”
$body+=“table.gridtable td{边框宽度:1px;填充:8px;边框样式:纯色;边框颜色:#666666;背景颜色:#ffffff;}”
$body+=“”
$body+=$bodyText
发送邮件-至$recipients-subject$subject-bodyashtml-body$body-from$fromAddress-SmtpServer$SmtpServer-Port 25
}
主要功能
{
$dateTimeOfServiceCheck=获取日期-格式F
$outputText=“服务检查器:”+$dateTimeOfServiceCheck
$emailBody=“”+$outputText+”#如果不希望datetime出现在表上,请取出
$emailBody+=Getservicechecker
SendMail$emailBody“BOXI服务器报告”+$DateTimeOfService检查
}
主要的
在PowerShell中编写文件有多种方法。你用哪一个完全取决于你自己
- 使用
命令设置内容
- 使用
命令输出文件
- 使用
方法[System.IO.File]::writealText()
正如Mathias在评论中提到的那样,
Tee Object
命令可用于写入文件并同时将其发送到管道,或将内容写入变量。可能还有更多的方法。在PowerShell中编写文件有很多方法。你用哪一个完全取决于你自己
- 使用
命令设置内容
- 使用
命令输出文件
- 使用
方法[System.IO.File]::writealText()
正如Mathias在评论中提到的那样,
Tee Object
命令可用于写入文件并同时将其发送到管道,或将内容写入变量。可能还有更多的方法。特雷弗的回答涵盖了编写文件的过程。写入文件后,可以在默认应用程序中使用Invoke Item
打开该文件:
$myPath = 'C:\my\file.txt'
$information | Set-Content -Path $myPath
$mypath | Invoke-Item
特雷弗的回答涵盖了文件的编写。写入文件后,可以在默认应用程序中使用
Invoke Item
打开该文件:
$myPath = 'C:\my\file.txt'
$information | Set-Content -Path $myPath
$mypath | Invoke-Item
请添加您的代码;这将更容易回答问题的方式,将有助于它。请添加您的代码;以有助于你的方式回答这个问题会更容易。我觉得
Tee Object
在这方面也值得一提<代码>>和>
:)我觉得Tee Object
在这方面也值得一提<代码>>和>
:)您好,这起作用了,但它没有将电子邮件的结果复制到notpad。只是提出来,空着document@OsmanFarooq您需要在问题中编辑代码。我们无法调试看不到的代码。@OsmanFarooq感谢您提供的addi