Pawel
Pawel

Reputation: 161

VB.NET - PS Pipeline - Responding to error

I'm executing a PS command in vb.net application and the PS command requires an answer 'Yes' or 'No' in order to proceed.

Shown below is my code for the command pipeline:

Command.AddCommand("Set-MailboxAutoReplyConfiguration").AddParameter("Identity", username).AddParameter("AutoReplyState", "Scheduled").AddParameter("StartTime", Format(FWDStart, "yyyy-MM-dd HH:mm:ss")).AddParameter("EndTime", Format(FWDEnd, "yyyy-MM-dd HH:mm:ss")).AddParameter("InternalMessage", message)

and this is what I've tried for the response to the exception:

Try
            results = pipeline.Invoke()
        Catch ex As Exception
            Dim messageresponse As Integer = MessageBox.Show(ex.Message, "User Information", MessageBoxButtons.YesNo, MessageBoxIcon.Error)

            If messageresponse = DialogResult.Yes Then
                pipeline.AddScript("Yes")
            ElseIf messageresponse = DialogResult.No Then
                pipeline.AddCommand("No")
            End If
            Try
                pipeline.Invoke()
            Catch ex2 As Exception
                MessageBox.Show(ex.Message, "User Information", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try

        End Try

For the messageresponse IF statement I did attempt pipeline.AddCommand/.AddScript/.AddParamater but this did not work.

The exception error has this message: "A command that prompts the user has failed because the host program or the command type does not support user interaction."

I have attempted the .AddParameter("Confirm", false) solution provided in first comment but that has not worked.

The error is caused by ForwardingSMTPAddress being set in the exchange as that is what is set when setting up out of office through the admin portal. We want to set it through our application now and that will set it in ForwardingAddress field.

Upvotes: 0

Views: 166

Answers (1)

Chris Boden
Chris Boden

Reputation: 219

The MS Docs (https://learn.microsoft.com/en-us/powershell/module/exchange/mailboxes/set-mailboxautoreplyconfiguration?view=exchange-ps) show the -Confirm parameter can be used to bypass the confirmation prompt using -Confirm:$false. I assume you should be able to do the following:

Command.AddCommand("Set-MailboxAutoReplyConfiguration").AddParameter("Identity", username).AddParameter("AutoReplyState", "Scheduled").AddParameter("StartTime", Format(FWDStart, "yyyy-MM-dd HH:mm:ss")).AddParameter("EndTime", Format(FWDEnd, "yyyy-MM-dd HH:mm:ss")).AddParameter("InternalMessage", message).AddParameter("Confirm", false)

Upvotes: 2

Related Questions