How to get the create user and date of a job

For some workflows it is necessary to know which Vault user created a job or when it has been created.

In order for this function to work powerVault and a Vault connection are needed:

Import-Module powerVault
Open-VaultConnection -Vault "VaultName" -Server "ServerName" -User "UserName" -Password "Password"
 

You have to get the user's id from the job in the job queue. With that id you can retrieve the matching user object..


<#
  .SYNOPSIS
  Returns a Vault user object
  .DESCRIPTION
  This function checks if the current user has permissions to retrieve the user information. If it doesn't the function logs a warning and does nothing. If it does the function returns an Autodesk.Connectivity.WebServices.User object.
  .EXAMPLE
  $JobCreator = GetVaultJobCreateUser -JobId $job.Id
  $JobCreator.Name
  $JobCreator.Id
    $JobCreator.Email

#>
function GetVaultJobCreateUser {

  param(
      [ValidateNotNullOrEmpty()]
      [long]$JobId
    )

      if(-not $vault.JobService.CheckRolePermissions(@("GetJobsByDate"))) {
          Write-Host "User needs 'JobQueueRead' permission to execute GetJobsByDate"
          return
        }

      if(-not $vault.AdminService.CheckRolePermissions(@("GetUserByUserId"))) {
          Write-Host "User needs 'AdminUserRead' permission to execute GetUserInfoByUserId"
          return
        }

      $allJobs = $vault.JobService.GetJobsByDate([int]::MaxValue,[DateTime]::MinValue)
      $currentJob = $allJobs | Where-Object { $_.Id -eq $JobId }
        $user = $vault.AdminService.GetUserByUserId($currentJob.CreateUserId)

      return $user
}

<#

  .SYNOPSIS
  Returns a System.DateTime object with the job creation date
  .DESCRIPTION
  This function checks if the current user has permissions to retrieve job information. If it doesn't the function logs a warning and does nothing. If it does the function returns a System.DateTime object.
  .EXAMPLE
    $JobCreateDate = GetVaultJobCreateDate -JobId $job.Id

#>
function GetVaultJobCreateDate {

  param(
      [ValidateNotNullOrEmpty()]
      [long]$JobId
    )

      if(-not $vault.JobService.CheckRolePermissions(@("GetJobsByDate"))) {
          Write-Host "User needs 'JobQueueRead' permission to execute GetJobsByDate"
          return
        }

      $allJobs = $vault.JobService.GetJobsByDate([int]::MaxValue,[DateTime]::MinValue)
        $currentJob = $allJobs | Where-Object { $_.Id -eq $JobId }

        return $currentJob.CreateDate

}