sitecore-hosts.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# # sitecore_hosts.ps1 # function Get-DefaultHosts() { return Join-Path -Path $env:windir -ChildPath '\System32\drivers\etc\hosts' } <# .SYNOPSIS Adds host entry to hosts file. .DESCRIPTION .PARAMETER IpAddress The IP address for host entry. .PARAMETER HostName The hostname for host entry. .PARAMETER Comment The comment/description for host entry. .PARAMETER Path The path to the hosts file. The default value is '%WINDIR%\System32\drivers\etc\hosts' .EXAMPLE New-SitecoreHostsEntry -IpAddress 127.0.0.2 -HostName 'HostName4' -Path $hostsFile -Verbose .EXAMPLE New-SitecoreHostsEntry -IpAddress 127.0.0.2 -HostName 'HostName4' -Path $hostsFile -Verbose #> function New-SitecoreHostsEntry { [CmdletBinding(SupportsShouldProcess=$true)] param ( [parameter(Mandatory=$true)] [string]$IpAddress, [parameter(Mandatory=$true)] [string]$HostName, [parameter()] [string]$Comment, [parameter()] [string]$Path = (Get-DefaultHosts) ) Write-Verbose "Processing hosts file: '$Path'" Write-Verbose "Add host entry $IpAddress $HostName $Comment" $matchPattern = '^(?<IP>[0-9a-f.:]+)\s+(?<Hostname>[^\s#]+)(?<Comment>.*)$' $found = $false [System.Collections.ArrayList]$ArrayList = @() if( Test-Path -Path $Path ) { $content = Get-Content -Path $Path } else { Write-Warning "File '$Path' not exists." return } foreach( $line in $content) { if( ($line.StartsWith("#")) -or ($line.Length -eq 0)) { $ArrayList.Add($line) | Out-Null continue } elseif( $line -match $matchPattern ) { if( $Matches.Hostname -ne $null ) { if( $Matches.Hostname -eq $HostName ) { Write-Verbose "Host '$HostName' already exists with $($Matches.IP) $($Matches.Comment)" $found = $true } } } $ArrayList.Add($line) | Out-Null } if( -not $found ) { if( [string]::IsNullOrWhiteSpace($Comment) ) { $ArrayList.Add("$IPAddress`t$Hostname") | Out-Null } else { $ArrayList.Add("$IPAddress`t$Hostname`t#$Comment") | Out-Null } Set-Content -Path $Path -Value $ArrayList | Out-Null } } <# .SYNOPSIS Removes host entry from hosts file. .DESCRIPTION .PARAMETER HostName The hostname for host entry. .PARAMETER Path The path to the hosts file. The default value is '%WINDIR%\System32\drivers\etc\hosts' .EXAMPLE Remove-SitecoreHostsEntry -HostName 'HostName4' #> function Remove-SitecoreHostsEntry { [CmdletBinding(SupportsShouldProcess=$true)] param ( [parameter(Mandatory=$true)] [string]$HostName, [parameter()] [string]$Path = (Get-DefaultHosts) ) Write-Verbose "Processing hosts file: '$Path'" Write-Verbose "Delete host entry $HostName" $matchPattern = '^(?<IP>[0-9a-f.:]+)\s+(?<Hostname>[^\s#]+)(?<Comment>.*)$' $found = $false [System.Collections.ArrayList]$ArrayList = @() if( Test-Path -Path $Path ) { $content = Get-Content -Path $Path } else { Write-Warning "File '$Path' not exists." return } foreach( $line in $content) { if( ($line.StartsWith("#")) -or ($line.Length -eq 0)) { $ArrayList.Add($line) | Out-Null continue } elseif( $line -match $matchPattern ) { if( $Matches.Hostname -ne $null ) { if( $Matches.Hostname -eq $HostName ) { Write-Verbose "Host '$HostName' exists with $($Matches.IP) $($Matches.Comment)" $found = $true } } } if( -not $found ) { $ArrayList.Add($line) | Out-Null } } if( $found ) { Set-Content -Path $Path -Value $ArrayList | Out-Null } } Export-ModuleMember -Function New-SitecoreHostsEntry Export-ModuleMember -Function Remove-SitecoreHostsEntry |