I ran into somewhat a chicken or the egg problem. I have a simple spreadsheet that stored 2 columns of data (hostname and IP)
$JoinDomain = 'tataoui.com' # test lab domain name
$strVMName = 'DC' # Virtual machine / remote host name - where my test lab DC/DNS services reside
$DNSParameters = Import-Excel -Path $DataSourcePath -WorksheetName DNS
$DNSParameters | ForEach-Object {
# Create DNS entries
Add-DnsServerResourceRecord -ZoneName $JoinDomain -CN $strVMName -A -Name $_.Hostname -AllowUpdateAny -IPv4Address $_.IP -TimeToLive 01:00:00 -AgeRecord
}
I can read in the excel row of data '$DNSParameters" and popular the variable $_.Hostname and $_.IP without issue. However, since the desktop I am executing this from is not on the same domain), I am getting a permission denied error
Add-DnsServerResourceRecord : Failed to get the zone information for tataoui.com on server DC.
At line:13 char:5
+ Add-DnsServerResourceRecord -ZoneName $JoinDomain -CN DC -A -Name ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (test:root/Microsoft/...rResourceRecord) [Add-DnsServerResourceRecord], CimException
+ FullyQualifiedErrorId : WIN32 5,Add-DnsServerResourceRecord
Not surprise at all. So I next I try to use the "Enter_PSsession" prior to the previous code
$JoinDomain = 'tataoui.com' # test lab domain name
$strVMName = 'DC' # Virtual machine / remote host name - where my test lab DC/DNS services reside
$DNSParameters = Import-Excel -Path $DataSourcePath -WorksheetName DNS
Enter-PSSession –CN $strVMName -Credential $DomainCredential
$DNSParameters | ForEach-Object {
# Create DNS entries
Add-DnsServerResourceRecord -ZoneName $JoinDomain -CN $strVMName -A -Name $_.Hostname -AllowUpdateAny -IPv4Address $_.IP -TimeToLive 01:00:00 -AgeRecord
}
I fix the permission error, but now the excel data/variable are not being passed through to the remote host.
Add-DnsServerResourceRecord : Cannot validate argument on parameter 'ZoneName'. The argument is null or empty. Provide an argument that is not null or empty, and then
try the command again.
At line:13 char:43
+ Add-DnsServerResourceRecord -ZoneName $JoinDomain -CN DC -A -Name ...
So how to find a happy medium where I can proper establish credential to the remote VM while passing my excel data over also?
You can refer to a local variable in a remote session with the 'using' qualifier, i.e. $using:JoinDomain.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference