Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Confusion with strings and invoke-expression

$
0
0

Hi,

I am trying to write a script that will change 3 DNS records on a DC in our domain and then i want to push the change to all other DCs.

Changing the DNS record:

#first delete the DNS record
Invoke-Command -Computername DC2 -ScriptBlock {cmd.exe /c dnscmd DC2 /RecordDelete contoso.com webmail A /f}
Invoke-Command -Computername DC2 -ScriptBlock {cmd.exe /c dnscmd DC2 /RecordDelete contoso.com smtp A /f}
Invoke-Command -Computername DC2 -ScriptBlock {cmd.exe /c dnscmd DC2 /RecordDelete corp.contoso.com outlook A /f}

#then create the DNS record with the new value
Invoke-Command -Computername DC2 -ScriptBlock {cmd.exe /c dnscmd DC2 /RecordAdd contoso.com webmail 60 A $args[0]} -ArgumentList $switch_ip
Invoke-Command -Computername DC2 -ScriptBlock {cmd.exe /c dnscmd DC2 /RecordAdd contoso.com smtp 60 A $args[0]} -ArgumentList $switch_ip
Invoke-Command -Computername DC2 -ScriptBlock {cmd.exe /c dnscmd DC2 /RecordAdd corp.contoso.com outlook 60 A $args[0]} -ArgumentList $switch_ip
This works fine.

I have a bunch of commands to run in order to force the DNS replication to all DCs (DCs will only replicate with their partner as defined by the KCC, so i cannot just use an array containing all DCs), so i tried the ugly script below

$replication_command1 = 'repadmin /replicate DC1 DC2 "DC=DomainDnsZones,DC=corp,DC=contoso,DC=com"'
$replication_command2 = 'repadmin /replicate DC1 DC2 "DC=DomainDnsZones,DC=corp,DC=contoso,DC=com"'

....

$replication_command24 = 'repadmin /replicate DC3 DC2 "DC=ForestDnsZones,DC=contoso,DC=com"'

$replication_command = '$replication_command'

for ($i = 1; $i -lt 25; $i++){

Invoke-Expression -Command $replication_command$i
}

This part does not work it just displays the expected value but just as if i would use write-host, not running the command but just writing to host.

Any help would be much appreciated.

Thanks


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>