0

I have the script below.

$log = "C:\DNSLog.txt"

############ GET THE LIST OF RECORDS
$zones = Get-Content "C:\dnslist.txt"

############ TRANSFER THE ZONES TO THE MASTER SERVER
Write-Host "Transferring the zones to the master server..."
foreach ($zone in $zones) {
    Write-Host $zone >> $log
    dnscmd . /zoneadd $zone /Secondary 10.x.x.x >> $log
}

This doesn't seem to be outputting what I need to the log file. All I am getting is the "write-host" portion. No feedback from the "dnscmd" command. I would expect to see this (because the zone already exists in this case, but you get the point for success/failure):

domain1.com

Command failed:  DNS_ERROR_ZONE_ALREADY_EXISTS     9609    0x2589

If I just do:

    Write-Host $zone
    dnscmd . /zoneadd $zone /Secondary 10.x.x.x >> $log

I get the dnscmd output and no "write-host" feedback (as expected). What am I doing wrong that won't let me get the "write-host AND the dnscmd output in the log file???

Many thanks!

1 Answer 1

1

Write-Host only writes to the console, and apparently its output cannot be redirected to a file. You should use Write-Output instead.

Notice that Write-Host has options for -Foreground and -Background color, which leads to the idea that it is really intended just for writing to the console window.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah that did it. Thank you so much. Still learning this nifty powershell tool. Lots of cmdlets and options to master. :) Thank you.
Can you "accept" the answer ? We both get a few extra points that way. Thanks !
Write-Host output can be redirected if you use Start-Transcript but Start-Transcript has the issue of not being able to capture exe output. That is fixed in V5. Write-Host output can also be captured like this: powershell.exe -command "& {Write-Host 'testing write-host'}" > test.log

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.