Inedo Community Forums Forums
    • Recent
    • Tags
    • Popular
    • Login

    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

    Scheduled Pinned Locked Moved Support
    3 Posts 2 Posters 9 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • L Offline
      lucas_001
      last edited by lucas_001

      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?

      atrippA 1 Reply Last reply Reply Quote 0
      • atrippA Offline
        atripp inedo-engineer @lucas_001
        last edited by

        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

        1 Reply Last reply Reply Quote 0
        • L Offline
          lucas_001
          last edited by

          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!

          1 Reply Last reply Reply Quote 0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • 1 / 1
          • First post
            Last post
          Inedo Website Home • Support Home • Code of Conduct • Forums Guide • Documentation