Admitted newbie. I feel like this should be an easy one, and am certain I’ve even done this before, but in digging through the few scripts I’ve created, I can’t find it…
I have a file called web.config at \hostname\C$\Folder\Subfolder\Web.config. I need to find a specific line containing the string “license.log” and turn that one line into a variable. I know how to do it if I know what specific line number it’s on, but this file can have different variations, so that line number can be different on any system.
I also need to be able to split the new variable when I get that far, so any tricks that need to be used to make that happen, I’ll need help in adding that to my arsenal as well… I know how to split, but in case there’s something specific I need when splitting from the text file, please say so!
#so far I've tried
$WC = gc "\\hostname\C$\Folder\Subfolder\Web.config" | select=string "*license.log*"
#and
$WC = gc "\\hostname\C$\Folder\Subfolder\Web.config" | -line -like "*license.log*"
#try not to laugh - I admittedly just started working with PowerShell last week :)
Get-Content returns an array of lines, so you need to do a foreach to check each line for your string. Using a example web.config, you can do something like this:
$webConfig = Get-Content .\Desktop\web.config
foreach ($line in $webConfig) {
if ($line -like '*authentication mode=*') {
[regex]::match($line,'"(.*?)"').value
}
}
Output:
"Windows"
However, a web.config is XML, so it’s a bit easier to parse it as XML:
You might start with learning the basics of Powershell. This way you would understand better how to modify the examples to your special needs. Here are some good starting points:
Thanks. I’ve gone through the “Getting Started with Microsoft Powershell” through the Microsoft Virtual Academy. I’ve used the knowledge from that and a few questions in between to get a ton done in the last week… And I have every intention of doing more, but the boss put me on a project that he wants done asap… Just one more piece to the puzzle and it’s done… Then I can start studying more. And in reference to my other post and not posting my work - it’s because everything I saw made absolutely no sense, so I really had nothing to show… My script is about a mile long, but that piece has stumped me. Once I’m done with lunch I’ll try your suggestion - that’s the last piece of the puzzle. Thanks for the input, I greatly appreciate it.