1

I am attempting to replace a string in a text file on a specific line number using PowerShell but the command is removing a majority of the content. I would like to leverage the line number because the string to be replaced occurs on several lines but only want the string on line 51 to be updated. I am currently attempting to use the following to perform the replacement:

$content = Get-Content "file.txt"
$contentUpdate = $content[51] -replace '"Endpoint": ""','"Endpoint": "bucket.s3.us-west-1.vpce.amazonaws.com",'
Set-Content file.txt $contentUpdate

When the above commands are run against the file referenced at the bottom of this question, only the following remains in the file:

 "Region": "",

File content:

{
 "Profile":{
 "ShareCreds" : true,
 "ShareProfile" : "",
 "ForceUpdateCreds" : false,
 "KeyAutoRotateDays": 0
 },
 "Mds": {
 "CommandWorkersLimit" : 5,
 "StopTimeoutMillis" : 20000,
 "Endpoint": "",
 "CommandRetryLimit": 15
 },
 "Ssm": {
 "Endpoint": "",
 "HealthFrequencyMinutes": 5,
 "CustomInventoryDefaultLocation" : "",
 "AssociationLogsRetentionDurationHours" : 24,
 "RunCommandLogsRetentionDurationHours" : 336,
 "SessionLogsRetentionDurationHours" : 336,
 "PluginLocalOutputCleanup": "",
 "OrchestrationDirectoryCleanup": ""
 },
 "Mgs": {
 "Region": "us-west-1",
 "Endpoint": "",
 "StopTimeoutMillis" : 20000,
 "SessionWorkersLimit" : 1000,
 "DeniedPortForwardingRemoteIPs" : [
 "169.254.169.254",
 "fd00:ec2::254",
 "169.254.169.253",
 "fd00:ec2::253"
 ]
 },
 "Agent": {
 "Region": "",
 "OrchestrationRootDir": "",
 "SelfUpdate": false,
 "TelemetryMetricsToCloudWatch": false,
 "TelemetryMetricsToSSM": true,
 "AuditExpirationDay" : 7,
 "LongRunningWorkerMonitorIntervalSeconds": 60
 },
 "Os": {
 "Lang": "en-US",
 "Name": "",
 "Version": "1"
 },
 "S3": {
 "Endpoint": "",
 "Region": "",
 "LogBucket":"",
 "LogKey":""
 },
 "Kms": {
 "Endpoint": ""
 }
}
asked Jul 12, 2022 at 14:10
1
  • 1
    Change $contentUpdate = $content[51] -replace ... to $content[51] = $content[51] -replace ..., then replace the last line with Set-Content file.txt $content (although it looks like 51 is not the correct line index) Commented Jul 12, 2022 at 14:26

1 Answer 1

4

Well, seeing that this is a JSON you're working with, I would treat it as such:

$content = Get-Content "file.txt" -Raw | ConvertFrom-Json
$content.S3.Endpoint = "bucket.s3.us-west-1.vpce.amazonaws.com"
$content | ConvertTo-Json | Set-Content "file.txt"

This way you avoid trying to index through an array and work with the objects themselves.

answered Jul 12, 2022 at 14:28
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.