perl - Powershell Host File edit -


guys i'm having issues converting perl script powershell, need help. in host file of our machines, have of url's our test environments blocked. in perl script, based on environment selected, comment out line of environment selected allow access , block others testers can't mistakenly things in wrong environment.

i need converting powershell

below have in perl:

sub edithosts { print "editing hosts file...\n"; $file = 'c:\\windows\\system32\\drivers\\etc\\hosts'; $data = readfile($file); @lines = split /\n/, $data; $row = '1'; open (file, ">$file") or die "cannot open $file\n"; foreach $line (@lines) {     if ($line =~ m/$web/) {          print file '#'."$line\n"; }     else {         if ($row > '21') {             $line =~ s/^\#*127\.0\.0\.1/127\.0\.0\.1/;             $line =~ s/[#;].*$//s; }         print file "$line\n"; }     $row++; } close(file); } 

here i've tried in powershell:

foreach ($line in get-content "c:\windows\system32\drivers\etc\hosts") { if ($line -contains $web) { $line + "#" } 

i've tried variation including set-content used in host file, etc.

any appreciated!

thanks, grant

-contains "set" operator, not substring operator. try .contains() or -like.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

How to merge four videos on one screen with ffmpeg -

c - getting error: cannot take the address of an rvalue of type 'int' -