Hi All,
I have a text file of 500+ FQDN server names. Example:
I need to add the following entries for each servername:
[servername1]
parent=Group
How would one go about doing this?
Thanks in advance
Hi All,
I have a text file of 500+ FQDN server names. Example:
I need to add the following entries for each servername:
[servername1]
parent=Group
How would one go about doing this?
Thanks in advance
String manipulation is a very powerful tool. Really you have 4 steps to accomplish.
Import your file, I’m choosing to do this with a get content
Massage your data, this is really the tricky part. I really only need to break off the leaf name (and I did this using a split and grabbing the 0 index). Then it’s just adding some string concatenation to add your brackets in.
Write your output, this is simple once you’ve massaged it.
Loop it
$FQDNS = Get-Content FQDNFile.txt
ForEach ($FQDN in $FQDNS)
{
$leaf = $FQDN.Split(".")[0]
out-file -filepath "FQDNFile2.txt" -Append -inputobject "[$leaf]"
out-file -filepath "FQDNFile2.txt" -Append -inputobject "parent=Group"
out-file -filepath "FQDNFile2.txt" -Append -inputobject "host=$FQDN"
}
# Create an object for each server then export results to csv
$server = Get-Content .\server.txt
$result =
foreach ($s in $server){
[PSCustomObject]@{
ServerName = $s -replace '.domain.com'
host = $s
parent = 'Group'
}
}
$result | Export-Csv .\server.csv -NoTypeInformation
Thanks Random Commandline!
I ended up using below code (Thanks Pedro Casalinho), but yours also seems to do the trick:
$FQDNServers = Get-Content C:\Temp\FQDNServers.txt
$Converted = @()
foreach ($iFQDNServers in $FQDNServers) {
$Output = @'
[{0}]
parent=Group
host={1}
'@ -f (($iFQDNServers.Split('.'))[0], $iFQDNServers)
$Converted += $Output
}
$Converted | Set-Content c:\Temp\FQDNServersConverted.txt
Thanks again for the help!
duplicate post