Since we are continously creating DNS records for some of our Linux-Based Server we came up with the idea idea of doing it a little bit more automatize so we though using powershell for it and this was the result:
#Import CSV File
$Data = import-csv -Delimiter ";" -Path .\DNS_List.csv
#Creation of the Session with the DC
$Session = New-PSSession -ComputerName CWVEP009
#Creation of DNS Records according to the CSV File
$Data | ForEach-Object {
$Name = $_.name
$ZoneName = $_.zonename
$Ip = [IPAddress]$_.ip
Write-Output $Name
Write-Output $ZoneName
Write-Output $Ip
Invoke-Command -Session $Session -ScriptBlock {Add-DnsServerResourceRecordA -Name '$Name' -ZoneName '$ZoneName' -AllowUpdateAny -IPv4Address '$Ip.IPAddressToString' -TimeToLive 01:00:00}
}
Unfortunately, we were not succesfull with our goal yet because we are getting this error over a over again:
Cannot process argument transformation on parameter ‘IPv4Address’. Cannot convert value “$Ip.IPAddressToString” to type “System.Net.IPAddress”. Error:
“Cannot convert value “$Ip.IPAddressToString” to type “System.Net.IPAddress”. Error: “An invalid IP address was specified.””
+ CategoryInfo : InvalidData: ( [Add-DnsServerResourceRecordA], ParameterBindin…mationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Add-DnsServerResourceRecordA
+ PSComputerName : DC01Test
I ran out of ideas… have you faced something similar??
So, you’re doing a couple of odd things. For one, this:
'$Ip.IPAddressToString'
Won’t work. Single quotes don’t have the magic variable stuff, and even in double quotes you can’t call methods and properties all naked like that.
"$($Ip.IPAddressToString)"
Is what you likely mean. But I don’t understand why you’re doing this; you’ve cast $.ip as [ipaddress], but then you’re just returning a string. Why not just use $.ip as-is, instead of casting it back and forth?
(and those Write-Output commands should technically be Write-Verbose, as what you’re currently outputting is not the intended “result” of your script… but this is a nit-pick, not a functional problem)
“Cannot validate argument on parameter ‘IPv4Address’. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.”
I get the following when I run the script with “$($Ip.IPAddressToString)” suggestion:
Cannot process argument transformation on parameter ‘IPv4Address’. Cannot convert value “” to type “System.Net.IPAddress”. Error: "Cannot convert value “” to type
“System.Net.IPAddress”. Error: “An invalid IP address was specified.”"