Remotely modify configuration file on 20 servers

Hi, configuration file for Log Insight Agent product (liagent.ini) contains a line:

hostname=OldServerName.domainname.local

I want to modify it with new server name remotely with:

Invoke-Command -ComputerName $servers -ScriptBlock {
(Get-Content -Path ‘C:\ProgramData\VMware\Log Insight Agent\liagent.ini’) -replace “(?<=^hostname=).*”,“NewServerName.domainname.local” | Set-Content -Path ‘C:\ProgramData\VMware\Log Insight Agent\liagent.ini’ -Force
}

However as end result I got empty liagent.ini file. Doing this locally works fine. Any help would be appreciated.

 

Hmm I tried it on localhost as admin and it worked.

# Create new file with config change, remove old file, then rename new file 
Invoke-Command -ComputerName $servers -ScriptBlock {
    $old = ‘C:\ProgramData\VMware\Log Insight Agent\liagent.ini’ ;
    $new = ‘C:\ProgramData\VMware\Log Insight Agent\liagent2.ini’ ;
    (Get-Content -Path $old) -replace “(?<=^hostname=).*”,”NewServerName.domainname.local” | 
    Set-Content -Path $new -Force ; Remove-Item $old ; 
    Rename-Item -Path $new -NewName liagent.ini
}

It worked. Thx.