VCF Automation Blog

from Stefan Schnell

Sometimes it is necessary to persist data at the runtime of a program. Here an approach how to store data in a PowerShell container at the runtime.

Temporary Data Storage in a PowerShell Container at Runtime


The interesting thing about this approach is that the PowerShell CmdLets do not work, but calling the dotNET methods works fine. It's not clear to me if it is a bug or a feature.

Another interesting thing is that it seems to be necessary to create a directory, in this example temp. Writing directly to /run/vco-polyglot/function did not succeed in my case with release 8.5.1.
The root filesystem of the container is also writeable, checked with release 8.13.1.

Here the commented code:

<#
 # Example to create a temporary directory at runtime
 #
 # @author Stefan Schnell <mail@stefan-schnell.de>
 # @license MIT
 # @version 0.2.0
 #
 # Checked with Aria Automation 8.5.1 and 8.12.0
 #>

function Handler($context, $inputs) {
  $inputsString = $inputs | ConvertTo-Json -Compress;
  # Write-Host "Inputs were $($inputsString)";

  # Create temp directory in /run/vco-polyglot
  [System.String]$directory = "temp";
  # New-Item -ItemType "directory" -Force -Path "$($directory)";
  if ([System.IO.Directory]::Exists($directory) -eq $false) {
    Write-Host "Create directory $($directory)";
    [System.IO.DirectoryInfo]$directoryInfo = `
      [System.IO.Directory]::CreateDirectory($directory);
    Write-Host "Directory in path $($directoryInfo.FullName)";
  }

  # Create file Test.txt in temp directory with text
  [System.String]$writeText = "This is a test";
  [System.String]$path = "$($directory)/Test.txt";
  [System.IO.File]::WriteAllText($path, $writeText);

  # List all files in temp directory
  [System.String[]]$files = [System.IO.Directory]::GetFiles($directory);
  forEach ($file in $files) {
    Write-Host $file;
  }

  # Read the content from the Text.txt file in temp directory
  [System.String]$readText = [System.IO.File]::ReadAllText($path);
  Write-Host $readText;

  # Delete directory temp
  if ([System.IO.Directory]::Exists($directory)) {
    Write-Host "Delete directory $($directory)";
    try {
      [System.IO.Directory]::Delete($directory, $true);
    } catch {
      Write-Host "An error occured";
    }
  }

  $output = @{status = "done"};
  return $output;
}

Here my test result with the VCF Automation:

vcf automation action

Conclusion

In a PowerShell container we have the possibility, by using dotNET methods, to access the file system in the path /run/vco-polyglot. This can be very helpful for certain requirements. However, we must not forget that the container with its content is also deleted at the end of the automation process. That is why this approach can only be used for temporary storage.