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

Powershell

Avatar of undefined
Last Comment
Daniel_PL
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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of janhoedt
janhoedt

ASKER

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?
Avatar of Daniel_PL
Daniel_PL
Flag of Poland image

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

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.

27K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo