我正在尝试将 CSV 中的联系人写入 Cisco 电话的 Web 界面。剧本是-
非常感谢任何帮助!
此脚本最初设计为在 Python 中运行,但是,Python 并不是我们的环境原生的,因此需要进行转换。
我为删除片段表示歉意,我觉得原始代码让这篇文章变得混乱。
脚本-
# 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
}
错误-
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
在有人遇到同样情况的情况下,解决方案是在脚本上方添加以下内容 -
add-type @" using System.Net; using System.Security.Cryptography.X509Certificates; public class TrustAllCertsPolicy : ICertificatePolicy { public bool CheckValidationResult( ServicePoint srvPoint, X509Certificate 证书, WebRequest request, intcertificateProblem) { return true; } } "@