Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 方法调用失败,方法名为items_Powershell - Fatal编程技术网

Powershell 方法调用失败,方法名为items

Powershell 方法调用失败,方法名为items,powershell,Powershell,我对下面的代码有一些问题。我试图找出如何使输出适用于所有用户配置文件,而不仅仅是当前用户。如果我使用$shell.NameSpace(34),代码就会正常工作(两行注释掉)。但是,当我尝试重写shell.namespace并手动添加路径时,会出现一个错误,即方法项不存在。想知道解决或绕过此问题的最佳方法是什么 实际错误消息:方法调用失败,因为[System.String]不包含名为“Items”的方法 感谢您在advanced中的帮助 $shell = New-Object -ComObject

我对下面的代码有一些问题。我试图找出如何使输出适用于所有用户配置文件,而不仅仅是当前用户。如果我使用$shell.NameSpace(34),代码就会正常工作(两行注释掉)。但是,当我尝试重写shell.namespace并手动添加路径时,会出现一个错误,即方法项不存在。想知道解决或绕过此问题的最佳方法是什么

实际错误消息:方法调用失败,因为[System.String]不包含名为“Items”的方法

感谢您在advanced中的帮助

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

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

更改:

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


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

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

更改:

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


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

根据文档,您可以从路径字符串创建名称空间对象

所以你可以这样做:

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

根据文档,您可以从路径字符串创建名称空间对象

所以你可以这样做:

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

看起来OP正在尝试获取FileInfo类中不可用的COM对象的URL属性。看起来OP正在尝试获取FileInfo类中不可用的COM对象的URL属性。