Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 计划PS脚本并获取作为警报邮件的输出_Powershell_Biztalk - Fatal编程技术网

Powershell 计划PS脚本并获取作为警报邮件的输出

Powershell 计划PS脚本并获取作为警报邮件的输出,powershell,biztalk,Powershell,Biztalk,我是PowerShell的新手,需要帮助。下面是检查BizTalk中主机实例状态的标准脚本 我有两项任务: 1.以邮寄脚本的输出。 2.计划PS脚本 $servers = (".") #This function checks the status of the host instances on a BizTalk server ($Server). function checkhostinstancestatusstarted ($server) { #gets all host inst

我是PowerShell的新手,需要帮助。下面是检查BizTalk中主机实例状态的标准脚本

我有两项任务: 1.以邮寄脚本的输出。 2.计划PS脚本

$servers = (".")

#This function checks the status of the host instances on a BizTalk server ($Server).
function checkhostinstancestatusstarted ($server)
{
#gets all host instances on the server. Isolated (hosttype = 2) or disabled hosts are excluded .
    $hostinstances = get-wmiobject MSBTS_HostInstance -namespace 'root\MicrosoftBizTalkServer' | where {$_.runningserver -match $server -AND $_.hosttype -ne "2" -and $_.IsDisabled -ne "True"}
    write-host "Checking the state of all host instances on the server $server`:"

    foreach ($hostinstance in $hostinstances)
    {
        $HostInstanceName = $HostInstance.hostname

#Checks the host instance state
        if ($HostInstance.ServiceState -eq 1)
        {
            write-host "$HostInstanceName`: Stopped."
        }
        elseif ($HostInstance.ServiceState -eq 2)
        {
            write-host "$HostInstanceName`: Start pending."
        }
        elseif ($HostInstance.ServiceState -eq 3)
        {
            write-host "$HostInstanceName`: Stop pending."
        }
        elseif ($HostInstance.ServiceState -eq 4)
        {
            write-host "$HostInstanceName`: Started."
        }
        elseif ($HostInstance.ServiceState -eq 5)
        {
            write-host "$HostInstanceName`: Continue pending."
        }
        elseif ($HostInstance.ServiceState -eq 6)
        {
            write-host "$HostInstanceName`: Pause pending."
        }
        elseif ($HostInstance.ServiceState -eq 7)
        {
            write-host "$HostInstanceName`: Paused."
        }
        elseif ($HostInstance.ServiceState -eq 8)
        {
            write-host "$HostInstanceName`: Unknown."
        } 
    }
write-host `n  

        }

foreach ($server in $servers)
{
    checkhostinstancestatusstarted $server
}

看起来您的PowerShell脚本仍需要更多代码才能将输出放入文件中。一旦该部件正常工作,如果您有Microsoft Exchange Server,我们每天早上都会使用此脚本通过电子邮件将日志文件的输出发送给我们。Exchange的好处在于它有一个名为“pickup”的文件夹,它将以电子邮件的形式发送一个名为“*.eml”的格式正确的文本文件。 将此代码放入vbscript文件(即,将其命名为“EmailScript.vbs”或其他名称),然后通过单击“开始”>“管理工具”>“任务计划程序”并指向*.vbs文件,将vbscript作为服务器上的每日计划任务运行。 对于计划Powershell脚本本身,以下是我在Google上找到的几篇文章:


你到底想要什么?有人帮你完成剧本吗?展示您如何尝试收集数据并通过电子邮件发送。这不是一个很难添加的内容,但是这里不赞成让人们为您完成整个任务。请首先重写它,以创建具有hostinstance/status属性的自定义对象数组。然后你就可以使用一些东西了。或者你也可以在任何常规SMTP服务器上使用Send MailMessage。
'First retrieve the contents of the log file
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("C:\logfile", 1)
content = file.ReadAll
file.close

msg = "To: jdoe@company.com"
msg = msg & chr(13) & chr(10)
msg = msg & "From: server@server.com"
msg = msg & chr(13) & chr(10)
msg = msg & "Subject: powershell results"
msg = msg & chr(13) & chr(10) & chr(13) & chr(10) & content

Set f = fso.OpenTextFile("\\ServerName\c$\program files\microsoft\exchange server\transportroles\pickup\powershell.eml",2, True)
f.Write msg
f.Close
Set f = Nothing
Set fso = Nothing