PowerShell script to show content of hosts file or remote Windows.
# Prompt the user to input the hostname or IP of the remote host $RemoteHost = Read-Host 'Please enter the hostname or IP of the remote host' # Ensure PowerShell remoting is enabled and configured on both local and remote machines try { # Use Invoke-Command to run the script block on the remote machine $scriptBlock = { # Path to the hosts file $hostsPath = 'C:\Windows\System32\drivers\etc\hosts' # Check if the hosts file exists if (Test-Path $hostsPath) { # Read each line of the hosts file and output them individually Get-Content $hostsPath | ForEach-Object { Write-Output $_ } } else { throw 'Hosts file not found.' } } # Execute the script block on the remote machine Invoke-Command -ComputerName $RemoteHost -ScriptBlock $scriptBlock } catch { # Handle errors, such as connectivity issues or file not found Write-Error "An error occurred: $_" }
PowerShell script to modify content of hosts file or remote Windows.
# Prompt the user to input the hostname or IP of the remote host $RemoteHost = Read-Host 'Please enter the hostname or IP of the remote host' # Prompt the user for the new record to be added to the hosts file $NewRecord = Read-Host 'Please enter the new record to add to the hosts file' # Ensure PowerShell remoting is enabled and configured on both local and remote machines try { # Define the script block for adding a new record to the hosts file $scriptBlock = { param($NewRecord) # Path to the hosts file $hostsPath = 'C:\Windows\System32\drivers\etc\hosts' # Check if the hosts file exists if (Test-Path $hostsPath) { # Append the new record to the hosts file #Add-Content -Path $hostsPath -Value $NewRecord Add-Content -Path $hostsPath -Value "`n$NewRecord" Write-Output "New record added successfully: `n$NewRecord" } else { throw 'Hosts file not found.' } } # Execute the script block on the remote machine, passing the new record as an argument Invoke-Command -ComputerName $RemoteHost -ScriptBlock $scriptBlock -ArgumentList $NewRecord } catch { # Handle errors, such as connectivity issues or file not found Write-Error "An error occurred: $_" }
PowerShell script to delete content of hosts file or remote Windows.
# Prompt the user to input the hostname or IP of the remote host $RemoteHost = Read-Host 'Please enter the hostname or IP of the remote host' # Prompt the user for the record to be deleted from the hosts file $RecordToDelete = Read-Host 'Please enter the record to delete from the hosts file' # Ensure PowerShell remoting is enabled and configured on both local and remote machines try { # Use Invoke-Command to run the script block on the remote machine $scriptBlock = { param ($RecordToDelete) # Path to the hosts file $hostsPath = 'C:\Windows\System32\drivers\etc\hosts' # Check if the hosts file exists if (Test-Path $hostsPath) { # Read the hosts file and exclude the record to delete $newContent = Get-Content $hostsPath | Where-Object { $_ -notmatch $RecordToDelete } # Write the updated content back to the hosts file Set-Content -Path $hostsPath -Value $newContent } else { throw 'Hosts file not found.' } } # Execute the script block on the remote machine, passing the record to delete as a parameter Invoke-Command -ComputerName $RemoteHost -ScriptBlock $scriptBlock -ArgumentList $RecordToDelete } catch { # Handle errors, such as connectivity issues or file not found Write-Error "An error occurred: $_" }
VÕ TÌNH THƯƠNG