【问题标题】:Get local profile list with powershell使用 powershell 获取本地配置文件列表
【发布时间】:2018-11-05 15:09:16
【问题描述】:

我在 Windows server 2008 R2 上,我需要提取本地配置文件列表,因此我使用 Powershell 查看注册表并获得我想要的:

$path = 'Registry::HKey_Local_Machine\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\*'
$items = Get-ItemProperty -path $path 
Foreach ($item in $items) {
$objUser = New-Object 
System.Security.Principal.SecurityIdentifier($item.PSChildName)
$objName = $objUser.Translate([System.Security.Principal.NTAccount])
$item.PSChildName = $objName.value
}
echo $items | Select-Object -Property PSChildName | Export-Csv 
C:\scripts\PSScripts\UserProfile.csv -Encoding UTF8

它在另一台使用 Windows Server 2012 R2 的机器上工作,但在这里我遇到了很多错误,但总是一样的:

使用“1”个参数调用“翻译”的异常:“部分或全部 身份参考无法翻译。”在 C:\scripts\PSScripts\users_profile.ps1:5 char:34 + $objName = $objUser.Translate

.csv 文件已创建,但存在问题,例如显示多次的个人资料,如下所示:

域\用户 1 域\用户 2 域\用户 3 域\用户 3 域\用户 4 域\用户 5 域\用户 5 域\用户 5 域\用户 6

WS2008 和 WS2012 之间是否存在可能导致此问题的差异?还是别的什么?

【问题讨论】:

  • 我无权访问那些操作系统,所以这是一种不同的方法……您尝试过 WMI 类了吗?这个Get-CimInstance -ClassName 'Win32_UserProfile' -ComputerName 'LocalHost' 似乎在win7ps5.1 上运行得很好。此外,WMI 对象调用 Get-WmiObject -Class 'Win32_UserProfile' -ComputerName 'LocalHost' 有一个 .Delete() 方法,可以清理几乎所有内容。
  • 也许我需要导入一个模块,或者它不适用于 WS2008,但是当我尝试 Get-CimInstance 时出现此错误:“Get-CimInstance”一词未被识别为cmdlet、函数、脚本文件或可运行程序的名称。 而另一个命令没有提供我需要的 PSChildName
  • 如果 CIM cmdlet 不工作,那么您运行的是旧版本的 PoSh。请改用Get-WMIObject

标签: powershell csv registry


【解决方案1】:

我建议使用 WMI 来确保跨平台的一致性以及一些错误处理:

$path = 'C:\scripts\PSScripts\UserProfile.csv'

Get-CimInstance -ClassName Win32_UserProfile -Filter Special=FALSE -PipelineVariable user |
    ForEach-Object -Begin {$ErrorActionPreference = 'Stop'} {
        try
        {
            $id = [System.Security.Principal.SecurityIdentifier]::new($user.SID)
            $id.Translate([System.Security.Principal.NTAccount]).Value
        }
        catch
        {
            Write-Warning -Message "Failed to translate $($user.SID)! $PSItem"
        }
    } |
    Select-Object -Property @{Label='PSChildName'; Expression={$PSItem}} |
    Export-Csv -Path $path -Encoding ascii -NoTypeInformation

PSv2解决方案:

Get-WmiObject -Class Win32_UserProfile -Filter Special=FALSE |
    ForEach-Object -Begin {$ErrorActionPreference = 'Stop'} {
        try
        {
            $sid = $_.SID
            $id = New-Object -TypeName System.Security.Principal.SecurityIdentifier -ArgumentList $sid
            $id.Translate([System.Security.Principal.NTAccount]).Value
        }
        catch
        {
            Write-Host "Failed to translate $sid! $_" -ForegroundColor Red
        }
    } |
    Select-Object -Property @{Label='PSChildName'; Expression={$_}} |
    Export-Csv -Path $path -Encoding ascii -NoTypeInformation

【讨论】:

  • PSv2 解决方案实际上帮助了我,我的 csv 文件中不再有任何重复项,但是当我执行它时它仍然给我一些错误:无法翻译 S-1-5- 21-2104366046-3998681915-3170305578-1235!使用“1”个参数调用“翻译”的异常:“无法翻译部分或全部身份引用。”
猜你喜欢
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 2016-03-09
  • 1970-01-01
  • 1970-01-01
  • 2020-03-07
  • 2010-12-25
  • 1970-01-01
相关资源
最近更新 更多