Powershell: create FQDN's for servers in globalvariables.ps1

Hi,

I'm loading my globalvariables.ps1 in my $profile.
Now I'd like to have all my servers available with NETBIOS and FQDN, how can I achieve this?

To clarify, a piece of my globalvariables.ps1:

GENERAL ################################################################################################
$Global:DomainFQDN = $env:USERDNSDOMAIN

SERVERS ################################################################################################
$Global:SCCMSiteServer01 = 'SCCMSERVER'
$Global:ConnectionBroker01 = 'CONNECTIONBROKER'
#ADDING EXTRA SERVERS HERE

#FQDN
#FOR EACH SERVER, ADD A GLOBAL VARIABLE SERVER_FQDN, HOWTO DO THIS?
$Global:SCCMSiteServer01_FQDN =  ($SCCMSiteServer01 + '.' + $DomainFQDN))
$Global:ConnectionBroker01_FQDN = ($ConnectionBroker01 + '.' + $DomainFQDN)

Open in new window

janhoedtAsked:
Who is Participating?
 
Daniel_PLDB Expert/ArchitectCommented:
Sure, how do you want to create\pass variables list? Do you need to discover those variables? I already suggested to name those variables which should be used by giving some descriptive prefix/suffix. It would do the job then, you'll never create variableFQDN for anything else than your special variables.
So instead $Global:SCCMSiteServer01 you could name it $Global:GlobalVar_SCCMSiteServer01 and then in the script discover only these:

get-variable -scope Global | ? {$_.Name -like '*GlobalVar_*' -and $_.Value -ne $null -and $_.Value -ne ''} | % {if (!(get-variable | ? {$_.Name -eq "$($_.name)_FQDN"})){New-Variable -Name "$($_.name)_FQDN" -Value "$($_.Value).$($Global:DomainFQDN)" -Force}}

Open in new window

0
 
Daniel_PLDB Expert/ArchitectCommented:
Hi,

Please find following example. I suggest to name your server variables with some constant, I used MySpecialVariable_ prefix to distinguish them.

$Global:DomainFQDN="local"
$Global:MySpecialVariable_server01="srv01"
$Global:MySpecialVariable_server02="srv02"
$Global:MySpecialVariable_server03="srv03"


get-variable -scope Global | ? {$_.Name -like '*MySpecialVariable*' -and $_.Value -ne $null -and $_.Value -ne ''} | % {if (!(get-variable | ? {$_.Name -eq "$($_.name)_FQDN"})){New-Variable -Name "$($_.name)_FQDN" -Value "$($_.Value).$($Global:DomainFQDN)" -Force}}

get-variable -Scope Global | ? {$_.Name -like '*MySpecialVariable*FQDN'} | ft -AutoSize

Open in new window


Regards,
Daniel
1
 
janhoedtAuthor Commented:
Thanks! Would it also be possible to only make variablesFQDN for a specific list? Otherwise it might also create variables which are also defined in scope Global (which are existing and should not get the FQDN).
0
 
janhoedtAuthor Commented:
Thanks but that makes it though to remember/use those variables in a script. Couldn't you just define those variables
$variables = $.... the ones I need
then add them with FQDN?
0
 
Daniel_PLDB Expert/ArchitectCommented:
Hi,

Do you have a list of those variables? In case you want variable names to be meaningful you can only prefix them for the script

#before
$Global:SCCMSiteServer01 = 'SCCMSERVER'
$Global:ConnectionBroker01 = 'CONNECTIONBROKER'
#after
$Global:AutoVar_SCCMSiteServer01 = 'SCCMSERVER'
$Global:AutoVar_ConnectionBroker01 = 'CONNECTIONBROKER'

#then
get-variable -scope Global | ? {$_.Name -like 'AutoVar_*' -and $_.Value -ne $null -and $_.Value -ne ''} | % {if (!(get-variable | ? {$_.Name -eq "$($_.name)_FQDN"})){New-Variable -Name "$($_.name)_FQDN" -Value "$($_.Value).$($Global:DomainFQDN)" -Force}}

Open in new window

That way you keep your name intact, variables are recognizable globally by prefix.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.