Skip to content

Instantly share code, notes, and snippets.

@so0k
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save so0k/e96c3dc5a81ef89715f8 to your computer and use it in GitHub Desktop.
Save so0k/e96c3dc5a81ef89715f8 to your computer and use it in GitHub Desktop.
Param(
[Parameter(Mandatory=$true)]
[string]$hostName,
[Parameter(Mandatory=$true)]
[string]$hostIp
)
function IsAdministrator
{
$Identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$Principal = New-Object System.Security.Principal.WindowsPrincipal($Identity)
$Principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
}
# Check to see if we are currently running "as Administrator"
if (!(IsAdministrator))
{
# We are not running "as Administrator" - so relaunch as administrator
[string[]]$argList = @('-NoProfile', '-File', $MyInvocation.MyCommand.Path)
$argList += $MyInvocation.BoundParameters.GetEnumerator() | Foreach {"-$($_.Key)", "$($_.Value)"}
$argList += $MyInvocation.UnboundArguments
Start-Process PowerShell.exe -Verb Runas -WorkingDirectory $pwd -ArgumentList $argList
return
}
write "updating hosts file.."
$hostsfile = "$env:windir\System32\drivers\etc\hosts"
(gc $hostsfile ) -replace "[0-9.]+ $hostName", "$hostIp $hostName" | sc $hostsfile
write "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment