0

I am trying to get the Exchange Database Status as well as the testing Outlook Web Services.

For getting the status of the existing Exchange databases, I have the following code :

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010

. .\config.ps1

$body +=Get-MailboxDatabase | Get-MailboxDatabaseCopyStatus | sort @{expression='Status';Descending=$true},@{expression='name';ascending=$true} | ft name,status,contentindexstate | Out-string
Write-Output $body

where config.ps1 is my config file which contains the details of the exchange servers

Now, after this, I found the command to test Outlook web services which reads the following :

Test-OutlookWebServices -Identity:[email protected]

I wanted to try using a semicolon to chain commands in powershell. But unfortunately, I can't try it because my environment is not yet ready.

So, can I do it this way :

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010

. .\config.ps1

$body +=Get-MailboxDatabase | Get-MailboxDatabaseCopyStatus | sort @{expression='Status';Descending=$true},@{expression='name';ascending=$true} | ft name,status,contentindexstate | Out-string; Test-OutlookWebServices -Identity:[email protected]
Write-Output $body

Is that the right way to do it? If not, How should I include the second command in the first to achieve this? I want both the results to be stored in $body and get printed.

3
  • Did you try do remove the echo and the quotation marks around the line breaks from the first output? : $body +="-----------------------------Exchange Databases Status-------------------------------`r`n`r`n" Since you simply want to display it with Write-Output Commented Feb 13, 2017 at 16:12
  • Yaaa...I have to get outputs from SQL, VMWare, Exchange, Ping servers and so on.. So, I am using that to distinguish all them as they will be mailed to my email address. Anyways, to make my code clear, I would remove that from the question. Commented Feb 13, 2017 at 16:45
  • I cannot try it with your commands but i tried it this way and for me it worked: $echo += "-----------------------------Exchange Databases Status-------------------------------rnrn" $echo += Get-Help Get-Help | Out-String $echo += Get-Command Get-Help | Out-String Write-Output $echo Commented Feb 15, 2017 at 7:08

1 Answer 1

1

Executing multiple commands in one line separated via a ; is not chaining the command, but simply executing them one after the other. Placing them on separate lines would have the same effect.

Instead you should save the result of the second command Test-OutlookWebServices -Identity:[email protected] also to the $body variable as you did with the first command:

$body = @();
$body += =Get-MailboxDatabase | Get-MailboxDatabaseCopyStatus | sort @{expression='Status';Descending=$true},@{expression='name';ascending=$true} | ft name,status,contentindexstate | Out-string; 
$body += Test-OutlookWebServices -Identity:[email protected]:
Write-Output $body;

With this, you output the result of both commands via one variable $body, which is an Object[] array with each array item holding the response from one command.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.