Saturday, November 5, 2016

Change fixed DNS setting via PSExec to deploy and run the PowerShell

Change fixed DNS setting via PSExec and PowerShell

1. Copy the "change_dns.ps1" to the target machine "C:\source\PSTools\change_dns\"

The "change_dns.ps1" content:

$dnsservers = "8.8.8.8"
$computers = Get-Content env:computername
foreach ($comp in $computers)
{

$adapters = gwmi -q "select * from win32_networkadapterconfiguration where ipenabled='true'" -ComputerName $comp
foreach ($adapter in $adapters)
{
  $adapter.setDNSServerSearchOrder($dnsservers)
}
 
}


2. PsExec command to run the PowerShell remotely:
(Copy the "change_dns.ps1" into the same folder of PSExec)

C:\source\PSTools\change_dns>PSExec \\Computer1 -u "admin" -p PASSWORD PowerShell -NoProfile -ExecutionPolicy Bypass C:\source\PSTools\change_dns\change_dns.ps1


If success, will return the following value:


PsExec v2.2 - Execute processes remotely
Copyright (C) 2001-2016 Mark Russinovich
Sysinternals - www.sysinternals.com

__GENUS          : 2
__CLASS          : __PARAMETERS
__SUPERCLASS     :
__DYNASTY        : __PARAMETERS
__RELPATH        :
__PROPERTY_COUNT : 1
__DERIVATION     : {}
__SERVER         :
__NAMESPACE      :
__PATH           :
ReturnValue      : 0

 

PowerShell exited on Computer1 with error code 0.


Reference:
http://stackoverflow.com/questions/1786185/run-powershell-scripts-on-remote-pc
http://blog.danskingdom.com/allow-others-to-run-your-powershell-scripts-from-a-batch-file-they-will-love-you-for-it/
https://community.spiceworks.com/topic/385832-need-to-change-dns-servers-remotely-on-workstations-no-dhcp-server

Print Friendly and PDF
Share/Bookmark

No comments:

Post a Comment