0

I am working on a PS script that does the following for each machine where hostname is a line in a file

  1. Stop Service
  2. Check .ini file for port number
  3. CONDITIONALLY update port number based on existing value
  4. 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

Wes
  • 290
  • 3
  • 18
  • 5
    You can't update a line in the file in-place. You'll have to read the file into a variable, modify the relevant line there and then re-write the entire file with the contents of the variable. – mjolinor Mar 10 '14 at 16:07
  • Do you have specific functions I should research for this? I'm a little hesitant to re-write the whole file. – Wes Mar 10 '14 at 16:14
  • The file shouldn't be more than a kb or two (hopefully), replacing the file should be a none issue. Just load the file to memory, make your adjustments in memory and then write the corrected file back to the remote machine. – Austin T French Mar 10 '14 at 16:16
  • I don't know what you mean by "specific functions". That's just the way the filesystem works. You can append data to a file, but you can't make changes in the middle of a file without re-writing the entire file. – mjolinor Mar 10 '14 at 16:36
  • The config file is about 220 lines total. Additionally, I have no idea how to load a file in to memory to do this manipulation? I kind of just piece examples together to build my scripts :D – Wes Mar 10 '14 at 16:37
  • Mjolinor,There is a replace function... – Wes Mar 10 '14 at 16:38

1 Answers1

1

I rewrote some of your script. Check this out:

# Define the name of the service to stop/start
$ServiceName = 'wuauserv';
# Get a list of server names from a text file
$ServerList = Get-Content -Path 'C:\Servers.txt';

foreach ($Server in $ServerList){
    # Stop service and wait
    Get-Service -ComputerName $Server -Name $ServiceName | Stop-Service;

    # Logic to see string. Looking for "ServerPort=%". Detect if Server port is 444 or 443. If one, set to other

    # Read the config.ini file into $ConfigFile variable
    $ConfigFilePath = "\\$Server\c$\config.ini";
    $ConfigFile = Get-Content -Path $ConfigFilePath -Raw;

    if ($ConfigFile -match 'ServerPort=443')
    {
        # Change ServerPort to 444
        Set-Content -Path $ConfigFilePath -Value ($ConfigFile -replace 'ServerPort=443', 'ServerPort=444');
    }
    elseif ($ConfigFile -match 'ServerPort=444') {
        # Change ServerPort to 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;
}
Wes
  • 290
  • 3
  • 18
  • 1
    Awesome man! That is so helpful! you missed the ConfigFilePath variable in the Get-Content line, but other than that it looks to be working perfectly! Thank you so much. – Wes Mar 10 '14 at 17:32
  • 1
    Gotcha - I just approved the edit. Thanks for catching that! Glad it's working :) Another approach would be to write the function as a self-containing function, and then deploy it to the remote systems over PowerShell Remoting (WinRM). –  Mar 10 '14 at 17:36
  • yeah, I can see the value of that, but I kind of like not having to have anything live on the server to execute. Thanks again for your awesome assistance. – Wes Mar 10 '14 at 20:36