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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.