Dynamic Variables

Product: PowerShell Universal
Version: 3.7.3

Hey its me again lol

I feel like this something really trivial but we use a variable pretty much platform wide called $HostName which as you would guess is the FQDN of the server such as “https://psu.com

To make this a bit dynamic I configured it as such

image

I attempted to use initialize.ps1 but using PSU cmdlets such as new-psuvariable requires authentication and I dont really want to hardcode app tokens in code. Maybe we could leverage a keyvault for this but i was hoping there was a variable that I just dont know about somewhere I can use to get the URL of the server…

Thanks
Michael

Could you use the system cache or something like that?

Set-PSUCache -Key ‘A’ -Value ‘Thing’

I dont think that will work since you need to use the connect-uaserver

I get the following error when using that cmdlet

Failed to set hostname. Specify a computer name or use the Connect-UAServer command.
switch ($ENV:COMPUTERNAME) {
    "comp1" { $CurrentHostName = "https://psu.domain" }
    Default { $CurrentHostName = "http://localhost:5000" }
}

try {
    Set-PSUCache -Key "HostName" -Value $CurrentHostName
    $message = "Setting hostname value to $CurrentHostName"
    $message | Out-File -FilePath $log -Append
}
catch {
    $message = "Failed to set hostname. $_"
    $message | Out-File -FilePath $log -Append
}

if we cant get a Variable built in that is available to the platform could we just get the conditional script block for the “New-PSUVariable” cmdlet :wink:

You could try integrated mode so it doesn’t need to connect.

  Set-PSUCache -Key "HostName" -Value $CurrentHostName -Integrated

Having a dynamic value for variables would be cool though. Could provide a script block that gets executed when the variable is read during startup.

New-PSUVariable -Name 'SomeVar' -ScriptBlock {
switch ($ENV:COMPUTERNAME) {
    "comp1" { $CurrentHostName = "https://psu.domain" }
    Default { $CurrentHostName = "http://localhost:5000" }
}
$CurrentHostName

}

Another thing is you could use the new PSU region. Put this at the top of your variables.ps1.

#region PSUHeader
switch ($ENV:COMPUTERNAME) {
    "comp1" { $CurrentHostName = "https://psu.domain" }
    Default { $CurrentHostName = "http://localhost:5000" }
}
New-PSUVariable -Name 'HostName' -Value $CurrentHostName
#endregion

@adam I tired that and I keep getting the following

Failed to set hostname. Specify a computer name or use the Connect-UAServer command.

Note im running this through the initialize.ps1.

Ah, ok. I know initialize is kinda different than the rest of the scripts we run in PSU.

I do think the PSUHeader strategy may work. You’d do that in variables.ps1.

This is what we are going to go with for the time being.

#region PSUHeader
switch ($ENV:COMPUTERNAME) {
	"PSU2" { $CurrentHostName = "https://psu" }
	"PSU3" { $CurrentHostName = "https://psudev" }
	"PSU1" { $CurrentHostName = "http://localhost:5000" }
	Default { $CurrentHostName = "http://localhost:5000" }
}

try {
	$Parameters = @{
		Name        = "HostName"
		Value       = $CurrentHostName
		Type        = "System.String"
		Description = "Hostname of the PSU instance"
		Integrated  = $true
	}

	New-PSUVariable @Parameters -Integrated
}
catch {
	"Failed to set hostname. $_"
}

#endregion

1 Like

So this isnt working as you would expect.

inside the header i have the following

#region PSUHeader
New-PSUVariable -Name "HostName" -Value $CurrentHostName -Description "Hostname of the PSU instance"
New-PSUVariable -Name "Environment" -Value $Environment -Description "PSU environment type"
#endregion

and if you refresh the psu instance you will see it add duplicate vars? Reloading the config and sometimes they will just disappear entirely.

If you create a new var through the PSU UI it will then screw up the config and add the code outside of the PSUheader/region

image

I tried to add some logic in the header which checked to see if the variable existed

   if (! (Get-PSUVariable -Name "HostName" -Integrated)) { 
		$Parameters = @{
			Name        = "HostName"
			Value       = $CurrentHostName
			Type        = "System.String"
			Description = "Hostname of the PSU instance"
			Integrated  = $true
		} 
		New-PSUVariable @Parameters
	}
	if (! (Get-PSUVariable -Name "Environment" -Integrated)) { 
		$Parameters = @{
			Name        = "Environment"
			Value       = $Environment
			Type        = "System.String"
			Description = "PSU environment type"
			Integrated  = $true
		}
		New-PSUVariable @Parameters 
	}

As you can imagine none of this works lol

I opened an issue because this seems broke.

I’ll circle back if I cannot reproduce.

2 Likes