I am working on building a script that will access a large amount of files and pull a specific string out in order to assign as a variable.
The string is similar across all files which is not the issue. I am able to make this process function as an individual event (define a single source file)
$hostname_import = select-string .\test.txt -Pattern 'hostname ABC-.+'
$hostname = $hostname_import -replace '.+ ',''
The above would output the specific hostname identified in the target file (the second function is to trim the word hostname and the space) I can then use this to proceed with using the variable as needed to perform various actions.
The issue I am having is executing this function in a foreach loop so that I can grab the initial file- execute the select-string function (or similar) and then perform the data modification within the loop as expected.
For context- I am going through configuration files- and building a separate file based on those configurations to report on findings- part of the report building requires the hostname of the device.
--EDIT 1: After consulting with my rubber duck I will be attempting to import these files as CSVs in order to potentially reach a solution.
- Thanks @Otter for getting me through that. I had a previous script that used this function but for whatever reason I couldn't execute the function as anticipated. The difference between my previous script and this one was the $_.Fullname
Huge help!