我敢打赌,这是一个愚蠢的问题,但我对此代码有问题...当我在函数外部运行函数的内容时,它工作得很好我可以编写控制台而没有问题。然而,在功能下运行并没有那么多...不知道为什么会这样,如果有人能让我知道什么是我想要的那将是伟大的。目标是在重新启动之前运行Preuptime功能并在Post after之后运行,然后将结果用于电子邮件。
由于 科迪
$startupvariables
# Store all the start up variables so you can clean up when the script finishes.
if ($startupvariables) { try {Remove-Variable -Name startupvariables -Scope Global -ErrorAction SilentlyContinue } catch { } }
New-Variable -force -name startupVariables -value ( Get-Variable | ForEach-Object { $_.Name } )
$date = Get-Date
$server = 'SGSQL1', 'SGSQL2', 'appv1', 'appv2', 'sltappv4'
$emailSmtpServer = "exmbx6"
$emailFrom = "gordonc@"
$emailTo = "retinaAlert@"
$emailSubject = "Result: Reboot report for $server $date"
$emailSubjectF = "Stuff Failed"
$emailBody = @"
<p><strong>Todays Date</strong>:$date</p>
<p> </p>
<p>Server: SGSQL1</p>
<p> <strong>Pre Boot Time:</strong>$sgr11</p>
<p> <strong>Post Boot Time:</strong>$sgr12</p>
<p>________________________________</p>
<p>Server: SGSQL2</p>
<p><strong>Pre Boot Time:</strong>$sgr21</p>
<p><strong>Post Boot Time:</strong>$sgr22</p>
<p>________________________________</p>
<p>Server: Appv1</p>
<p><strong>Pre Boot Time:</strong>$spr11</p>
<p><strong>Post Boot Time:</strong>$apr12</p>
<p>________________________________</p>
<p>Server: Appv2</p>
<p><strong>Pre Boot Time:</strong>$apr21</p>
<p><strong>Post Boot Time:</strong>$apr22</p>
<p>________________________________</p>
<p>Server: SltAppv4</p>
<p><strong>Pre Boot Time:</strong>$spr11</p>
<p><strong>Post Boot Time:</strong>$spr12</p>
<p> </p>
<p>Please close the Event Notifcation in FootPrints</p>
"@
$emailBodyF = @"
<p>THINGS FAILD</p>
"@
Function Preuptime{
$sgs11 = Get-WmiObject -Class Win32_OperatingSystem -Computer "sgsql1"
$sgr11 = $sgs11.ConvertToDateTime($sgs11.LastBootUpTime) | Out-String
#
$sgs21 = Get-WmiObject -Class Win32_OperatingSystem -Computer "sgsql2"
$sgr21 = $sgs21.ConvertToDateTime($sgs21.LastBootUpTime) | Out-String
#
$app11 = Get-WmiObject -Class Win32_OperatingSystem -Computer "appv1"
$apr11 = $app11.ConvertToDateTime($app11.LastBootUpTime) | Out-String
#
$app21 = Get-WmiObject -Class Win32_OperatingSystem -Computer "appv2"
$apr21 = $app21.ConvertToDateTime($app21.LastBootUpTime) | Out-String
#
$spp11 = Get-WmiObject -Class Win32_OperatingSystem -Computer "sltappv4"
$spr11 = $spp11.ConvertToDateTime($spp11.LastBootUpTime) | Out-String
}
Function Postuptime{
$sgs12 = Get-WmiObject -Class Win32_OperatingSystem -Computer "sgsql1"
$sgr12 = $sgs12.ConvertToDateTime($sgs12.LastBootUpTime) | Out-String
#
$sgs22 = Get-WmiObject -Class Win32_OperatingSystem -Computer "sgsql2"
$sgr22 = $sgs22.ConvertToDateTime($sgs22.LastBootUpTime) | Out-String
#
$app12 = Get-WmiObject -Class Win32_OperatingSystem -Computer "appv1"
$apr12 = $app12.ConvertToDateTime($app12.LastBootUpTime) | Out-String
#
$app22 = Get-WmiObject -Class Win32_OperatingSystem -Computer "appv2"
$apr22 = $app22.ConvertToDateTime($app22.LastBootUpTime) | Out-String
#
$spp12 = Get-WmiObject -Class Win32_OperatingSystem -Computer "sltappv4"
$spr12 = $spp12.ConvertToDateTime($spp12.LastBootUpTime) | Out-String
}
Function FailedEmail {
Send-MailMessage -To $emailTo -From $emailFrom -Subject $emailSubjectF -Body $emailBodyF -SmtpServer $emailSmtpServer -BodyAsHtml
}
Function WorkedEmail {
Send-MailMessage -To $emailTo -From $emailFrom -Subject $emailSubject -Body $emailBody -SmtpServer $emailSmtpServer -BodyAsHtml
}
Function ServerReboot {
foreach($server in $servers){
ping -n 2 $server >$null
if($lastexitcode -eq 0){
#Restart-Computer -Wait -For Wmi $server
} else {
FailedEmail
}
}
}
PreUptime
ServerReboot
PostUptime
WorkedEmail
Function Clean-Memory {
Get-Variable |
Where-Object { $startupVariables -notcontains $_.Name } |
ForEach-Object {
try { Remove-Variable -Name "$($_.Name)" -Force -Scope "global" -ErrorAction SilentlyContinue -WarningAction SilentlyContinue}
catch { }
}
}
我不得不把电子邮件正文放在Post重启时间的最后一点以使其拉出正确的信息......结果它结果只是空白..很奇怪有人知道为什么?
$sgs12 = Get-WmiObject -Class Win32_OperatingSystem -Computer "sgsql1"
$sgr12 = $sgs12.ConvertToDateTime($sgs12.LastBootUpTime) | Out-String
#
$sgs22 = Get-WmiObject -Class Win32_OperatingSystem -Computer "sgsql2"
$sgr22 = $sgs22.ConvertToDateTime($sgs22.LastBootUpTime) | Out-String
#
$app12 = Get-WmiObject -Class Win32_OperatingSystem -Computer "appv1"
$apr12 = $app12.ConvertToDateTime($app12.LastBootUpTime) | Out-String
#
$app22 = Get-WmiObject -Class Win32_OperatingSystem -Computer "appv2"
$apr22 = $app22.ConvertToDateTime($app22.LastBootUpTime) | Out-String
#
$spp12 = Get-WmiObject -Class Win32_OperatingSystem -Computer "sltappv4"
$spr12 = $spp12.ConvertToDateTime($spp12.LastBootUpTime) | Out-String
$emailSmtpServer = "exmbx6"
$emailFrom = "gordonc@slhs.org"
$emailTo = "retinaAlert@slhs.org"
$emailSubject = "Result: Reboot report for $server $date"
$emailSubjectF = "Stuff Failed"
$emailBody = @"
<p><strong>Todays Date</strong>:$date</p>
<p> </p>
<p>Server: SGSQL1</p>
<p> <strong>Pre Boot Time:</strong>$sgr11</p>
<p> <strong>Post Boot Time:</strong>$sgr12</p>
<p>________________________________</p>
<p>Server: SGSQL2</p>
<p><strong>Pre Boot Time:</strong>$sgr21</p>
<p><strong>Post Boot Time:</strong>$sgr22</p>
<p>________________________________</p>
<p>Server: Appv1</p>
<p><strong>Pre Boot Time:</strong>$apr11</p>
<p><strong>Post Boot Time:</strong>$apr12</p>
<p>________________________________</p>
<p>Server: Appv2</p>
<p><strong>Pre Boot Time:</strong>$apr21</p>
<p><strong>Post Boot Time:</strong>$apr22</p>
<p>________________________________</p>
<p>Server: SltAppv4</p>
<p><strong>Pre Boot Time:</strong>$spr11</p>
<p><strong>Post Boot Time:</strong>$spr12</p>
<p> </p>
<p>Please close the Event Notifcation in FootPrints</p>
"@
#PostUptime
WorkedEmail
不移动身体
今日日期:07/09/2014 19:55:41 服务器:SGSQL1 预启动时间: 发布时间:
服务器:SGSQL2 预启动时间: 发布时间:
服务器:Appv1 预启动时间: 发布时间:
服务器:Appv2 预启动时间: 发布时间:
服务器:SltAppv4 预启动时间: 发布时间:
移动身体
今日日期:07/09/2014 19:58:41 服务器:SGSQL1 预启动时间:2014年7月9日星期三12:34:42 发布时间:2014年7月9日星期三上午12:34:42
服务器:SGSQL2 预启动时间:2014年7月9日星期三上午12:27:41 发布时间:2014年7月9日星期三上午12:27:41
服务器:Appv1 预启动时间:2014年7月9日星期三12:49:38 发布时间:2014年7月9日星期三上午12:49:38
服务器:Appv2 预启动时间:2014年7月9日星期三上午12:56:08 发布时间:2014年7月9日星期三12:56:08
服务器:SltAppv4 预启动时间:2014年7月9日星期三上午1:03:05 发布时间:2014年7月9日星期三上午1:03:05 请关闭FootPrints中的事件通知
答案 0 :(得分:0)
好吧我觉得我找到了我在函数中设置Var的解决方案我必须使用这个$ global标签,即$ global:sgr11。然后我可以毫无问题地调用$ sgr11。所以这回答了我的问题,但是我仍然在调用serverreboot函数时遇到问题,但这可能是一个新问题。
Function Preuptime{
$global:sgs11 = Get-WmiObject -Class Win32_OperatingSystem -Computer "sgsql1"
$global:sgr11 = $sgs11.ConvertToDateTime($sgs11.LastBootUpTime) | Out-String
#
$global:sgs21 = Get-WmiObject -Class Win32_OperatingSystem -Computer "sgsql2"
$global:sgr21 = $sgs21.ConvertToDateTime($sgs21.LastBootUpTime) | Out-String
#
$global:app11 = Get-WmiObject -Class Win32_OperatingSystem -Computer "appv1"
$global:apr11 = $app11.ConvertToDateTime($app11.LastBootUpTime) | Out-String
#
$global:app21 = Get-WmiObject -Class Win32_OperatingSystem -Computer "appv2"
$global:apr21 = $app21.ConvertToDateTime($app21.LastBootUpTime) | Out-String
#
$global:spp11 = Get-WmiObject -Class Win32_OperatingSystem -Computer "sltappv4"
$global:spr11 = $spp11.ConvertToDateTime($spp11.LastBootUpTime) | Out-String
}
PS C:\ Users \ gordonc&gt; preuptime
PS C:\ Users \ gordonc&gt; Write-Host @ spr11
你好吗,J u l y 1 0,2 0 1 4 1:3 5:0 4 A M
答案 1 :(得分:0)
至于我的ServerReboot功能,我想通了!
foreach($servers in $server){
ping -n 2 $servers >$null
if($lastexitcode -eq 0){
Restart-Computer -Wait -For Wmi $servers
} else {
FailedEmail
}
}
}
之前我在$ server中使用$ server,它应该是$ server中的服务器...只是一个非常基本的错误,需要8个youtube视频和很多故障排除......