It may be worth noting that if I change -Body ''$webhookJson'' to -Body '$webhookJson', I receive a different error.
Invoke-Web-Request : Bad payload received by generic incoming webhook.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello, I apologize if this is a basic question but I am very new to PowerShell and trying to learn. I have a script to get specific information about a pc. The script works and I get successfully export the data to a CSV. I am trying to use a webhook with Microsoft teams and send the PC information to a teams chat. I am getting the following Error:
Invoke-WebRequest : A positional parameter cannot be found that accepts argument
Here is my script, I have removed the actual link but I know the link works as I was able to send a test message. Any help is appreciated.
$results = foreach ($computer in $computers)
{
Write-Verbose "Processing $computer"
$computerSystem = get-wmiobject Win32_ComputerSystem -Computer $computer
$computerBIOS = get-wmiobject Win32_BIOS -Computer $computer
$computerOS = get-wmiobject Win32_OperatingSystem -Computer $computer
$computerCPU = get-wmiobject Win32_Processor -Computer $computer
$computerDisk = Get-WmiObject Win32_logicaldisk -ComputerName $computer -Filter drivetype=3 |
select DeviceID, @{Name="Size(GB)";Expression={[decimal]("{0:N0}" -f($_.size/1gb))}}
[PSCustomObject]@{
"Computer Name" = $computerSystem.Name
Manufacturer = $computerSystem.Manufacturer
Model = $computerSystem.Model
"Serial Number" = $computerBIOS.SerialNumber
CPU = $computerCPU.Name
RAM = "{0:n2} GB" -f ($computerSystem.TotalPhysicalMemory/1GB)
"Operating System" = $computerOS.caption
Disk = "{0:n2} GB" -f ($computerDisk.'Size(GB)')
}
}
# Webhook Stuff
$webhookJson = ConvertTo-Json $results
$TeamsChannel = "My_Link"
Invoke-WebRequest -Method POST -ContentType 'Application/Json' -Body ''$webhookJson'' -Uri $TeamsChannel
It may be worth noting that if I change -Body ''$webhookJson'' to -Body '$webhookJson', I receive a different error.
Invoke-Web-Request : Bad payload received by generic incoming webhook.
Don't put any quotes around $webhookJson.
Invoke-WebRequest -Method POST -ContentType 'Application/Json' -Body $webhookJson -Uri $TeamsChannel
You have 2 sets of single quotes around $webhook in your first post. That is invalid.
In your second post you first show double quotes around the variable, which should work, but then you show single quotes which will not expand the variable contents.
Paste this into Powershell_ISE to see.
cls
$var = "Hello"
'Single quotes do NOT expand variables: $var'
"Double quotes do expand variables: $var"
'''3 single quotes are needed to put one single quote around the data: $var'''
"""3 double quote are needed to put one double quote around the expanded data: $var"""
Just type in $var to see it's contents. Do the same with $webhookJson.
Here is a good reference,
[https://www.sapien.com/books_training/Windows-PowerShell-4