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: 
If you only want to ensure that a service is running you don't need to check it yourself, ansible can handle this for you.

```
- name: ensure apache is started
  service:
    name: apache2
    state: started

```

Ansible will start it if it is no running, and will do nothing if it already runs.

If you need to check this for another reason you can do the check in ansible instead of the shell:

```
- name: check if apache is running
  shell: ps -eaf
  register: running_processes
  changed_when: >
    ('apache' in running_processes.stdout) or 
    ('httpd' in running_processes.stdout)

```

Afterward you can use this with `running_processes.changed` (or move the check itself to the next task).
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']`