Question: I'am not able to get any results of the Exchange Management CmdLet (OnPremises) `Get-MailboxDatabaseCopyStatus` within a Function or Expression.

```
function Get-DAGDatabaseCopys ($Identity) {
   # Neither this line:
   #[Int]$DBCopys = Invoke-Command { @(Get-MailboxDatabaseCopyStatus -Identity $Identity).count }
   # Or this line works:
   $DBCopys = @(Get-MailboxDatabaseCopyStatus -Identity $Identity).count
   return $DBCopys
}

```

Usage:

```
Get-DAGDatabaseCopys -Identity "DatabaseName"

```

Expression Example:

```
$strMasterType = "DatabaseAvailabilityGroup"
$expDatabaseSpaceGB   = @{Expression={if(($_.MasterType -eq $strMasterType) -and ($IncludeDagCopys)){
    start-sleep 1 
    $DBCopys = @(Get-MailboxDatabaseCopyStatus -Identity $_).count
    return [math]::Round((Value-ToGB -Value $_.AvailableNewMailboxSpace),2) * $DBCopys
};Label="DatabaseWhiteSpaceSizeGB"}

```

Usage:

```
$MailboxDBStatus      = Get-MailboxDatabase -Status | select ServerName, Name, MasterType, $expDatabaseSpaceGB

```

The Returnvalue of the function or expression will always be `empty` or `0` (if DataType is Int). There is no data in the `$DBCopys` Object within the function/expression, even if i try to receive the full object (not just the count).

The Same CmdLet direct in the Script Root or on the Powershell Console work's as expected.

Is this a general behaviour of this cmdlet or am I doing something wrong here?

Thx.

Answer: 
Fix the *if-then-else* statement. The keyword *then* is missing. I've added *ps* parameter *-x* to include "*processes which do not have a controlling terminal*" (which normally web servers do not). The script below works as expected

```sh
#!/bin/sh
if ps -axef | egrep 'apache|http' | grep -v grep > /dev/null; then
    echo 'process_running'
else
    echo 'process_not_running'
fi

```

Then use the script in Ansible. You might want to use [Literal block scalar](https://yaml.org/spec/1.2/spec.html#Block) to improve the code's readability. For example

```yaml
    - name:  verify application/database processes are not running
      shell: |
        if ps -axef | egrep 'apache|http' | grep -v grep > /dev/null; then
            echo 'process_running'
        else
            echo 'process_not_running'
        fi
      ignore_errors: false
      register: app_process_check
    - debug:
        var: app_process_check.stdout

```

gives, if Apache is running

```yaml
  app_process_check.stdout: process_running

```

otherwise

```yaml
  app_process_check.stdout: process_not_running

```
Instead of using command:/shell: consider the ansible.builtin.service\_facts: module. [ansible.docs page for service\_facts](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/service_facts_module.html)

From that page:

```
- name: Populate service facts
  service_facts:

- debug:
    var: ansible_facts.services

```

From there you can access various facts about configured services.

`ansible_facts.services['httpd']` or `ansible_facts.services['apache']`