VMware Cloud Community
vin01
Expert
Expert
Jump to solution

Passing data in the variable while doing invoke-vmscript

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

pastedImage_1.png

Error:

pastedImage_2.png

Regards Vineeth.K
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
vin01
Expert
Expert
Jump to solution

Hello LucD. This is not working for me!

when I executed this

$dnsserverinstallSub = $ExecutionContext.InvokeCommand.ExpandString($dnsserverinstall) 

$dnsserverinstallSub

pastedImage_0.png

$dnsserver contains multiple values. How to select exact value like ($dnsserver.Sitename =LIBPOC.abc.COM)

pastedImage_2.png

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
vin01
Expert
Expert
Jump to solution

Superb Its workingSmiley Happy

Regards Vineeth.K
0 Kudos
vin01
Expert
Expert
Jump to solution

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

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

vin01
Expert
Expert
Jump to solution

Yeah. This looks better way then earlier. Thanks for your suggestion. 

Regards Vineeth.K
0 Kudos
vin01
Expert
Expert
Jump to solution

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

pastedImage_0.png

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Is the reverse lookup zone created correctly?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vin01
Expert
Expert
Jump to solution

No Its creating Like this.

Its should be like this right '115.77.153.in-addr.arpa'

pastedImage_0.png

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
vin01
Expert
Expert
Jump to solution

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 supportSmiley Happy

Regards Vineeth.K
0 Kudos