I have data in $dnsserver variable but while doing invoke-vmscript I can't able to pass the data inside the guest os. How to do it?
$dnsserver
$dnsserverinstall= @'
Install-WindowsFeature DNS -includeManagementTools
Add-DnsServerPrimaryZone -Name $dnsserver.Sitename -ZoneFile $dnsserver.Sitename
Add-DnsServerPrimaryZone -NetworkId $dnsserver.IP -ZoneFile $dnsserver.Sitename
Add-DnsServerResourceRecordA -Name $dnsserver.VMName -IPv4Address $dnsserver.IP -ZoneName $dnsserver.Sitename -CreatePtr
'@
Invoke-VMScript -VM 'LIBPRO02' -GuestUser 'administrator' -GuestPassword 'password' -ScriptType Powershell -ScriptText $dnsserverinstall
Error:
In that case, try like this
$dnsserverinstall= @'
Install-WindowsFeature DNS -includeManagementTools
Add-DnsServerPrimaryZone -Name $($dnsserver.Sitename) -ZoneFile $($dnsserver.Sitename)
Add-DnsServerPrimaryZone -NetworkId $($dnsserver.IP) -ZoneFile $($dnsserver.Sitename)
Add-DnsServerResourceRecordA -Name $($dnsserver.VMName) -IPv4Address $($dnsserver.IP) -ZoneName $($dnsserver.Sitename) -CreatePtr
'@
$dnsserverinstallSub = $ExecutionContext.InvokeCommand.ExpandString($dnsserverinstall)
Invoke-VMScript -VM 'LIBPRO02' -GuestUser 'administrator' -GuestPassword 'password' -ScriptType Powershell -ScriptText $dnsserverinstallSub
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Use the ExpandString method.
See alse my Here strings and the ExpandString method dive.
$dnsserverinstall= @'
Install-WindowsFeature DNS -includeManagementTools
Add-DnsServerPrimaryZone -Name $dnsserver.Sitename -ZoneFile $dnsserver.Sitename
Add-DnsServerPrimaryZone -NetworkId $dnsserver.IP -ZoneFile $dnsserver.Sitename
Add-DnsServerResourceRecordA -Name $dnsserver.VMName -IPv4Address $dnsserver.IP -ZoneName $dnsserver.Sitename -CreatePtr
'@
$dnsserverinstallSub = $ExecutionContext.InvokeCommand.ExpandString($dnsserverinstall)
Invoke-VMScript -VM 'LIBPRO02' -GuestUser 'administrator' -GuestPassword 'password' -ScriptType Powershell -ScriptText $dnsserverinstallSub
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hello LucD. This is not working for me!
when I executed this
$dnsserverinstallSub = $ExecutionContext.InvokeCommand.ExpandString($dnsserverinstall)
$dnsserverinstallSub
$dnsserver contains multiple values. How to select exact value like ($dnsserver.Sitename =LIBPOC.abc.COM)
In that case, try like this
$dnsserverinstall= @'
Install-WindowsFeature DNS -includeManagementTools
Add-DnsServerPrimaryZone -Name $($dnsserver.Sitename) -ZoneFile $($dnsserver.Sitename)
Add-DnsServerPrimaryZone -NetworkId $($dnsserver.IP) -ZoneFile $($dnsserver.Sitename)
Add-DnsServerResourceRecordA -Name $($dnsserver.VMName) -IPv4Address $($dnsserver.IP) -ZoneName $($dnsserver.Sitename) -CreatePtr
'@
$dnsserverinstallSub = $ExecutionContext.InvokeCommand.ExpandString($dnsserverinstall)
Invoke-VMScript -VM 'LIBPRO02' -GuestUser 'administrator' -GuestPassword 'password' -ScriptType Powershell -ScriptText $dnsserverinstallSub
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Superb Its working
One last question.
With this script I can able to configure DNS inside the guest OS.
After DNS is configured I need to create host records.to achieve this I am importing a list of vmnames with required info from a csv ($inputfile) but unable to create host records.
$inputfile=Import-Csv -Path 'C:\Users\vk185112\Desktop\answerfilecsv.csv' -UseCulture
$dnsserver
$dnsserverinstall= @'
$inputfile
Install-WindowsFeature DNS -includeManagementTools
Add-DnsServerPrimaryZone -Name $($dnsserver.Sitename) -ZoneFile $($dnsserver.Sitename)
Add-DnsServerPrimaryZone -NetworkId $($dnsserver.IP) -ZoneFile $($dnsserver.Sitename)
foreach($dnsrecords in $inputfile){
Add-DnsServerResourceRecordA -Name $($dnsrecords.VMName) -IPv4Address $($dnsrecords.IP) -ZoneName $($dnsrecords.Sitename) -CreatePtr
}
'@
$dnsserverinstallSub = $ExecutionContext.InvokeCommand.ExpandString($dnsserverinstall)
Invoke-VMScript -VM 'VM1' -GuestUser 'administrator' -GuestPassword 'Paswd' -ScriptType Powershell -ScriptText $dnsserverinstallSub
I think the best way to do this would be in 2 stages.
First install and setup the DNS server and zone.
Then create the A- and PRT-records.
Something like this
$dnsserverinstall= @'
Install-WindowsFeature DNS -includeManagementTools
Add-DnsServerPrimaryZone -Name $($dnsserver.Sitename) -ZoneFile $($dnsserver.Sitename)
Add-DnsServerPrimaryZone -NetworkId $($dnsserver.IP) -ZoneFile $($dnsserver.Sitename)
'@
$dnsserverinstallSub = $ExecutionContext.InvokeCommand.ExpandString($dnsserverinstall)
Invoke-VMScript -VM 'VM1' -GuestUser 'administrator' -GuestPassword 'Paswd' -ScriptType Powershell -ScriptText $dnsserverinstallSub
$addRecord = @'
Add-DnsServerResourceRecordA -Name $($dnsrecords.VMName) -IPv4Address $($dnsrecords.IP) -ZoneName $($dnsrecords.Sitename) -CreatePtr
'@
$inputfile = Import-Csv -Path 'C:\Users\vk185112\Desktop\answerfilecsv.csv' -UseCulture
foreach($dnsrecords in $inputfile){
$addRecordSub = $ExecutionContext.InvokeCommand.ExpandString($addRecord)
Invoke-VMScript -VM 'VM1' -GuestUser 'administrator' -GuestPassword 'Paswd' -ScriptType Powershell -ScriptText $addRecordSub
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yeah. This looks better way then earlier. Thanks for your suggestion.
The corresponding PTR record is not creating. What could be the issue?
I am suspecting the reverse lookup zone is created wrongly.
Add-DnsServerPrimaryZone -NetworkId 153.77.115.194 -ZoneFile LIBPOC.Test.COM
Is the reverse lookup zone created correctly?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
No Its creating Like this.
Its should be like this right '115.77.153.in-addr.arpa'
Try doing the creation of the reverse lookup zone as
Add-DnsServerPrimaryZone -NetworkId "$($dnsserver.IP.Split('.')[0..2] -join '.').0/24" -ReplicationScope Domain
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I can able to create like this.
Add-DnsServerPrimaryZone -NetworkId "$($dnsserver.IP.Split('.')[0..2] -join '.').0/24" -ZoneFile $($dnsserver.Sitename)
Thanks LucD for all your support