windows services - Powershell: Conditional change value within file -


i working on ps script following each machine hostname line in file

  1. stop service
  2. check .ini file port number
  3. conditionally update port number based on existing value
  4. start service

where having hard time understanding syntax step 3: if 'serverport=443', change 'serverport=444' elseif 'serverport=444', change 'serverport=443'

here far:

$servers = get-content 'c:\servers.txt'     foreach ($server in $servers){    # stop service , wait.    (get-service -computername $server -name servicename).stop     # logic see string. looking "serverport=%". detect if server port 444 or 443. if one, set other     # port config file    $port = get-content c:\config.ini | where-object {$_ -like 'serverport=*'}     # conditionally update port      if ($port -eq "serverport=443")     {         # update 444     }     elseif ($port -eq "serverport=444")     {         # update 443     }     else     {         write-host "value not detected within param"     }      #start service     (get-service -computername $server -name servicename).start } 

based on have going on here, think syntax have reopen file, re-search line , update it... quite inefficient when going on network... perhaps there more logical , simple way go this?

your appreciated!

-wes

i rewrote of script. check out:

# define name of service stop/start $servicename = 'wuauserv'; # list of server names text file $serverlist = get-content -path 'c:\servers.txt';  foreach ($server in $serverlist){     # stop service , wait     get-service -computername $server -name $servicename | stop-service;      # logic see string. looking "serverport=%". detect if server port 444 or 443. if one, set other      # read config.ini file $configfile variable     $configfilepath = "\\$server\c$\config.ini";     $configfile = get-content -path $configfilepath -raw;      if ($configfile -match 'serverport=443')     {         # change serverport 444         set-content -path $configfilepath -value ($configfile -replace 'serverport=443', 'serverport=444');     }     elseif ($configfile -match 'serverport=444') {         # change serverport 443         set-content -path $configfilepath -value ($configfile -replace 'serverport=444', 'serverport=443');     }     else {         write-host -object ('could not find matching serverport value in {0}' -f $configfilepath);     }      get-service -computername $server -name $servicename | start-service; } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -