Link to home
Start Free TrialLog in
Avatar of janhoedt
janhoedt

asked on

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

Avatar of Daniel_PL
Daniel_PL
Flag of Poland image

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
Avatar of janhoedt
janhoedt

ASKER

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).
ASKER CERTIFIED SOLUTION
Avatar of Daniel_PL
Daniel_PL
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
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.