# Writing to Event Log with PowerShell - System.Object\[ \] conversion error

**URL:** <https://forums.powershell.org/t/writing-to-event-log-with-powershell-system-object-conversion-error/5319>\
**Category:** PowerShell Help\
**Created:** [December 7, 2015, 1:46am UTC](https://forums.powershell.org/t/writing-to-event-log-with-powershell-system-object-conversion-error/5319 "2015-12-07T01:46:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tony-bennett](https://avatars.discourse-cdn.com/v4/letter/t/e5b9ba/32.png) [@tony-bennett](https://forums.powershell.org/u/tony-bennett)\
**Post date:** [December 7, 2015, 1:46am UTC](https://forums.powershell.org/t/writing-to-event-log-with-powershell-system-object-conversion-error/5319/1 "2015-12-07T01:46:58Z")

</div>

Hi,

I’m trying to write a script that will write an event to the application log when a script is run that finds an account for which delegation has been configured. The code I’ve been trying to get working is this:

$result=Get-ADGroupMember -Identity “Domain Admins” | ForEach-Object {Get-ADUser -Identity $\_ -Properties AccountNotDelegated | Where-Object {$\_.AccountNotDelegated -eq $false} | Format-Table DistinguishedName,AccountNotDelegated -AutoSize}

If ($result -ne $null) {  
Write-EventLog -Logname Application -Source “My Test” -EntryType Warning -EventID 1 -Message $result}

When I run this, I get the error:

“Write-EventLog : Cannot convert ‘System.Object’ to the type ‘System.String’ required by parameter ‘Message’.  
Specified method is not supported.”

Does anyone know how I might change this so I can get the output I’m after, without having to first write the data to a text file on the file system? All I’m trying to do is get a list of high level accounts that have delegation configured and log an event on it which the monitoring system can then pick up and alert on.

Thanks

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [December 7, 2015, 2:09am UTC](https://forums.powershell.org/t/writing-to-event-log-with-powershell-system-object-conversion-error/5319/2 "2015-12-07T02:09:31Z")

</div>

The Format-Table and similar commands output a bunch of objects which are meant to be processed by the Out-\* commands. You can’t assign them directly to another command that expects a string, but you can pipe Format-Table to Out-String first:

```
$result = Get-ADGroupMember -Identity "Domain Admins" |
          ForEach-Object {
              Get-ADUser -Identity $_ -Properties AccountNotDelegated |
              Where-Object {$_.AccountNotDelegated -eq $false}
          } |
          Format-Table DistinguishedName,AccountNotDelegated -AutoSize |
          Out-String
```

For readability, I’ve split some of your code across multiple lines, and I’ve moved the pipe to Format-Table to be outside of your ForEach-Object loop (which was likely your intention; otherwise you’d end up with a separate table for every user.)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [December 7, 2015, 2:11am UTC](https://forums.powershell.org/t/writing-to-event-log-with-powershell-system-object-conversion-error/5319/3 "2015-12-07T02:11:20Z")

</div>

Incidentally, try this and see if it works. Simplifies your code a bit, and it would be faster besides 🙂

```
$result = Get-ADGroupMember -Identity "Domain Admins" |
          Get-ADUser -Properties AccountNotDelegated -Filter { AccountNotDeleted -eq $false } |
          Format-Table DistinguishedName,AccountNotDelegated -AutoSize |
          Out-String
```

---

<div class="post-metadata">

**Author:** ![tony-bennett](https://avatars.discourse-cdn.com/v4/letter/t/e5b9ba/32.png) [@tony-bennett](https://forums.powershell.org/u/tony-bennett)\
**Post date:** [April 4, 2016, 5:58pm UTC](https://forums.powershell.org/t/writing-to-event-log-with-powershell-system-object-conversion-error/5319/4 "2016-04-04T17:58:36Z")

</div>

Thanks Dave Wyatt, much appreciated 🙂

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:41pm UTC](https://forums.powershell.org/t/writing-to-event-log-with-powershell-system-object-conversion-error/5319/5 "2024-05-16T20:41:38Z")

</div>


