I am working on a project to automate creating AD users, creating folders in the different departments/sections for the users, also send password automatically when AD user is created to their cellphones & startup package in the user company mail.
I am having a problem doing this in PowerShell, this works great in Python (my main language). I get this odd error when trying to process my request in powershell.
Code:
$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"
The error message as follows:
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
It looks like you're trying to use variables but inside single quotes. It needs to be double quotes for the variables to be expanded.
Try this:
$JSON = @"
{
"content": "$tekst",
"senderNumber": "SSB",
"targetNumbersAsDelimitedString": "47$nummer",
"sendDate": null,
"status": {"id": "5"},
"numberOfTargetNumbers": null,
"numberOfInvalidNumbers": ""
}
"@