有没有一种方法可以将列表导出到文本文件,并以某种方式(而不是单个文件)按共享名将其分开?
我想用这种格式,"$hostname-$sharename.txt".
这是我到目前为止所拥有的:
$Shares = Get-WmiObject Win32_Share -Filter "not name like '%$'" |
Select-Object -Expand Path
$re = ($Shares | ForEach-Object {[Regex]::Escape($_)}) -join '|'
$results = foreach ($Share in $Shares) {
(Get-ChildItem $Share -Recurse | Select-Object -Expand FullName) -replace "^($re)\\"
}
$results | Out-File -FilePath "C:\Output\$($env:computername)-$sharename.txt"
答案 0 :(得分:0)
如果我对问题的理解正确,那么代码中所缺少的就是各种输出文件的共享名部分。
以下希望可以做您想做的事情:
$outputDir = 'C:\Output'
$Shares = Get-WmiObject Win32_Share -Filter "not name like '%$'"
$re = ($Shares | ForEach-Object {[Regex]::Escape($_.Path)}) -join '|'
foreach ($Share in $Shares) {
$result = (Get-ChildItem -Path $Share.Path -File -Recurse | Select-Object -Expand FullName) -replace "^($re)\\"
# output the results per share in a text file
$fileOut = Join-Path -Path $outputDir -ChildPath ('{0}-{1}.txt' -f $env:COMPUTERNAME, $Share.Name)
$result | Out-File -FilePath $fileOut -Force
}