我正在进行一个项目,以自动创建AD用户,在不同部门/部门为用户创建文件夹,并在创建AD用户时自动将密码发送到用户的手机和用户公司邮件中的启动包中。
我在PowerShell中执行此操作时遇到问题,这在Python(我的主要语言)中效果很好。尝试在Powershell中处理我的请求时出现此奇怪错误。
代码:
$url = "hiding_this_for_obvious_reasons" # Request URL
$nummer = Read-Host("Skriv inn mobil nummer: ")
$tekst = Read-Host("Skriv inn melding: ")
#Request Payload information
$JSON = @'
{
"content": "$tekst",
"senderNumber": "SSB",
"targetNumbersAsDelimitedString": "47$nummer",
"sendDate": null,
"status": {"id": "5"},
"numberOfTargetNumbers": null,
"numberOfInvalidNumbers": ""
}
'@
Invoke-WebRequest -Uri $url -Method Post -Body $JSON -ContentType "application/json"
错误消息如下:
Invoke-WebRequest : Error happened while processing request
At H:\Scripts\Powershell Scripts\ClearAD\SendSMSPowershell.ps1:19 char:1
+ Invoke-WebRequest -Uri $url -Method Post -Body $JSON -ContentType "ap ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExce
ption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
答案 0 :(得分:0)
您似乎在尝试使用变量,但在单引号内。要扩展变量,必须用双引号引起来。
尝试一下:
$JSON = @"
{
"content": "$tekst",
"senderNumber": "SSB",
"targetNumbersAsDelimitedString": "47$nummer",
"sendDate": null,
"status": {"id": "5"},
"numberOfTargetNumbers": null,
"numberOfInvalidNumbers": ""
}
"@