Steven Askew
Steven Askew

Reputation: 11

Cannot bind parameter 'RemainingScripts'

I'm trying to write contacts from a CSV to the web interface for a Cisco phone. The script is-

Script

Error

Any help is greatly appreciated!

This script was originally designed to run in Python, however, Python isn't native to our environment, so a conversion was necessary.

I apologize for dropping snippets, I felt like raw code made this post muddy.

Script-

# Define the phone ID and the Cisco URL
$phone_id = '10.45.210.166'
$cisco_url = "https://$phone_id/addContact"

# Define the parameters
$params = @('name', 'workPhone', 'key', 'ringTone')

# Import the CSV file
$csv_file = Import-Csv -Path 'phone-list.csv' -Header 'Name', 'Number'

# Loop through each row in the CSV file
foreach ($row in $csv_file) {
    # Skip the header row
    if ($row.Name -eq 'Name') {
        continue
    }

    # Prepare the data for the POST request
    $data = @{
        'name'       = $row.Name
        'workPhone'  = $row.Number
        'key'        = '0'  # Assuming a default value for 'key'
        'ringTone'   = '1'  # Assuming a default value for 'ringTone'
    }

    # Convert the data to URL-encoded format
    $encodedData = $data | ForEach-Object { "$($_.Key)=$($_.Value)" } -join '&'

    # Send the POST request
    Invoke-RestMethod -Uri $cisco_url -Method Post -Body $encodedData -ContentType 'application/x-www-form-urlencoded' -SkipCertificateCheck

    # Wait for 0.5 seconds
    Start-Sleep -Seconds 0.5
}

error-

ForEach-Object : Cannot bind parameter 'RemainingScripts'. Cannot convert the "-join" value of type "System.String" to
type "System.Management.Automation.ScriptBlock".
At line:27 char:28
+ ... edData = $data | ForEach-Object { "$($_.Key)=$($_.Value)" } -join '&'
+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [ForEach-Object], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ForEachObjectCommand

Invoke-RestMethod : A parameter cannot be found that matches parameter name 'SkipCertificateCheck'.
At line:30 char:120
+ ... ContentType 'application/x-www-form-urlencoded' -SkipCertificateCheck
+                                                     ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-RestMethod], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Upvotes: 1

Views: 209

Answers (1)

Steven Askew
Steven Askew

Reputation: 11

In the instance where someone lands in this same situation, the resolution for this was to add the following above the script-

Add-Type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;

public class TrustAllCertsPolicy : ICertificatePolicy {
  public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
  { return true; }
}
"@

Upvotes: 0

Related Questions