I am working on a PS script that does the following for each machine where hostname is a line in a file
- Stop Service
- Check .ini file for port number
- CONDITIONALLY update port number based on existing value
- Start Service
Where I am having a hard time is understanding the syntax for step 3: If 'ServerPort=443', change it to 'ServerPort=444' ElseIf 'ServerPort=444', change it to 'ServerPort=443'
Here is where I am so far:
$servers = Get-Content 'C:\Servers.txt'
foreach ($server in $servers){
# stop service and wait.
(get-service -ComputerName $server -Name SERVICENAME).stop
# Logic to see string. Looking for "ServerPort=%". Detect if Server port is 444 or 443. If one, set to other
# Get Port from config file
$port = get-content C:\config.ini | Where-Object {$_ -like 'ServerPort=*'}
# Conditionally update port
IF ($port -eq "ServerPort=443")
{
# update to 444
}
ELSEIF ($port -eq "ServerPort=444")
{
# update to 443
}
ELSE
{
Write-Host "Value not detected within Param"
}
#start service
(get-service -ComputerName $server -Name SERVICENAME).start
}
Based on what I have going on here, I think that the syntax would have to REOPEN the file, re-search for the line and then update it... Quite inefficient when going over the network... perhaps there is a much more logical and simple way to go about this?
Your help is GREATLY appreciated!
-Wes