Advertisement
RehabCZ

Powershell XAMMP script

Mar 30th, 2024 (edited)
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 1.78 KB | Source Code | 0 0
  1. # Check if the script is running with administrative privileges
  2. if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
  3.     Write-Host "Please run this script as an administrator."
  4.     Exit
  5. }
  6.  
  7. # Prompt for IP address
  8. $ipAddress = Read-Host -Prompt "Enter IP address"
  9.  
  10. # Prompt for hostname
  11. $hostname = Read-Host -Prompt "Enter hostname"
  12.  
  13. # Prompt for directory
  14. $directory = Read-Host -Prompt "Enter directory"
  15.  
  16. # Construct the hosts file entry
  17. $hostsEntry = "$ipAddress`t$hostname"
  18.  
  19. # Path to the hosts file
  20. $hostsFilePath = "$env:SystemRoot\System32\drivers\etc\hosts"
  21.  
  22. # Check if the entry already exists in the hosts file
  23. if (Select-String -Path $hostsFilePath -Pattern "^$hostname") {
  24.     Write-Host "Entry for $ipAddress already exists in hosts file."
  25. }
  26. else {
  27.     # Add the entry to the hosts file
  28.     Add-Content -Path $hostsFilePath -Value $hostsEntry
  29.     Write-Host "Entry added to hosts file:"
  30.     Write-Host $hostsEntry
  31. }
  32.  
  33. # Check if in vhosts
  34. $apacheVhostsFile = "C:\Program Files\XAMPP\apache\conf\extra\httpd-vhosts.conf"
  35.  
  36. $apacheEntry = @"
  37. <VirtualHost $hostname:80>
  38.    DocumentRoot "$directory"
  39.    ServerName "$hostname"
  40.    <Directory "$directory">
  41.        Options All
  42.        AllowOverride All
  43.        Allow from all
  44.    </Directory>
  45. </VirtualHost>
  46. "@
  47.  
  48. # Check if the entry already exists in the vhosts file
  49. if (Select-String -Path $apacheVhostsFile -Pattern '^ServerName "$hostname"') {
  50.     Write-Host "Entry for $hostname exists in vhosts file."
  51. }
  52. else {
  53.     # Add the entry to the hosts file
  54.     Add-Content -Path $apacheVhostsFile -Value $apacheEntry
  55.     Write-Host "Entry added to hosts file:"
  56.     Write-Host $apacheVhostsFile
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement