우리는 모든 사용자 appdata 폴더 내에서 식별된 잘못된 구성 파일의 존재를 제거하고 수동으로 수정하려고 합니다. 구성 파일은 모든 사용자가 아닌 여기 저기에만 존재합니다. 그래서 지정된 텍스트가 config.xml의 get-content와 일치하면 true/false를 반환하는 스크립트를 하나로 묶으려고 합니다. 모든 컴퓨터의 각 사용자 프로필에 대해 이것을 실행하고 결과를 csv로 출력하고 싶습니다.
지금까지 문제가 발생했습니다. 아직 Powershell을 배우고 있으므로 도움을 주시면 감사하겠습니다.
$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"
}
}
보시다시피, get-content를 생성할 올바른 경로를 얻을 수 없기 때문에 아직 csv 부분을 다루지 못했습니다.
foreach 루프가 작동할 때 내가 바랐던 올바른 경로를 갖는 대신 다음과 같은 방법이 될 것입니다.
"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"
대신 다음과 같은 오류가 발생합니다.
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
여기서 내 겉으로는 어리석은 가정은 $Users가 "C:\Users"
(내가 정의했기 때문에 그렇다고 생각함) $Username이 될 것이라는 것입니다 "Username1"
(분명히 그렇지 않습니다). 대신 "C:\Users"
에 이 경우에도 생성되는 경로가 다음과 같이 잘못된 경로임을 알 수 있습니다 .
C:\Users\C:\Users\AppData\Local\MyApp\Settings\Config.xml
foreach 루프는 내부의 각 폴더를 $Users
개별 개체로 처리하고 각 루프 인스턴스가 되어 다음 과 같이 $Username
구성 파일에 삽입할 수 있을 것으로 예상했습니다.$path
$Users $Username
↓ ↓
C:\Users\Username1\AppData\Local\MyApp\Settings\Config.xml
내가 뭘 잘못하고 있는지 완전히 확신할 수는 없지만 여기서 어리석은 일을 하고 있다는 것은 확실합니다. 더 나은 PS 기술을 가진 사람이 이를 지적하는 데 도움을 주시면 정말 감사하겠습니다.
답변1
방정식에서 Get-Childitem이 누락되었다는 사실을 알아냈습니다.
이것이 올바른 방법입니다. 내 PS에 큰 개선이 필요하다고 확신하지만 내보낼 적절한 형식의 CSV를 얻을 수 없어서 개별 CSV를 출력하는 지저분한 접근 방식을 선택했습니다. 각 인스턴스는 true이지만 적어도 작동합니다!
$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"
}
}
}