Powershell - 从用户个人资料中获取内容

时间:2015-12-09 22:35:20

标签: powershell foreach

Powershell和脚本新手,所以在这里。我试图通过一个pc列表来提取任何Java异常站点(最终我会查询所有工作站的AD)。 exception.sites文件位于每个用户配置文件\ AppData \ LocalLow \ Sun \ Java \ Deployment \ security文件夹中。我不知道为什么我只是从我的工作站拉出网站。

$comps = Get-Content \\server1\users\james\test\comps.txt
$addPath = "\AppData\LocalLow\Sun\Java\Deployment\security"
$userprofiles = Get-WmiObject win32_userprofile -filter "LocalPath Like '%\\Users\\%'" | Select-Object -ExpandProperty Localpath | foreach {$_ + $addpath}
foreach ($pc in $comps)
    {foreach ($profile in $userprofiles)
        {if ((test-path "$profile\exception.sites") -ne $false)
        {get-content -path "$profile\exception.sites" | Out-File \\server2\packages\java\siteexceptions\SiteExceptions.txt -Append
        }
    }
}

1 个答案:

答案 0 :(得分:0)

you need to make a couple of adjustments to the code.

Get-WmiObject has a -computername parameter which will make it run on a remote computer. Also you will need to move the WMi query inside the computer loop so that the query is run once on each computer that you are iterating.

Note: Since the destination points to a location within another user's profile, you will need administrator privileges to be able to traverse the protected path.

foreach ($pc in $comps)
{

$userprofiles = Get-WmiObject win32_userprofile -filter "LocalPath Like '%\\Users\\%'" -ComputerName $pc

foreach ($profile in $userprofiles)
{
    $drive = (Split-Path -Path $profile.localpath -Qualifier) -replace ':','$'
    $Remotepath =  "\\$pc\$drive" + (Split-Path -Path $profile.localpath -NoQualifier) + $addPath + '\exception.sites'
    if (test-path $Remotepath)
    {
        get-content -path $Remotepath | Out-File \\server2\packages\java\siteexceptions\SiteExceptions.txt -Append

    }
}
}