Powershell Shell.Application 对象找不到文件夹

Powershell Shell.Application object not finding folder

我正在尝试从文件中获取元数据。我正在使用找到的代码示例 here.

下面是我用来尝试访问任何文件夹的一些示例代码,但我似乎无法这样做:

$TheThing = "C:\Windows"

$folder = {$TheThing}

foreach($sFolder in $folder) 
  { 
   $a = 0 
   $objShell = New-Object -ComObject Shell.Application 
   $objFolder = $objShell.NameSpace($folder)

   foreach ($File in $objFolder.items()) 
    {  
     $FileMetaData = New-Object PSOBJECT 
      for ($a ; $a  -le 266; $a++) 
       {  
         if($objFolder.getDetailsOf($File, $a)) 
           { 
             $hash += @{$($objFolder.getDetailsOf($objFolder.items, $a))  = 
                   $($objFolder.getDetailsOf($File, $a)) } 
            $FileMetaData | Add-Member $hash 
            $hash.clear()  
           } #end if 
       } #end for  
     $a=0 
     $FileMetaData 
    } #end foreach $file 
  } #end foreach $sfolder 

行:

$objFolder = $objShell.NameSpace($folder)

...实际上什么也没做。事实上,当代码落入 foreach 循环时,它失败并在行上显示 "You cannot call a method on a null-valued expression" 错误:

foreach ($File in $objFolder.items()) 

我是不是漏掉了什么?

这一行导致了您的问题:

$folder = {$TheThing}

需要是:

$folder = $TheThing

通过使用 { } 你可以将它变成 scriptblock,因为你只需要它是一个包含你要查询的路径的字符串。

您也可以将路径放在那里,而不是为了一个目的而使用两个变量。