VMware Cloud Community
StefanSchnell
Enthusiast
Enthusiast

Tip: Temporary Data Storage in a PowerShell Container at Runtime

Based on the discussion about Access vRO Filesystem using Polyglot from @imtrinity94 here an approach how to store data in a PowerShell container at the 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. 

Here the commented code:

 

# Begin-----------------------------------------------------------------

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.Directory]::CreateDirectory($directory);
  }

  # 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)";
    [System.IO.Directory]::Delete($directory, $true);
  }

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

# End-------------------------------------------------------------------

 

Here my test result with the vRealize Automation 8.5.1.18666:

fileInPSContainer.jpg

Conclusion

In a PowerShell container we have the possibility, by using dotNET methods, to access the file system in the path /run/vco-polyglot/function. 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.


More interesting information at blog.stschnell.de

2 Replies
xian_
Expert
Expert

The root filesystem of the container is writeable:

function Handler($context, $inputs) {

    $dir = New-Item -ItemType "directory" -Force -Path "/mydata"
    Get-Date | Out-File -FilePath "/mydata/date.txt"
    Get-Content -Path "/mydata/date.txt" | Write-Host

    return @{}
}

xian__0-1695051754586.png

You can use it in Python as well, if needed.

StefanSchnell
Enthusiast
Enthusiast

Hello @xian_,

thank you very much for your reply and experience sharing.

Interesting that it works now. I tried it at HOL with release 8.5.1 a longer time ago and I tried it now with release 8.13.1. It didn't work then, but now it works, good to know.

Best regards
Stefan


More interesting information at blog.stschnell.de

Reply
0 Kudos