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"
                    }
                }
            }
        }
    }
}
4

2 回答 2

2

您将字符串视为文件夹对象。

改变:

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

到:

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

这将获取文件夹对象并允许您根据需要对其进行操作。

于 2012-06-05T18:53:03.603 回答
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"
                    }
                }
            }
        }
    }
}
于 2012-06-05T19:02:34.200 回答