Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Check if the script is running with administrative privileges
- if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
- Write-Host "Please run this script as an administrator."
- Exit
- }
- # Prompt for IP address
- $ipAddress = Read-Host -Prompt "Enter IP address"
- # Prompt for hostname
- $hostname = Read-Host -Prompt "Enter hostname"
- # Prompt for directory
- $directory = Read-Host -Prompt "Enter directory"
- # Construct the hosts file entry
- $hostsEntry = "$ipAddress`t$hostname"
- # Path to the hosts file
- $hostsFilePath = "$env:SystemRoot\System32\drivers\etc\hosts"
- # Check if the entry already exists in the hosts file
- if (Select-String -Path $hostsFilePath -Pattern "^$hostname") {
- Write-Host "Entry for $ipAddress already exists in hosts file."
- }
- else {
- # Add the entry to the hosts file
- Add-Content -Path $hostsFilePath -Value $hostsEntry
- Write-Host "Entry added to hosts file:"
- Write-Host $hostsEntry
- }
- # Check if in vhosts
- $apacheVhostsFile = "C:\Program Files\XAMPP\apache\conf\extra\httpd-vhosts.conf"
- $apacheEntry = @"
- <VirtualHost $hostname:80>
- DocumentRoot "$directory"
- ServerName "$hostname"
- <Directory "$directory">
- Options All
- AllowOverride All
- Allow from all
- </Directory>
- </VirtualHost>
- "@
- # Check if the entry already exists in the vhosts file
- if (Select-String -Path $apacheVhostsFile -Pattern '^ServerName "$hostname"') {
- Write-Host "Entry for $hostname exists in vhosts file."
- }
- else {
- # Add the entry to the hosts file
- Add-Content -Path $apacheVhostsFile -Value $apacheEntry
- Write-Host "Entry added to hosts file:"
- Write-Host $apacheVhostsFile
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement