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:
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)
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
$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
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}}
That way you keep your name intact, variables are recognizable globally by prefix.
Please find following example. I suggest to name your server variables with some constant, I used MySpecialVariable_ prefix to distinguish them.
Open in new window
Regards,
Daniel