Search Unity

Storing Unity Hub CLI result in a variable in powershell

Discussion in 'Unity Hub' started by tom_e_roberts, Sep 17, 2019.

  1. tom_e_roberts

    tom_e_roberts

    Joined:
    Jul 19, 2017
    Posts:
    29
    Has anyone had any luck storing the unity hubs new CLI stuff in a variable in powershell? I'm trying to make some build scripts but the Hub seems to do some very strange things to the displayed console buffer (it returns immediately and writes over the top of it after that)

    I'm using Unity Hub 2.1.1 on Windows 10

    When I type this command in powershell
    & ".\Unity Hub.exe" --headless editors

    Nothing shows and the cursor immediately returns and then strangely goes to the next line

    When I type this command
    & ".\Unity Hub.exe" -- --headless editors

    The list of editors is shown, but only after the command has returned and then proceeds to write over the top of where the cursor is and beyond, often making the console unusable

    I have tried assigning it to variables and such but no luck
     
  2. tom_e_roberts

    tom_e_roberts

    Joined:
    Jul 19, 2017
    Posts:
    29
    For those wondering if there's a way in the hub CLI to be used with a scripting language, I have managed to get my build machines using it with powershell (only on windows for now) with the following script:
    Code (CSharp):
    1. [CmdletBinding()]
    2. [OutputType([bool])]
    3. param (
    4.     # Parameter help description
    5.     [Parameter(Mandatory=$true)]
    6.     [string]
    7.     $UnityVersion,
    8.     [Parameter(Mandatory=$true)]
    9.     [string]
    10.     $UnityChangeset,
    11.     [Parameter(Mandatory=$false)]
    12.     [string[]]
    13.     $Modules = @(),
    14.     [Parameter(Mandatory=$false)]
    15.     [string]
    16.     $UnityHubLocation = 'C:\Program Files\Unity Hub\Unity Hub.exe',
    17.     [Parameter(Mandatory=$false)]
    18.     [int]
    19.     $TimeOutMilliseconds = 3600000
    20. )
    21. [Diagnostics.ProcessStartInfo]$pInfo = [Diagnostics.ProcessStartInfo]::new()
    22. $pInfo.FileName = $UnityHubLocation
    23. $pInfo.RedirectStandardError = $false
    24. $pInfo.RedirectStandardOutput = $false
    25. $pInfo.UseShellExecute = $false
    26. $pInfo.Arguments = "-- --headless install --version $UnityVersion --changeset $UnityChangeset"
    27. foreach ($module in $modules) {
    28.     $pInfo.Arguments += " --module $module"
    29. }
    30.  
    31. [Diagnostics.Process]$p = [Diagnostics.Process]::new()
    32. $p.StartInfo = $pInfo
    33. Write-Host "Waiting for Unity Hub command '$($pInfo.Arguments)'"
    34. $started = $p.Start()
    35. if ($started -eq $false)
    36. {
    37.     Write-Warning "Start for Unity Hub process returned false"
    38. }
    39. $exited = $p.WaitForExit($TimeOutMilliseconds)
    40. Write-Host "Finished waiting for Unity Hub command"
    41. if ($exited -eq $false)
    42. {
    43.     Write-Error "Unity Hub process did no exit after 1 hour, killing..."
    44.     $p.Kill()
    45.     return $false
    46. }
    47. return $p.ExitCode -eq 0
    Hope that helps people looking for the same thing
     
    nils_vixel and Kamand0l like this.
  3. Kamand0l

    Kamand0l

    Joined:
    May 19, 2014
    Posts:
    24
    I've ran into the exact same problem. Many thanks for your script, It was super helpful !
     
  4. RageAgainstThePixel

    RageAgainstThePixel

    Joined:
    Mar 11, 2020
    Posts:
    66
    Last edited: Jun 9, 2020