I'm attempting to create virtual hosts having a Powershell script. It really works fine, however i am getting challenge with the encoding. I've attempted all of the -encoding switch values permitted without any luck.

Here's my script

$hostName = $args[0];
if ($args -eq $null -or $args.Length -eq 0)
{
    "No arguments. Supply the virtual host name as the first argument."
    exit
}
if (!(Test-Path -path D:\wamp\$hostName))
{
    $indexText = "<?php echo `"<h1>Holder for $hostName</h1>`";"
    New-Item D:\wamp\$hostName -type directory
    New-Item D:\wamp\$hostName\index.php -type file
    Out-File -FilePath D:\wamp\$hostName\index.php -InputObject $indexText -Encoding UTF8
}
$hosts = Get-Content C:\Windows\System32\drivers\etc\hosts
$hosts = $hosts + "127.0.0.1 $hostName.localhost"
Out-File -FilePath C:\Windows\System32\drivers\etc\hosts -InputObject $hosts
$conf = Get-Content "C:\Program Files (x86)\wamp\bin\apache\Apache2.2.11\conf\extra\httpd-vhosts.conf"
$conf = $conf + "<VirtualHost *:80>"
$conf = $conf + "    DocumentRoot `"D:/wamp/$hostName`""
$conf = $conf + "    ServerName $hostName.localhost"
$conf = $conf + "    ErrorLog `"logs/$hostName.localhost-error.log`""
$conf = $conf + "    CustomLog `"logs/$hostName.localhost-access.log`" common"
$conf = $conf + "</VirtualHost>"
Out-File -FilePath "C:\Program Files (x86)\wamp\bin\apache\Apache2.2.11\conf\extra\httpd-vhosts.conf" -InputObject $conf -Encoding ascii
Write-Host "Restart wamp server to effect changes"

Has anybody handled to obtain this working dependably or shall we be held onto a none-starter here? I'm able to always employ PHP rather :)

When the encoding isn't correct then Apache will not start under wampserver. Basically alter the encoding in notepad++ to ANSI then everything works fine, but powershell does not like -encoding ANSI.

while using -Encoding OEM option on Out-File appears to become a fix for your problem incase anybody else pops up against it.

For instance:-

Out-File -FilePath "C:\Program Files (x86)\wamp\bin\apache\Apache2.2.11\conf\extra\httpd-vhosts.conf" -InputObject $conf -Encoding OEM

Edit:-
I eventually solved this issue by spinning the script in PHP when i could never get results consistently in Powershell. Here's my script incase others think it is helpful, it is made to run in the command line eg:-

php d:/scripts/newVHost.php www

Can create a brand new virtual host call 'www'.

<?php
/**
 * create a new vhost on this server
 * 
 * @param host name
 */
$ds = DIRECTORY_SEPARATOR;

//for windows
$eol = chr(10) . chr(13);
//for other OS's
//$eol = "\n";

// you may need to alter this if your host file is elsewhere
$hostsFile = "C:\Windows\System32\drivers\etc\hosts";
$hostFH = fopen($hostsFile, 'a+');

// you may need to alter this if your htttp-vhosts.conf file is elsewhere
$apacheVHfile = "C:\Program Files\wamp\bin\apache\Apache2.2.17\conf\extra\httpd-vhosts.conf";
$apacheFH = fopen($apacheVHfile, 'a+');

if($argc <= 1) die("You must specify a host name\n");
$hostName = $argv[1];
$pathName = "d:{$ds}www{$ds}$hostName";
echo "\nCreating new virtual host '$hostName'\n";

if(!file_exists($pathName)) mkdir($pathName);

$writeStr = "\n127.0.0.1       $hostName.localhost";
writeFile($hostFH, $writeStr, $hostsFile);

$writeStr = "<VirtualHost *:80>
    DocumentRoot \"$pathName\"
    ServerName $hostName.localhost
    ErrorLog \"logs{$ds}$hostName.localhost-error.log\"
    CustomLog \"logs{$ds}$hostName.localhost-access.log\" common
</VirtualHost>$eol";

writeFile($apacheFH, $writeStr, $apacheVHfile);

fclose($hostFH);
fclose($apacheFH);

echo "New virtual host $hostName created. Restart WAMP server\n";
    echo "\n===================================\n\n";

function writeFile($file, $writeStr, $fileName = null){
    $written = fwrite($file, $writeStr);
    if($written){
        echo "\nModified $fileName\n";
    } else echo "\nCould not Modify $fileName\n";
}