PowerShell新手在这里,
我需要:
我已经想通了,在这里和其他地方搜索和学习过,如何将#1输出到屏幕,但不知道怎么把它放到#2的哈希表中:
$documentsfolder = "C:\records\"
foreach ($file in Get-ChildItem $documentsfolder -recurse | Select String -pattern "students.txt" ) {$file}
我想在#2中获取名称我需要使用RegEx,因为有时可能只有1个名称。
对于输出到Excel,这个:| Export-Csv -NoType output.csv
感谢我的任何帮助。
答案 0 :(得分:0)
我认为这应该让你开始。解释在代码注释中。
# 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