How do I write a value to an OPC tag in kepware using PowerShell
How do I write/update the value of tag2 in Kepware using PowerShell. I am getting the error:
Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
At line:21 char:13
+ $response = Invoke-RestMethod -Uri $kepwareServerUrl -Method Put -Bod ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
Here is my code:
$kepwareServerUrl = 'http://localhost:57412/config/v1
/project/channels/Channel1/devices/Device1/tags/Tag2'
$newValue = 10
$payload = @{
"v" = $newValue
} | ConvertTo-Json
$username = "Administrator"
$password = ""
$encodedCredentials =
[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes
("${username}:${password}"))
Write-Host "Payload: $payload"
Write-Host "URL: $kepwareServerUrl"
try
{
$response = Invoke-RestMethod `
-Uri $kepwareServerUrl -Method Put `
-Body $payload `
-ContentType "application/json" `
-Headers @{Authorization=("Basic
$encodedCredentials")}
Write-Host "Tag value updated successfully."
}
catch
{
Write-Host "Failed to update the tag value. Error: $_"
}
if ($response -eq $null) {
Write-Host "Failed to update the tag value."
}
else
{
Write-Host "Tag value updated successfully."
}