Skip to content

Instantly share code, notes, and snippets.

@markgodiy
Created February 2, 2023 14:06
Show Gist options
  • Select an option

  • Save markgodiy/d98df8d9eb369f096b408f58a0407de1 to your computer and use it in GitHub Desktop.

Select an option

Save markgodiy/d98df8d9eb369f096b408f58a0407de1 to your computer and use it in GitHub Desktop.
function Set-Lab-RemoteHostsFile {
<#
.EXAMPLE
Set-Lab-RemoteHostsFile -ComputerName <RemoteHostname> -InsertIP "214.47.x.x" -InsertDescription "RemoteServer"
#>
param(
[string]$ComputerName,
[string]$InsertIP,
[string]$InsertDescription
)
try {test-connection -cn $ComputerName -count 1 -quiet -ea 0 | out-null} catch {Write-Error -Message "Houston, we have a problem: No Ping." -ErrorAction Stop}
try {gsv -cn $ComputerName -name WinRM | Start-Service} catch {Write-Error -Message "Houston, we have a problem: WinRM Stopped." -ErrorAction Stop}
icm $ComputerName {
$insertLine = "`n$($args[0])`t$($args[1])"
Add-Content -Path $env:windir\System32\drivers\etc\hosts -Value $insertLine -Force
} -ArgumentList $InsertIP,$InsertDescription
$verify = Get-Content "\\$ComputerName\C$\Windows\System32\drivers\etc\hosts"
If ([bool]($verify | Select-String $InsertIP)) {"Success"} else {"Failed"}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment