Issue in editing conf file through script

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

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.

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

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