2

We want to eliminate the existence of a bad config file we've identified (and also have the manual fix for) from within every users appdata folder. The config file only exists here and there and not within every user. So I am trying to piece together a script that will return true/false if specified text is matched with get-content on config.xml. I want to run this against each user profile on every machine and output results to csv.

So far I've run into an issue. Still learning powershell so any help much appreciated.

$Users = "C:\Users"
$x = "This is the text I'm looking for"


Foreach ($Username in $Users)

{
$Path = "$Users\$Username\AppData\Local\MyApp\Settings\Config.xml"
$y = Get-Content -Path $Path
If ($y -match $x)

{
    "true"

}
else 
{
    "false"
}
}

As you can see i haven't gotten round to the csv part yet because I'm not able to get the correct path to generate for get-content.

Rather than having the correct path which I hoped as the foreach loop worked it's way through would be:

"C:\Users\Username1\AppData\Local\MyApp\Settings\Config.xml"
"C:\Users\Username2\AppData\Local\MyApp\Settings\Config.xml"
"C:\Users\Username3\AppData\Local\MyApp\Settings\Config.xml"

I am instead getting the following error:

Get-Content : Cannot find path 'C:\Users\C:\Users\AppData\Local\MyApp\Settings\Config.xml' because it does not exist.
At C:\Users\mastaxx\Desktop\test.ps1:9 char:6
+ $y = Get-Content -Path $Path
+      ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\C:\Use...ings\Config.xml:String) [Get-Content], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

My seemingly silly assumption here is that $Users would be "C:\Users" (which i believe it is because I've defined it) and $Username would be "Username1" (which clearly it is not). Instead I can see it's also "C:\Users" in this case, making the path being constructed to be this incorrect one:

C:\Users\C:\Users\AppData\Local\MyApp\Settings\Config.xml

I expected the foreach loop to treat each folder inside $Users as an individual object and become the $Username in each loop instance, allowing me to insert it into the config file $path like so:

$Users   $Username
  ↓          ↓
C:\Users\Username1\AppData\Local\MyApp\Settings\Config.xml

I'm not entirely sure what i'm doing wrong but i know I'm definitely doing something silly here. Would very much appreciate anyone with better PS skills to help point it out?

9
  • This is not clear. Config files from what Application? Finding User Profile errors is probably next to impossible.
    – anon
    Commented Jun 23, 2022 at 12:05
  • We know the manual fix for this and have been going around trashing the culprit config file and allowing the application to rebuild it from scratch on next launch. I've deliberately left out the application name for business purposes. All i want the script to do is match the text specified in $text and if true export config file user path to csv. This will allow me to go round all those user profiles over the weekend and apply to manual fix to each one. Which beats checking every single workstation individually.
    – Mastaxx
    Commented Jun 23, 2022 at 12:34
  • Sorry i meant to say $x not $text
    – Mastaxx
    Commented Jun 23, 2022 at 12:40
  • I've come closer to working this out. Might post an answer to my own question soon
    – Mastaxx
    Commented Jun 23, 2022 at 12:56
  • 1
    You should post the answer (that worked) to an actual answer on your post since you are allowed to answer your own questions you ask too. It could be helpful to future reading have the same issue you explain here. Commented Jun 24, 2022 at 1:29

1 Answer 1

1

I've managed to work out that i was missing Get-Childitem in the equation.

This is the correct way to do it, although I'm sure my PS is in need of major improvement, as I was unable to get a properly formatted CSV to export so I opted for the messy approach which was to output an individual CSV for each instance of true, but at least it works!

$computers = Get-Content -Path "C:\computers.txt"



foreach ($comp in $computers) {

$i = "\\$comp\C$\Users\"
$AppData = "\AppData\Local\MyApp\Settings\Config.xml"
$text = "Text I'm Looking For*"



Get-ChildItem -Path $i
Foreach ($folder in Get-Childitem $i)
{

$ReadConfig = Get-Content -Path "$i$folder$AppData"
If ($ReadConfig -match $text)

{
  
  out-file "c:\results\$comp $folder.csv"
  
}
else 
{
    "false"
}
}
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .