Bill Hurt
Bill Hurt

Reputation: 749

IE History Permissions for other users

I have a script which I think is most of the way to being able to grab IE history for all of the users on a machine. My problem is it doesn't work for users other than myself because I get a permissions error when attempting open the folder. Does anyone have any ideas how I can fix the permissions problem?

I'm running this script on a Windows 2012r2 machine and I am an administrator on the box.

Thanks!

function Get-History 
{
    param
    (
        [string]$userName
    )

$shell = New-Object -ComObject Shell.Application 

if($username)
{
    $users = $username
}
else
{
    $users = Get-ChildItem C:\Users
}

if((([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")))
{
    Foreach($user in $users)
    {
        $user = Split-Path $user -leaf
        try
        {
            $ErrorActionPreference = 'Stop'
            $hist = $shell.NameSpace("C:\Users\$user\AppData\Local\Microsoft\Windows\History") 
        }
        catch
        {
            continue
        }
        $folder = $hist.Self 
        #$folder.Path

            if($hist){
            $hist.Items() | foreach { 
                #""; ""; $_.Name 
                 if ($_.IsFolder) { 
                     $siteFolder = $_.GetFolder 
                     $siteFolder.Items() | foreach { 
                        $site = $_ 
                        #""; $site.Name 
                        if ($site.IsFolder) { 
                            $pageFolder  = $site.GetFolder 
                            $pageFolder.Items() | foreach { 
                                $url = $pageFolder.GetDetailsOf($_,0) 
                                $date =  $pageFolder.GetDetailsOf($_,2) 
                                #"$user`: $date visited $url"  

                                #Write-Output ("$user,$date,`"$url`"" | ConvertFrom-Csv)

                                New-Object -TypeName PSObject -Property @{
                                                                          user=$user;
                                                                          date = $date;
                                                                          url = $url
                                                                          }
                            } 
                        } 
                     } 
                 } 
            }
        }    

    }
}
else
{
    Write-Host "Not Admin"
}
}

If I run just the small snippet:

$shell = New-Object -ComObject Shell.Application            
$hist = $shell.NameSpace("C:\Users\MyOwnUsername\AppData\Local\Microsoft\Windows\History")

Then I successfully assign the $hist variable as a System.__ComObject

But if I run:

$shell = New-Object -ComObject Shell.Application            
$hist = $shell.NameSpace("C:\Users\SomeOtherUser\AppData\Local\Microsoft\Windows\History")

I get:

Exception calling "NameSpace" with "1" argument(s): "Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))"
At line:3 char:1
+ $hist = $shell.NameSpace("C:\Users\SomeOtherUser\AppData\Local\Microsoft\Windows\History ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

Full Disclosure: it's a slight modification of the script at this blog post

Upvotes: 0

Views: 314

Answers (1)

Bill Hurt
Bill Hurt

Reputation: 749

I never solved the permissions problem. I changed strategy to use BrowsingHistoryViewer by Nirsoft.

$execPath = "C:\BrowsingHistoryView.exe"

$computers = 'computer1','computer2','computer3','computer4'

$outPath = "c:\"

$computers | 
    ForEach-Object{ `
        Start-Process $execPath `
            -argumentList "/HistorySource 3", 
                          "/HistorySourceFolder ""\\$_\c$\Users""",
                          "/scomma ""$outpath\history_$_.txt"""
    }

Upvotes: 0

Related Questions