Foreach 循环中通过驱动器的参考电流驱动路径

Reference current drive path in a Foreach loop through drives

我试图在多台机器上查找并记录所有本地 PST 文件,跨每台机器上的所有本地驱动器。到目前为止,我有下面的代码,但我无法在 foreach 循环中当前驱动器的根上下文中将其获取到 运行,它只是 运行s 在其中的上下文中脚本来自 运行。

If (-not (Test-Path -Path "\BETH-FS01\F$\PSTBackups")){
    Exit 1
} # check if PC is connected to domain, some laptops aren't always on the VPN
Else{
    #defines the path where to store the log CSV
    $LogPath = "\BETH-FS01\F$\PSTBackups" 
    $Log = @() #object array to store "PST objects"

    # defining string variable to combine with another string variable later on
    $Ext = ".pst" 
    
    # creates array of local drives on PC
    $Drives = Get-PSDrive -PSProvider 'FileSystem' 

    foreach ($Drive in $Drives) {

        # searches drive for PST files, creates an array of those files,
        # then passes each through to create PST objects
        $Log = ForEach ($PST in ($PSTS = Get-ChildItem -LiteralPath $Drive.Name -Include *.pst -Recurse -Force -erroraction silentlycontinue)){ 
            New-Object PSObject -Property @{
                ComputerName = $env:COMPUTERNAME
                Path = $PST.DirectoryName
                FileName = $PST.BaseName+$Ext
                Size = "{0:N2} MB" -f ($PST.Length / 1mb)
                Date = $PST.LastWriteTime.ToString("yyyy-MM-dd HH:mm")
            } #creates PST object
        }
    }
}
$Name = $env:COMPUTERNAME #define string to use in log path below
$Log | Export-Csv $LogPath$Name.csv -NoTypeInformation #exports the Log object array to a CSV

为了澄清,我试图找出如何引用这样一个事实,即如果 foreach 循环当前正在执行 C: 驱动器,它将使用“C:”路径作为 Get-ChildItem 的 -path , 即:

$PSTS = Get-ChildItem -path "*somehow reference C: drive path*" -Include *.pst -Recurse -Force -erroraction silentlycontinue

抱歉,如果代码草率,我不是最擅长保持干净代码的人...

这一行...

$Log = ForEach ($PST in ($PSTS = Get-ChildItem -LiteralPath $Drive.Name -Include *.pst -Recurse -Force -erroraction silentlycontinue)){ 

...您只是传递 -LiteralPath 的驱动器号,这不是路径。

您需要传递根路径,e。 G。 "C:\",不仅仅是 "C""C:"。后者只表示C盘的当前目录。

这应该可以解决问题:

$Log = ForEach ($PST in ($PSTS = Get-ChildItem -LiteralPath $Drive.Root -Include *.pst -Recurse -Force -erroraction silentlycontinue)){