This is my first attempt in creating a powershell module. I moved from VBS and JScript for my automation scripts to Powershell recently so please go easy on me;
This Module consists of a set of functions to get and set virtual hosts in the apache vhosts.conf file and in the windows hosts file.
I've added Test-Apache.ps1 to the repository to run a few tests.
Using Git:
git clone [email protected]:Marcuzzo/Powershell-Apache-Module.git
cp Apache\ powershell\ Environment.lnk 'C:/xampp'
mkdir 'C:/xampp/tools'
cp -r tools/* 'C:/xampp/tools'
This is only valid if you've installed XAMPP under C:\Xampp, change where needed
Because some of the functions will read from and write to a file in the system32 directory you need to be running it with elevated permissions.
These functions have been tested on a Windows 8.1 machine:
Major Minor Build Revision
----- ----- ----- --------
6 3 9600 0
with the following $PSVersionTable info:
Name Value
---- -----
PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.34014
BuildVersion 6.3.9600.17090
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2
I've only tested this with apache that comes with XAMPP but I'm not expecting any mayor changes for other Apache MySQL and PHP packages
This module and it's additional files are distributed under the GNU General Public License v3.
If you want to contribute to this Powershell module or if you have any ideas to improve it please fork it and make send me a pull request.
The Get-ApacheVirtualHost function will return all virtual hosts that have been configured in the file httpd-vhosts.conf which is located in the 'conf/extra' subdirectory of the apache installation directory.
The Get-ApacheVirtualHost function has no mandatory parameters
Get-ApacheVirtualHost
to get all the virtual hosts that start with 'test' you can run the following:
Get-ApacheVirtualHost -Filter { $_.ServerName -like 'test*' }
The Get-ApacheVirtualHost function will return an object of Type PSCustomObject.
PS C:\CmdLets\Apache-Vhost> Get-ApacheVirtualHost | Get-Member
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Directory NoteProperty System.Management.Automation.PSCustomObject
DocumentRoot NoteProperty System.String
ErrorLog NoteProperty System.String
ServerAdmin NoteProperty System.String
ServerAlias NoteProperty System.String
ServerName NoteProperty System.String
The Directory Object:
PS C:\CmdLets\Apache-Vhost> (Get-ApacheVirtualHost).Directory | Get-Member
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Allow NoteProperty System.String
AllowOverride NoteProperty System.String
Order NoteProperty System.String
Path NoteProperty System.String
Require NoteProperty System.String
The New-ApacheVirtualHost will add a new virtual host to the httpd-vhosts.conf file.
New-ApacheVirtualHost [-InputObject] <PSObject> [-WhatIf] [<CommonParameters>]
New-ApacheVirtualHost [-ServerName] <String> [-ServerAdmin <String>]
[-DocumentRoot] <String> [-ServerAlias <String>] [-ErrorLog <String>]
[-CustomLog <String>] [-Directory <String>] [-AllowOverride <String>] [-Order <String>]
[-Allow <String>] [-Require <String>] [-Force] [-WhatIf] [<CommonParameters>]
New-ApacheVirtualHost -ServerName "SomeHost.local" `
-DocumentRoot "C:\sites\SomeHost.local"
This will create the following Virtual host in the vhosts.conf file:
<VirtualHost *:80>
ServerAdmin Marcuzzo
DocumentRoot "C:\sites\SomeHost.local"
ServerName SomeHost.local
ServerAlias SomeHost.local
ErrorLog "logs/SomeHost.local-error.log"
CustomLog "logs/SomeHost.local-custom.log"
<Directory "C:\sites\SomeHost.local">
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
This Function will return the PSObject of the created virtual host
There are 2 ways to remove a Virtual host:
- by using the
Name
andDocumentRoot
parameters. - by piping the output of the
Get-ApacheVirtualHost
to theRemove-ApacheVirtualHost
Cmdlet
Remove-ApacheVirtualHost [-Name] <string> [-DocumentRoot] <string> [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]
Remove-ApacheVirtualHost [-InputObject] <psobject> [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]
Parameters:
Remove-ApacheVirtualHost -Name "SomeHost.local" -DocumentRoot "C:\sites\SomeHost.local" -Confirm:$false
Pipeline:
Get-ApacheVirtualHost -Filter{ $_.ServerName -eq "SomeHost.local" } | Remove-ApacheVirtualHost -Confirm:$false
See the Test-Apache.ps1 script for a working example
To get all WindowsHosts run:
Get-WindowsHost | Format-Table
To get a specific host you can use the Where-Object CmdLet
Get-WindowsHost -Filter { $_.HostName -eq 'SomeHost.local' }
PS C:\CmdLets\Apache-Vhost> Get-WindowsHost | gm
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
HostName NoteProperty System.String HostName=SomeHost.local
IPAddress NoteProperty System.String IPAddress=127.0.0.1
to Create a new Host with the localhost IP Addres and the name SomeHost.local use:
New-WindowsHost -Name "SomeHost.local"
The function also accepts a PSObject through the pipeline. this is used by the Remove-WindowsHost Function:
$oWindowsHost | New-WindowsHost
You can remove a WindowsHost by using the -(Host)Name and the optional IPAddress Parameter.
Remove-WindowsHost -Name "SomeHost.local"
Or you can get the Host object using the Get-WindowsHost function and pipe it to the Remove-WindowsHost Function
Get-WindowsHost -Filter { $_.HostName -eq 'SomeHost.local' } | Remove-WindowsHost