0

I am new to Powershell and I am wondering why this function doesn't work the way I want it too. The following function uncomments line in the hosts file of the computer specified. It works great! But I want the ability to output each line to the screen using a Write-Output and get the variable and store it in another array. This Write-Output doesn't do anything and I cannot add it to the array. Why? The above Write-Output works just fine.

function UnCommentHostsFile($ip){

   Write-Output "This Write-Output Works!"

   $hosts = $hosts | Foreach {

       #This part of the function works great!
       if ($_ -match $regex + $ip){
           $_.replace("#", "")

           #This is where I want to add it to an array and write-output but I can't.
       } 
       else {
           $_
       }

       #This does not output!
       Write-Output $_
   }
}

Any help would be greatly appreciated! Thanks! :)

2 Answers 2

1

To answer your question, when you do this:

$hosts = $hosts | Foreach {...}

everything that is output to the pipeline within that foreach script block is going to get re-directed to the variable $hosts.

Write-Output writes to the pipeline, and you're doing it within that script block.

If you want to explicitly write to the screen, use Write-Host, or Write-Verbose.

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

6 Comments

This didn't work. I tried putting Write-Host, Write-Output, and Write-Verbose. Nothing is outputting to the screen but it is still correctly writing to the file.
Nothing. Nothing gets written to the screen in any of the three cases. If I place the Write- statement inside the if statement it does not get run. If I place Write-Host here my original Write-Output was it prints out every line in the file. I only want the lines that have been changed.
Are you sure any of it matched the If condition?
Yes, cause the file changes. Every time I run it, it comments out about 10 lines in the hosts.file. The next time I run it i switch it and it comments out a different set of IP's. This is very strange.
It is strange indeed. Running that with test data, I get it written to the console If I set $VerbosePreference to Continue I get Verbose console output along with it.
|
0

You don't really need the Write-Output. Leave it as an object, and you can format that object however you want outside the function. I updated it so that for each line if it matches your regex it replaces # with nothing as you already had it, but I removed the Else clause because it was pointless. I changed $_.replace("#", "") to $_ = $_.replace("#", "") so that it actually updates the line in $Hosts instead of just echoing it. Then when all is said and done it outputs the entire $hosts updated lines and all.

function UnCommentHostsFile($ip){

   $hosts = $hosts | Foreach {

       #This part of the function works great!
       if ($_ -match $regex + $ip){
           $_ = $_.replace("#", "")

           #This is where I want to add it to an array and write-output but I can't.
       } 

       #This does not output!
   }
   $Hosts
}

Edit: When you call the function it should spit back whatever is in $Hosts, so it should output to the screen. No need to write-output or anything. It will show up on screen for the user, unless you are redirecting the output to something, in which case just add a bit to the pipe that prints it to the screen.

1 Comment

I need to write to the screen, some non technical people will be using this script, and save it to an array to run operations on specific entries. $_.replace("#", "") worked just fine and your code works too.

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.