Created
February 2, 2023 14:06
-
-
Save markgodiy/d98df8d9eb369f096b408f58a0407de1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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