title: Powershell Get-Service report wrong information
body: <p>I've created a powershell script to list all domain controllers under current trusted forest, and than check each individual server for specific services if it's running or not and send an email with report.</p>
  
  <p>I found 2 issues so far.</p>
  
  <ol>
  <li><p><code>Get-Service -name "MyService" -ComputerName $myComputer</code> "will say no service is found with that name, but if I list all the services : <code>Get-Service  -ComputerName $myComputer it will say "This operation might require other privileges."</code> This is a problem because I'm reporting that the service doesn't exist but it is actually there.</p></li>
  <li><p>For some reason if I just run the powershell script from command line manually it lists way more servers and most of the info is correct. But I schedule the powershell script from windows task manager with that same account the information is all wrong and it reports way less servers.</p></li>
  </ol>
  
  <p>Script:</p>
  
  <pre><code>#$domain = [system.directoryservices.activedirectory.domain]::GetCurrentDomain().Name
  #$numerOfDomainControlers= nslookup $domain
  Clear-Content .\log.txt
  
  $startTime = Get-Date
  
  $allDCs = ((Get-ADForest).Domains | %{ Get-ADDomainController -Filter * -Server $_ } | Where-Object { $_.hostname -notlike '*root*' }).hostname
  
  
  foreach( $allDC in $allDCs){
  
  
  
  $testConnection = Test-Connection $allDC -Quiet -Count 1 -ErrorAction SilentlyContinue
  
  Write-Host 
  
  if($testConnection -like "true") {
  
      $SyncStatus = Get-Service -name "MyServicesName" -ComputerName $allDC  -ErrorAction SilentlyContinue 
  
  
      if($SyncStatus.length -eq 0) {
      Write-Host "MyServicesName doesn't exists on:"$allDC 
  
      $SyncStatus = "MyServicesName doesn't exists on:" + $allDC 
      $logs = $SyncStatus |Out-File .\log.txt -Append
                                           }
                                              else{
                                                  write-host "MyServicesName on "$allDC "is: " $SyncStatus.status
                                                  $SyncStatus = "MyServicesName on " + $allDC + "is: " + $SyncStatus.status
                                                   $logs = $SyncStatus |Out-File .\log.txt -Append
                                                  }
  
                                      }
                                      else 
                                      {
                                      Write-Host "Test Connection to Server failed:"$allDC 
                                      $SyncStatus = "Test Connection to Server failed:" + $allDC 
                                      $logs = $SyncStatus |Out-File .\log.txt -Append
                                      }
  
  
  }
  
  
  $endTime = Get-Date
  $totalTime =  New-TimeSpan -start $startTime -end $endTime
  Write-Host $totalTime
  $logs = "Total Time in Seconds:" + $totalTime |Out-File .\log.txt -Append
                                                                  #send Email
  
                                                                  $logs = Get-Content .\log.txt
                                                                  $serverName = $env:computername
                                                                  $LogTime = Get-Date -Format "MM/dd/yyyy hh:mm:ss"
                                                                  $FromAddress = "email.com"
                                                                  $ToAddress = "email.com"                                                                
                                                                  foreach($log in $logs)
                                                                  {
                                                                  $Messagebody = $messagebody + $log + "`r`n" |Out-String
                                                                  }
                                                                  [string] $MessageSubject ="Status Report runing on " + $serverName +"  " + $endTime
                                                                  $SendingServer = "server"
                                                                  Send-MailMessage -to $ToAddress  -from $FromAddress -subject $MessageSubject -smtpServer  $SendingServer -body $Messagebody -Attachments .\log.txt
  </code></pre>