Welcome to the Inedo Forums! Check out the Forums Guide for help getting started.
If you are experiencing any issues with the forum software, please visit the Contact Form on our website and let us know!
Otter - PSEnsure script failed
-
Hello,
I'm evaluating Otter Version 2023.6 (Build 2).
I'm trying to write a remediating drift powershell script:<# .DESCRIPTION This script ensures that the specified environment variable is set to the desired value. .PARAMETER EnvironmentVariableName Environment variable name. .PARAMETER EnvironmentVariableValue Desired value for the environment variable. .AHEXECMODE $ExecutionMode .AHDESIREDVALUE $EnvironmentVariableDesiredValue .AHCURRENTVALUE $EnvironmentVariableCurrentValue .AHVALUEDRIFTED $EnvironmentVariableDriftedValue #> param ([string]$EnvironmentVariableName, [string]$EnvironmentVariableValue) $EnvironmentVariableDesiredValue = $EnvironmentVariableValue $EnvironmentVariableCurrentValue = [System.Environment]::GetEnvironmentVariable($EnvironmentVariableName, [System.EnvironmentVariableTarget]::Machine) Write-Host "Current value of environment variable '$EnvironmentVariableName' is '$EnvironmentVariableCurrentValue'." if ($ExecutionMode -eq "Collect") { if ($EnvironmentVariableValue -ne $EnvironmentVariableCurrentValue) { Write-Information "Environment variable '$EnvironmentVariableName' has drifted from the desired value." $EnvironmentVariableDriftedValue = $EnvironmentVariableCurrentValue return $false } else { Write-Information "Environment variable '$EnvironmentVariableName' is set to the desired value." return $true } } elseif ($ExecutionMode -eq "Configure") { if ($EnvironmentVariableValue -ne $EnvironmentVariableCurrentValue) { [System.Environment]::SetEnvironmentVariable($EnvironmentVariableName, $EnvironmentVariableValue, [System.EnvironmentVariableTarget]::Machine) Write-Information "Environment variable '$EnvironmentVariableName' has been set to the desired value." } else { Write-Information "Environment variable '$EnvironmentVariableName' is set to the desired value." } }
When this script is executed on a server it fails with following error:
DEBUG: 2024-10-03 10:05:11Z - Beginning collection run... DEBUG: 2024-10-03 10:05:12Z - Collecting current configuration from server... DEBUG: 2024-10-03 10:05:12Z - Using Windows PowerShell 5.1... DEBUG: 2024-10-03 10:05:12Z - Importing ExecutionMode... DEBUG: 2024-10-03 10:05:12Z - Assigning parameter EnvironmentVariableName... DEBUG: 2024-10-03 10:05:12Z - Assigning parameter EnvironmentVariableValue... INFO : 2024-10-03 10:05:12Z - Current value of environment variable 'MyTestVar' is ''. INFO : 2024-10-03 10:05:12Z - Environment variable 'MyTestVar' is set to the desired value. ERROR: 2024-10-03 10:05:12Z - Unhandled exception: System.Management.Automation.Remoting.PSRemotingTransportException: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. LineNumber: 0 | BytePositionInLine: 0. ---> System.Text.Json.JsonReaderException: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. LineNumber: 0 | BytePositionInLine: 0. at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan`1 bytes) at System.Text.Json.Utf8JsonReader.Read() at System.Text.Json.JsonDocument.Parse(ReadOnlySpan`1 utf8JsonSpan, JsonReaderOptions readerOptions, MetadataDb& database, StackRowStack& stack) at System.Text.Json.JsonDocument.Parse(ReadOnlyMemory`1 utf8Json, JsonReaderOptions readerOptions, Byte[] extraRentedArrayPoolBytes, PooledByteBufferWriter extraPooledByteBufferWriter) at System.Text.Json.JsonDocument.Parse(ReadOnlyMemory`1 json, JsonDocumentOptions options) at Inedo.Extensions.Scripting.PowerShell.PowerShellScriptRunner.ParseJson(ReadOnlyMemory`1 json) in C:\Users\builds\AppData\Local\Temp\InedoAgent\BuildMaster\192.168.44.60\Temp\_E380919\Src\Scripting\InedoExtension\PowerShell\PowerShellScriptRunner.cs:line 387 at Inedo.Extensions.Scripting.PowerShell.PowerShellScriptRunner.<>c__DisplayClass32_0.<RunAsync>b__0(Object s, DataAddedEventArgs e) in C:\Users\builds\AppData\Local\Temp\InedoAgent\BuildMaster\192.168.44.60\Temp\_E380919\Src\Scripting\InedoExtension\PowerShell\PowerShellScriptRunner.cs:line 217 at System.Management.Automation.PSDataCollection`1.RaiseEvents(Guid psInstanceId, Int32 index) at System.Management.Automation.PSDataCollection`1.InternalAddRange(Guid psInstanceId, ICollection collection) at System.Management.Automation.Internal.PSDataCollectionStream`1.Write(Object obj, Boolean enumerateCollection) at System.Management.Automation.Runspaces.Internal.ClientRemotePowerShell.HandleOutputReceived(Object sender, RemoteDataEventArgs`1 eventArgs) at System.Management.Automation.Internal.ClientPowerShellDataStructureHandler.ProcessReceivedData(RemoteDataObject`1 receivedData) at System.Management.Automation.Remoting.ClientRemoteSessionDSHandlerImpl.ProcessNonSessionMessages(RemoteDataObject`1 rcvdData) at System.Management.Automation.Remoting.BaseTransportManager.OnDataAvailableCallback(RemoteDataObject`1 remoteObject) at System.Management.Automation.Remoting.Client.BaseClientTransportManager.ServicePendingCallbacks(Object objectToProcess) --- End of inner exception stack trace --- at System.Management.Automation.Runspaces.AsyncResult.EndInvoke() at System.Management.Automation.PowerShell.EndInvoke(IAsyncResult asyncResult) at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization) --- End of stack trace from previous location --- at Inedo.Extensions.Scripting.PowerShell.PowerShellScriptRunner.RunAsync(String script, Dictionary`2 variables, Dictionary`2 parameters, Dictionary`2 outVariables, String workingDirectory, CancellationToken cancellationToken) in C:\Users\builds\AppData\Local\Temp\InedoAgent\BuildMaster\192.168.44.60\Temp\_E380919\Src\Scripting\InedoExtension\PowerShell\PowerShellScriptRunner.cs:line 267 at Inedo.Extensions.Scripting.PowerShell.ExecutePowerShellJob.StandardRunner.ExecuteAsync(String script, Dictionary`2 variables, Dictionary`2 parameters, String[] outVariables, String workingDirectory, CancellationToken cancellationToken) in C:\Users\builds\AppData\Local\Temp\InedoAgent\BuildMaster\192.168.44.60\Temp\_E380919\Src\Scripting\InedoExtension\PowerShell\ExecutePowerShellJob.cs:line 278 at Inedo.Extensions.Scripting.PowerShell.ExecutePowerShellJob.ExecuteAsync(CancellationToken cancellationToken) in C:\Users\builds\AppData\Local\Temp\InedoAgent\BuildMaster\192.168.44.60\Temp\_E380919\Src\Scripting\InedoExtension\PowerShell\ExecutePowerShellJob.cs:line 37 at Inedo.Agents.AgentCommand`1.Inedo.Agents.IAgentCommandWithResponse.ExecuteAsync(Stream responseStream) at Inedo.Agents.AgentCommandDispatcher.ExecuteCommandAsync(AgentCommand command, IClientConnection connection) ERROR: 2024-10-03 10:05:12Z - Collection run failed.
I would like to add that when I removed all augmented help metadata then this script would run with no issues.
Can you tell me what is the problem?
-
Hi @lucas_001
I'm a bit rusty with these and I'm going off memory, but I know that
AHVALUEDRIFTED
is supposed to point to a boolean.Have you tried a script like this instead?
$EnvironmentVariableDesiredValue = $EnvironmentVariableValue $EnvironmentVariableCurrentValue = [System.Environment]::GetEnvironmentVariable($EnvironmentVariableName, [System.EnvironmentVariableTarget]::Machine) $EnvironmentVariableDriftedValue = $EnvironmentVariableValue -ne $EnvironmentVariableCurrentValue Write-Host "Current value of environment variable '$EnvironmentVariableName' is '$EnvironmentVariableCurrentValue'." if ($ExecutionMode -eq "Collect") { if ($EnvironmentVariableValue -ne $EnvironmentVariableCurrentValue) { Write-Information "Environment variable '$EnvironmentVariableName' has drifted from the desired value." } else { Write-Information "Environment variable '$EnvironmentVariableName' is set to the desired value." } } elseif ($ExecutionMode -eq "Configure") { if ($EnvironmentVariableDriftedValue) { [System.Environment]::SetEnvironmentVariable($EnvironmentVariableName, $EnvironmentVariableValue, [System.EnvironmentVariableTarget]::Machine) Write-Information "Environment variable '$EnvironmentVariableName' has been set to the desired value." } else { Write-Information "Environment variable '$EnvironmentVariableName' is set to the desired value." } }
That might do the trick... note the only thing aside from logging that's happening is in the execute portion.
Can you try that? Obviously the logging experience should be improved. No idea why there's a JSON parse error happening.
Thanks,
Alana
-
Hello Alana,
Yes, you are right.
AHVALUEDRIFTED
has to be a boolean value. The name implies it actually but I did not connect the dots.Thank you for the help!