Powershell 将输出路径作为变量导出CSV

Powershell 将输出路径作为变量导出CSV,powershell,Powershell,有没有一种方法可以将列表导出到一个文本文件中,并通过共享名而不是单个文件来分隔它们 我想使用这种格式,“$hostname-$sharename.txt”。 以下是我到目前为止的情况: $Shares = Get-WmiObject Win32_Share -Filter "not name like '%$'" | Select-Object -Expand Path $re = ($Shares | ForEach-Object {[Regex]::Escape($_

有没有一种方法可以将列表导出到一个文本文件中,并通过共享名而不是单个文件来分隔它们

我想使用这种格式,
“$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"

如果我理解正确的话,代码中缺少的只是各种输出文件的共享名部分

以下内容有望满足您的需求:

$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
}