这里是PowerShell新手
我需要:
我在这里和其他地方搜索和学习了如何将#1输出到屏幕上,但不知道如何将其放在带有#2的哈希表中:
$documentsfolder = "C:\records\"
foreach ($file in Get-ChildItem $documentsfolder -recurse | Select String -pattern "students.txt" ) {$file}
我正在考虑在#2中获得名称,我需要使用RegEx,因为有时可能只有一个名称。对于输出到Excel,如下所示:| Export-Csv -NoType output.csv
任何能让我继续前进的帮助都是非常感谢的。
发布于 2016-04-28 05:43:42
我觉得这应该能让你开始。解释在代码注释中。
# base directory
$documentsfolder = 'C:\records\'
# get files with names ending with students.txt
$files = Get-ChildItem $documentsfolder -recurse | Where-Object {$_.Name -like "*students.txt"}
# process each of the files
foreach ($file in $files)
{
$fileContents = Get-Content $file
$fileName = $file.Name
#series of matches to clean up different parts of the content
#first find the gc.... pattern
$fileContents = ($fileContents | Select-String -Pattern 'gc.student=".*"').Matches.Value
# then select the string with double quotes
$fileContents = ($fileContents | Select-String '".*"').Matches.Value
# then remove the leading and trailing double quotes
$fileContents = $fileContents -replace '^"','' -replace '"$',''
# drop the objects to the pipeline so that you can pipe it to export-csv
# I am creating custom objects so that your CSV headers will nave nice column names
Write-Output [pscustomobject]@{file=$fileName;name=$fileContents}
} | Export-Csv -NoType output.csv
https://stackoverflow.com/questions/36903031
复制相似问题