none
Powershell Script for Biztalk port information is running in Powershell ISE But Not running through Task Scheduler RRS feed

  • Question

  • Hi Team,

    I have created one power shell script which will check after every 15 min if any port is disabled and if it is disabled then it will enable it and send email notification.

    My script is running fine when I am running it in Windows Powershell ISE but when I am trying to run it through Task scheduler then it is getting failed .

    Note:

    1).Task scheduler configuration I have refered to https://devblogs.microsoft.com/scripting/use-powershell-to-create-scheduled-tasks/

    2). The user through which I am configuring has got all access rights.

    Any help and suggestion will be appreciated.

    History says Action was started and got Completed:

    Thanks in Advance!

    RG


    • Edited by Rishi Gaur Thursday, June 13, 2019 11:50 AM more information
    Thursday, June 13, 2019 11:39 AM

All replies

  • Hi Rishi,

    Thank you for posting on MSDN forum.

    I am assuming you are using powershell scripting fine then Please check the property of your file, it should be like below,

    


    Thanks,
    Kamlesh Kumar

    If my reply is helpful please mark as Answer or vote as Helpful.

    My blog | Twitter | LinkedIn

    Thursday, June 13, 2019 12:48 PM
    Moderator
  • Hi Kamlesh,

    Thanks for the reply !

    I made changes in the property of the file suggested by you but still it is not working as expected.

    RG


    RG

    Friday, June 14, 2019 4:06 AM
  • Check PS version you are using in TaskScheduler in Actions tab of that Task and try to change it.

    32-bit PowerShell C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe
    64-bit PowerShell C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

    Friday, June 14, 2019 7:53 AM
  • Hello ,

    I have made same changes but still no Success:(

    RG


    RG

    Friday, June 14, 2019 8:57 AM
  • Hi ,

    I have made below suggested changes but still no success :-

    (a). changes the property of file to Windows Powershell

    (b). updated the PC version of Task Scheduler under ACTION tab also.

    Below is my power shell script which is working fine in Windows Powershell but not in Task Scheduler:-

    [array]$ConsiderReceiveLocation = [string[]](Get-Content .\MonitorReceiveLocationList.txt)
    [array]$ConsiderSendPort = [string[]](Get-Content .\MonitorSendPortList.txt)


    #Need to  Send Email
    [bool]$SendEmail = $false

    #Get Send ports
    [ARRAY]$SendPorts = get-wmiobject MSBTS_SendPort -namespace 'root\MicrosoftBizTalkServer' 

    #Get Receive locations
    [ARRAY]$ReceiveLocations = get-wmiobject MSBTS_ReceiveLocation -namespace 'root\MicrosoftBizTalkServer' 

    #Set mail variables
    [STRING]$PSEmailServer = "abc.xyz.com"
    [STRING]$SubjectPrefix = "Port status - "
    [STRING]$From = "biztalksupport@abc.com"
    [array]$EmailTo = ("Rishi.Gaur@abc.com ")

    #Get DB info
    [STRING]$SQLInstance = get-wmiobject MSBTS_GroupSetting -namespace root\MicrosoftBizTalkServer | select-object -expand MgmtDbServerName
    [STRING]$BizTalkManagementDb = get-wmiobject MSBTS_GroupSetting -namespace root\MicrosoftBizTalkServer | select-object -expand MgmtDbName
    [STRING]$BizTalkGroup = "$SQLInstance" + ":" + "$BizTalkManagementDb"

    #=== Make sure the ExplorerOM assembly is loaded ===#
    [void] [System.reflection.Assembly]::LoadWithPartialName("Microsoft.BizTalk.ExplorerOM")

    #=== Connect to the BizTalk Management database ===#
    $Catalog = New-Object Microsoft.BizTalk.ExplorerOM.BtsCatalogExplorer
    $Catalog.ConnectionString = "SERVER="+ "$SQLInstance" +";DATABASE=BizTalkMgmtDb;Integrated Security=SSPI"

    #Create mail content
    $mailBodyPT = ""

    #Create mail content
    #$mailTextReportPT = "There are: "
    [STRING]$Subject = $SubjectPrefix + $BizTalkGroup 
    [INT]$counter = 0;   


    Write-HOST "Inside Script"
        
    #================= FUNCTIONS ==============================================================================================

    function SendEmailForPort
    {
        Param($mailBodyPT)

    # HTML Format for Output 
    $HTMLmessage = @"
    <font color=""black"" face=""Arial"" size=""3"">
    <h2 style='font-family:arial;'><b>BizTalk Port Status Report on  Server</b></h2>
    <style type=""text/css"">body{font: .8em ""Lucida Grande"", Tahoma, Arial, Helvetica, sans-serif;}
    ol{margin:0;}
    table{width:80%;}
    thead{}
    thead th{font-size:120%;text-align:left;}
    th{border-bottom:2px solid rgb(79,129,189);border-top:2px solid rgb(79,129,189);padding-bottom:10px;padding-top:10px;}
    tr{padding:10px 10px 10px 10px;border:none;}
    #middle{background-color:#900;}
    </style>
    <body BGCOLOR=""white"">
    $mailBodyPT
    </body>
    "@

    If ($SendEmail -eq $True)
    {
    #Send mail
    #foreach ($to in $EmailTo) 
    #{
    $Body = $HTMLmessage + " " +  $BizTalkGroup
    $SMTPClient = New-Object Net.Mail.SmtpClient($PSEmailServer) 
    $message = New-Object Net.Mail.MailMessage($from, $EmailTo, $Subject, $Body)
    $message.IsBodyHtml = $true
    $SMTPClient.Send($message)
    #}
    }
    }

    #=============================================================================================================================
        #Add mail content for stopped send ports - only stopped or Unelisted Send Ports are added to the mail
        Foreach ($SendPort in $SendPorts)
        {
    #Write-HOST "Foreach  Send port" + $SendPort.status

            $mailBodyPT = ""

    #Check only Consider Send Port
    if($ConsiderSendPort -contains $SendPort.name )
    {

    If($SendPort.status -ne  3)
    {
    $SendEmail = $True

    if ($SendPort.status -eq 2)
    {
    $SendPortStatus = "Stopped"
    }
    elseif ($SendPort.status -eq 1)
    {
    $SendPortStatus = "Unelisted"
    }

    $mailBodyPT += "<h2>Send ports Stopped</h2>"
    $mailBodyPT += "<br/>" + "<b>S.No : </b>" + (++$counter) + "<br/>"
                    $mailBodyPT += "<b>Application : </b>" + $Catalog.SendPorts[$SendPort.name].Application.Name + "<br/>"
    $mailBodyPT += "<b>Send Port : </b>"  + $SendPort.name + "<br/>"
                    $mailBodyPT += "<b>Status : </b>"  + $SendPortStatus + "<br/>"
    $mailBodyPT += "<b>Transport type : </b>"  + $SendPort.PTTransportType + "<br/>"
                    $mailBodyPT += "<b>URI : </b>"  + $SendPort.PTAddress +  "<br/>"
    $mailBodyPT += "<b>Is Dynamic : </b>" + $SendPort.IsDynamic + "<br/>"
                    $mailBodyPT += "<b>Is Two Way : </b>"  + $SendPort.IsTwoWay + "<br/>"
                    if (![string]::IsNullOrEmpty(!$SendPort.Filter))
    {
    $mailBodyPT += "<b>Has Filters : </b>" + "False" + "<br/>"
    }
    else
    {
    $mailBodyPT += "<b>Has Filters : </b>" + "True" + "<br/>"
    }

                    if($Catalog.SendPorts[$SendPort.name].OutboundTransforms.Count -eq 0)
    {
    $mailBodyPT += "<b>Has Outbound Transformations : </b>" + "False" + "<br/>"
    }
    else
    {
    $mailBodyPT += "<b>Has Outbound Transformations : </b>" + "True" + "<br/>"
    }

    $mailBodyPT += "<b>Error Occured On DateTime : </b>" + $(Get-Date) + "<br/>"

    if ($SendPort.status -eq 2 -or $SendPort.status -eq 1)
    {
    #Auto-Healing feature
    $SendPort.InvokeMethod("Start",$null)

    $mailBodyPT += "<BR><font color='green'>The Stopped ports were automatically Started by this script! Please access the environment to check if ports are running correctly.</font><BR>" + "<br/>"
    }
                
                 #Call Email Function
                if (-not ([string]::IsNullOrEmpty($mailBodyPT)))
                {
                    SendEmailForPort ($mailBodyPT)
                }
                 $counter = 0;
                 $mailBodyPT = "";
    }
    }


        [INT]$counter = 0;   
        Foreach ($ReceiveLocation in $ReceiveLocations)
        {

            $mailBodyPT = ""

    #Check only Consider Receive location
    if($ConsiderReceiveLocation -contains $ReceiveLocation.Name)
    {
    #Only Add Disable Receive Location to Emails
    If($ReceiveLocation.IsDisabled  -eq  "true")
    {
    $SendEmail = $True

    #Set status to a user friendly name
    If($ReceiveLocation.IsDisabled  -eq  "true")
    {
    $ReceiveLocationStatus = "Disabled"
    }
    elseif ($ReceiveLocation.IsDisabled  -eq  "false")
    {
    $ReceiveLocationStatus = "Enabled"
    }
    $mailBodyPT += "<h2>Receive locations Disabled</h2>"
                    $mailBodyPT += "<b>S.No : </b>" + (++$counter) + "<br/>"
                    $mailBodyPT += "<b>Application : </b>" + $Catalog.ReceivePorts[$ReceiveLocation.ReceivePortName].Application.Name + "<br/>"
    $mailBodyPT += "<b>Receive Port : </b>"  + $ReceiveLocation.ReceivePortName + "<br/>"
                    $mailBodyPT += "<b>Receive Location : </b>"  + $ReceiveLocation.name + "<br/>"
                    $mailBodyPT += "<b>Status : </b>"  + $ReceiveLocationStatus + "<br/>"
    $mailBodyPT += "<b>Transport type : </b>"  + $ReceiveLocation.AdapterName + "<br/>"
                    $mailBodyPT += "<b>URI : </b>"  + $ReceiveLocation.InboundTransportURL +  "<br/>"
    $mailBodyPT += "<b>Is Dynamic : </b>" + $ReceiveLocation.IsPrimary + "<br/>"
                    $mailBodyPT += "<b>Is Two Way : </b>"  + $Catalog.ReceivePorts[$ReceiveLocation.ReceivePortName].IsTwoWay + "<br/>"
                    if($Catalog.ReceivePorts[$ReceiveLocation.ReceivePortName].InboundTransforms.Count -eq 0)
    {
    $mailBodyPT += "<b>Has Inbound Transformations : </b>" + "False" + "<br/>"
    }
    else
    {
    $mailBodyPT += "<b>Has Inbound Transformations : </b>" + "True" + "<br/>"
    }
    $mailBodyPT += "<b>Error Occured On DateTime : </b>" + $(Get-Date) + "<br/>"
    #Auto-Healing feature
    $ReceiveLocation.InvokeMethod("Enable",$null)
    $mailBodyPT += "<BR><font color='green'>The Disabled ports were automatically Enable by this script! Please access the environment to check if ports are running correctly.</font><BR>" + "<br/>"
    }
                #Call Email Function
                if (-not ([string]::IsNullOrEmpty($mailBodyPT)))
                {
                    SendEmailForPort ($mailBodyPT)
                }
                $counter = 0
                $mailBodyPT = ""
    }
        }

    RG

    Friday, June 14, 2019 9:16 AM
  • Could you please share the Action details from Task Scheduler ?

    Also try below configuration and this should work.

    -file "C:\Scripts\CheckFileCount.PS1"

    Also could you please simply try to execute .PS1 file and see if it's working ?


    Thanks,
    Kamlesh Kumar

    If my reply is helpful please mark as Answer or vote as Helpful.

    My blog | Twitter | LinkedIn

    Friday, June 14, 2019 11:51 AM
    Moderator
  • Before worrying about the Task Scheduler, does the script run at the PowerShell prompt?
    Friday, June 14, 2019 12:05 PM
    Moderator
  • Hi ,

    I am following same Action as suggested by you but still it's not working:-



    • Edited by Rishi Gaur Friday, June 14, 2019 12:08 PM updated pic
    Friday, June 14, 2019 12:07 PM
  • Yes script is running as expected when running from Powershell prompt.

    RG


    RG

    Friday, June 14, 2019 12:13 PM
  • So, now the question is...What exactly do you mean by 'not working'?

    What is the error message?  Without that, anything just a guess.

    Please post the error.

    Friday, June 14, 2019 1:01 PM
    Moderator
  • Hello John,

    I am not getting any error in Event Viewer as my Task is getting completed but I am not getting the desired output i.e disabled port should be enabled and email should be triggered

    NOTE: same script while running in power shell is giving the desired output:


    RG

    Saturday, June 15, 2019 6:03 AM
  • You have to capture the output from the script.
    Saturday, June 15, 2019 11:59 AM
    Moderator
  • Rishi, could you please share the Last Run Result in Task Scheduler Library? I suspect somewhere you have some error code related to powershell if this script is working fine.

    Thanks,
    Kamlesh Kumar

    If my reply is helpful please mark as Answer or vote as Helpful.

    My blog | Twitter | LinkedIn

    Saturday, June 15, 2019 3:51 PM
    Moderator