Add-DnsServerResourceRecord with multiple lines of descriptive text - powershell

I'm trying to add multiple strings to the -DescriptiveText property of a single resource record using the command as shown in the docs here:
# a,b,c,d in the following are different items separated by `r`n
Add-DNSServerResourceRecord -DescriptiveText "a`r`nb`r`nc`r`nd" -Name "identifier" -Txt -ZoneName $ZONE -ZoneScope $ZONE_SCOPE -TimeToLive 0:1:0:0
I know the expected return value of another command that retrieves this data results in the following:
TXT
"a"
"b"
"c"
"d"
However the above returns the following which doesn't work
TXT
"a
b
c
d"
I have tried other methods as follows including:
Trying arrays like "a","b","c","d" as referenced in the accepted answer here which errored
Other methods of formatting the string to match the expected output involving adding missing quotes marks to try and align it with the expected return value
Tried calling the command with each individual part which made multiple records with the required data but didn't work as it needs to be in one record
Any advice on how to do it would be appreciated. Thanks :)

Txt records can have multiple strings and/or multiline strings. It looks like your other command is expecting the former.
Unfortunately, the Add-DnsServerResourceRecord cmdlet only accepts a single string for the DescriptiveText. `r`n only adds a newline to that single string, which is why your tool isn't processing them as separate entries.
Instead, use dnscmd:
dnscmd.exe servername /recordadd example.com identifier TXT "a" "b" "c"
A similar annoyance is that Get-DnsServerResourceRecord will only return the first string of a text record as well.

Related

PowerShell unable to write to csv file. Write-Host and Export-Csv produces 2 different outputs [duplicate]

This question already has answers here:
Export-CSV exports length but not name
(6 answers)
Closed 4 years ago.
Operating System - Windows 10
Powershell version - 5.1.15063.1088
Ok, I'm really trying hard to think logically what can be wrong with this PowerShell script, but apparently can't get an idea and asking for some help. So here is what I'm trying to do, simple as 1+1
If I understood the tutorial correctly, creating an array in PowerShell is like this:
$someVariable = "PowerShell", "MowerShell", "HowerShell", "ZowerShell"
Then I'm simply trying to write this thing to csv file with comma as delimeter, but firstly give it a try in the console output
$someVariable | ConvertTo-Csv -NoTypeInformation
According to PowerShell 5.1 official documentation
...Specifies a delimiter to separate the property values. The default
is a comma (,).
So no additional writing that I would like to use comma as delimiter is not required. Once the command Write-Host $someVariable is executed, I see this weird output:
"Length" "10" "10" "10" "10"
What is this? Am I suppose to see the values of my variable separated with simple comma? So from the numbers I can guess that scripts calculates the amount of alphabet letters in each word -
P o w e r S h e l l
contains 10 letters.
Is this the suggested way to calculate the amount of letters in the string (in case I get PowerShell task on my next job interview) using ConvertTo-Csv command?
Writing this funky data to the csv file itself leads to more unexpected results:
Now I'm completely lost what those numbers are...
Is this possible to write my strings as STRINGS to the csv file in one line rather then silly numbers?
The desired output is this entry as headers in the csv file:
"PowerShell","MowerShell","HowerShell","ZowerShell"
The output reads "Length", and has a series of 10's. Each of your strings are 10 characters long (the double quotes aren't factored in).
Length can be calculated many ways. I wouldn't say there is one suggested way, only the ways that fit what you're trying to do.
To get the literal text of what you posted (no headers, etc.) in a csv, try:
$someVariable | Out-File foo.csv

Find All Words Containing a String From a .txt Document

I'm cleaning up a few dozen old files, and pulling them into one format. I've extracted the headers for each of the sources to a text (.txt) file, and need to identify the different wording for similar column names.
To do this, I need to be able to identify common words in the headers. For example, I need to identify all column names that might be "firstname", "1stname", "first_name", "f_name", etc, etc, etc.
What PowerShell syntax would I use to find all words containing the string "name" (e.g. "First_Name"), and extract that entire word to a separate text file?
Split the content of the file into individual words, then select the words containing the particular string:
(Get-Content 'input.txt') -split '\s+' -match 'name'

How to grab a portion of an LDAP string into a variable (Powershell)

I am able to get the full LDAP path into a variable $strPath and it will return a result such as:
LDAP://CN=computername,OU=City,OU=Servers,OU=###,DC=dom,DC=ain,DC=com
or in other locations it could look like:
LDAP://CN=computername,OU=Servers,OU=##,DC=dom,DC=ain,DC=com
I want to return only the ##, ### or #### value (it can be either two, three or four characters) which is our district code.
So some computer objects have a city name in their LDAP string while others do not (depending on the size of the district), and the district code can be two, three or four characters long.
I'm guessing I want to do something like find text "Servers,OU=" and remove everything including and before that and also remove text ",DC=dom,DC=ain,DC=com" to get my final variable.
Thanks in advance!
You may be interested in the ADName module I wrote:
https://github.com/Bill-Stewart/PowerShell-ADName
For your first example, the OU code is the 4th element from the right:
LDAP://CN=computername,OU=City,OU=Servers,OU=###,DC=dom,DC=ain,DC=com
So for example, you can write this:
(Get-ADName "LDAP://CN=computername,OU=City,OU=Servers,OU=###,DC=dom,DC=ain,DC=com" -Split -ValuesOnly)[-4]
The -Split parameter splits the LDAP path into an array, and -ValuesOnly omits the CN=, OU=, DC=, etc. The [-4] means "return the 4th element from the end of the array."
How about something like this:
$ldapString = "LDAP://CN=computername,OU=Servers,OU=##,DC=dom,DC=ain,DC=com"
$temp = $ldapString.Split(",=")
$districtCode = $temp[[array]::IndexOf($temp,"DC") - 1]
It works with both and no extra imports. As long as you do not have a district code that is "DC".

Advanced syntax

I came across the following line of syntax which is rather advanced (to me):
(Get-ADReplicationSubnet -Filter *) -notmatch [String]::Join('|',$c.Subnet)
The above does exactly what I want, retrieve the list of subnets that are not matched against the $c.Subnet variable. I tried recreating the same effect with the line below. This does not work.
Get-ADReplicationSubnet -Filter * | Where {$_.Name -notmatch $c.Subnet}
My question is; Can someone explain in simple English how the first line works? (Not sure on the [String]::Join('|',$c.Subnet) part. It is difficult to search for something you don't know the name of. Besides that, why does my version not work?
I should clarify that $c.Subnet is an array of values.
[String]::Join('|',$c.Subnet) is call to the .NET framework -- the Join() method of the String class. You can take a look at the documentation here.
The documentation leads to the following explanation of the return value: A string that consists of the elements in value delimited by the separator string. If value is an empty array, the method returns String.Empty.
This means your return string will be something like value1|value2|value3 (where each value is value from $c.Subnet), which -notmatch interprets as a regex, with | meaning or. What the first line does is return values that do not match value1 or value2 or value3.
Why (I think) your line doesn't work is because you're using -notmatch with an array rather than a string. This article has some usage info about the two.
[String]::Join('|',$c.Subnet)
The [String] part means the .NET class.
The ::Join part means make a call to a static method of the String class, i.e. you don't have to "new up" an object before you use it.
The Join method takes and array and turns it into a string delineated by the pipe (in this case)
Someone else will have an explanation for the rest.

Powershell script problem (Get-content vs assigning to variable)

I'm attempting to write a Twitter Powershell script that will use community created interfaces PoshTwitter with the Twitter API to attempt and find a list of followers who are potential spammers.
I have a feeling that my problem lies not with the particular cmdlet I'm calling (Get-TwitterFollowers), but rather with the difference between assigning a variable:
If I try this:
$rawFol = get-twitterfollowers -page $page -raw 1
$rawFol is different than if I do this:
get-twitterfollowers -page $page -raw 1 > .\page$page.txt
$rawFol = gc .\page$page.txt
The Get-TwitterFollowers cmdlet returns an XML file converted to string.
What things can I try to determine the differences between these two assignments? They look like they'd result with same content.
The difference you're seeing is how powershell handles new lines in strings. When calling the get-twitterfollowers CmdLet, it is either returning a single string or an array of strings. My guess by your description is that it returns a string. So the $rawFol variable will have a single string value. Any new lines are simply embedded into the string value.
The second command you write the return to a file. Now all of the newlines in the string are represented as lines in the file. Later when you call gc on that file, each line will be returned as a separate string. So the $rawFol variable will now have an array of strings.