My Code Works in PowerShell 5.1 but Fails in 7.2 – Need Help!

0
0
Asked By CodingNinja92 On

I'm working on a project that sends messages to Teams users automatically. This code works perfectly in PowerShell version 5.1, but when I try to use it in version 7.2, I get an error stating that it's missing body content. Here's the code I'm using:
```powershell
$params = @{
body = @{
contentType = "html"
content = "test"
}
}
New-MgChatMessage -ChatId $ChatDetails.Id -BodyParameter $params
```

But in version 7.2, I get the following error:
```
New-MgChatMessage_Create:
Line |
7 | New-MgChatMessage -ChatId $ChatDetails.Id -BodyParameter $params
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Missing body content
Status: 400 (BadRequest)
ErrorCode: BadRequest
Date: 2025-04-21T13:52:32
```

I've checked the service error codes, but I'm not sure what the issue is. Any insights?

4 Answers

Answered By CodeWizard_45 On

You might want to check how you are referencing the body parameter. After `-BodyParameter`, try `$params.body` or you could even do `(($params.body) | ConvertTo-JSON)` to make sure it's properly formatted.

Answered By DevDude_22 On

I noticed that the error message you posted doesn't quite match the snippet. Make sure you're actually calling the updated code when testing. It can sometimes behave differently in the ISE compared to running it directly in the terminal.

Answered By ScriptMaster_17 On

I've encountered a similar issue, and for version 7.2, it looks like you need to modify your command to ensure proper syntax:
```powershell
New-MgChatMessage -ChatId $($ChatDetails.Id) -BodyParameter $params
```
Make sure to use `$($ChatDetails.Id)` instead of just `$ChatDetails.Id`. It really seems to help with any missing content errors.

Answered By TechGuru_88 On

It looks like you're not passing the parameters correctly for version 7.2. Try this instead:
```powershell
$params = @{
ChatId = $ChatDetails.Id
Body = @{
contentType = "html"
content = "test"
}
}
New-MgChatMessage @params
```
This should fix the issue! Just a heads-up, I’m replying from my phone so formatting might be off a bit.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.