2012-06-05 36 views
1

我在下面的代碼中遇到了一些問題。我想弄清楚如何讓輸出適用於所有用戶配置文件,而不僅僅是當前用戶。如果我使用$ shell.NameSpace(34)代碼工作(這兩行註釋掉)。但是,當我嘗試重寫shell.namespace並手動添加路徑時,我得到一個錯誤,方法項不存在。想知道解決或解決此問題的最佳方法是。方法調用失敗方法命名項目

實際的錯誤消息:方法調用失敗,因爲[System.String]不包含名爲'Items'的方法。

感謝您的幫助。

$shell = New-Object -ComObject Shell.Application 
$colProfiles = Get-ChildItem "C:\Users\" -Name 
#$hist = $shell.NameSpace(34) 
#Write-Host $hist 

foreach ($userProfile in $colProfiles) 
{ 
    [string]$hist = "C:\Users\$userProfile\AppData\Local\Microsoft\Windows\History" 
    $date = "" 
    $url = "" 

    $hist.Items() | foreach { 
     ""; ""; 
     if ($_.IsFolder) 
     { 
      $siteFolder = $_.GetFolder 
      $siteFolder.Items() | foreach { 
       $site = $_ 
       ""; 
       if ($site.IsFolder) 
       { 
        $pageFolder = $site.GetFolder 
        Write-Host $pageFolder 
        $pageFolder.Items() | foreach { 
         $url = $pageFolder.GetDetailsOf($_,0) 
         $date = $pageFolder.GetDetailsOf($_,2) 
         echo "$date $url" 
        } 
       } 
      } 
     } 
    } 
} 

回答

2

根據文檔,您可以從路徑字符串中創建一個名稱空間對象。

http://msdn.microsoft.com/en-us/library/windows/desktop/bb774085(v=vs.85).aspx

所以,你可以這樣做:

$shell = New-Object -ComObject Shell.Application 
$colProfiles = Get-ChildItem "C:\Users\" -Name 

foreach ($userProfile in $colProfiles) 
{ 
    [string] $histPath = "C:\Users\$userProfile\AppData\Local\Microsoft\Windows\History" 
    $hist = $shell.NameSpace($histPath) 

    $date = "" 
    $url = "" 

    $hist.Items() | foreach { 
     ""; ""; 
     if ($_.IsFolder) 
     { 
      $siteFolder = $_.GetFolder 
      $siteFolder.Items() | foreach { 
       $site = $_ 
       ""; 
       if ($site.IsFolder) 
       { 
        $pageFolder = $site.GetFolder 
        Write-Host $pageFolder 
        $pageFolder.Items() | foreach { 
         $url = $pageFolder.GetDetailsOf($_,0) 
         $date = $pageFolder.GetDetailsOf($_,2) 
         echo "$date $url" 
        } 
       } 
      } 
     } 
    } 
} 
+0

非常感謝!那就是這個伎倆 – user1048209

2

你對待一個字符串作爲文件夾對象。

變化:

[string]$hist = "C:\Users\$userProfile\AppData\Local\Microsoft\Windows\History" 

要:

$hist = Get-Item "C:\Users\$userProfile\AppData\Local\Microsoft\Windows\History" 

這將讓該文件夾對象,並允許您根據需要操縱它。

+0

貌似OP的努力得到了COM對象,它是不具備的FileInfo類的URL屬性。 –