PowerShell Invoke-WebRequest : A positional parameter cannot be found that accepts argument XXXXX

Kyler Gilbert 0 Reputation points
2023-01-18T17:35:11.2666667+00:00

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

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,058 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Kyler Gilbert 0 Reputation points
    2023-01-18T18:14:38.6133333+00:00

    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.

    0 comments No comments

  2. MotoX80 31,571 Reputation points
    2023-01-19T00:33:47.48+00:00

    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"""
    

    User's image

    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

    0 comments No comments