Issue in editing conf file through script

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Tim Pringle Tim Pringle 8 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #34424
    Profile photo of Ankit Parmar
    Ankit Parmar
    Participant

    Hi,

    I have one file (inputs.conf) which i want to edited through script. I am pasting first 3 lines of the inputs.conf file below to get script easily understand –

    #[SplunkAzure://Collection1]
    #storageAccountKey = Your storage account key
    #storageAccountName = Your storage account name

    Now i have a script given below –

    $webConfig = 'C:\Program Files\SplunkUniversalForwarder\etc\apps\SplunkAzure\local\inputs.conf'
    Get-Content $webConfig
    $obj = 'SplunkAzure://Collection1' | where {$_storageAccountKey -eq 'Your storage account key'}
    $obj.storageAccountKey = '78945'

    Now when i am trying to replace "78945" with 'Your storage account key' its given below error –

    The property 'storageAccountKey' cannot be found on this object. Verify that the property exists and can be set.At D:\Ankit\data\CentOS\New folder\scripts\inputsfile.ps1:4 char:1
    + $obj.storageAccountKey = '78945' .. Any suggestions .. Kindly advise..

    Thanks
    Ankit

    #34425
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    Hi Ankit,

    I don't see anywhere that you are setting $obj to have these properties.

    $obj = 'SplunkAzure://Collection1' | where {$_storageAccountKey -eq 'Your storage account key'}

    Will just treat 'SplunkAzure://Collection1' as a string, and will always return a null value from that statement.

    #34426
    Profile photo of Ankit Parmar
    Ankit Parmar
    Participant

    thanks for your input Tim .. do you suggest me or else make corrections in my script so that i will do it for the rest other part.

    Thanks
    Ankit

    #34443
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    The format of the file you show appears to be of a .ini type.

    As you're doing a straightforward replace, then i'd suggest a better idea might be to do something like the following

      $fileContent = Get-Content D:\temp\test.txt | Out-String 
      $fileContent = $fileContent -replace 'our storage account key','78945'
      $fileContent | Out-File -FilePath d:\temp\test.txt -force
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.