如何 "read" 使用 Powershell 的用户帐户

How to "read" a user account with Powershell

我正在尝试为用户文档创建一个备份 powershell 脚本。我创建了一个脚本,我将我要备份的用户名放在一个表单框中,然后脚本继续。最后一件事是我想要一个规则,如果我输入错误的名字,脚本将不会继续。

有谁知道我如何 "read" 笔记本电脑上的当前用户帐户,以便创建规则来交叉检查用户帐户的输入?

此致。

Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True'" | Select Name

将为您提供本地帐户的名称。 select 的其他可用字段列在 here

这将读取 C:\Users 中的所有文件夹并尝试在 AD 中找到相应的帐户:

Get-Childitem C:\users -Directory |
    ForEach-Object {
        $profilePath = $_.FullName

        Get-AdUser -Filter {SamAccountName -eq $_.Name} |
            ForEach-Object {
                New-Object -TypeName PsCustomObject |
                    Add-Member -MemberType NoteProperty -Name Name -Value $_.Name -PassThru |
                    Add-Member -MemberType NoteProperty -Name ProfilePath -Value $profilePath -PassThru |
                    Add-Member -MemberType NoteProperty -Name SID -Value $_.SID -PassThru
        }

    } | Format-Table Name,SID, profilePath -AutoSize

显然,如果需要,您可以修改以获得不同的 AD 属性。此外,删除 Format-Table 命令以便通过管道输出对象以供进一步操作。

注:

  • 这需要在您 运行 脚本所在的系统上安装 AD PowerShell Module
  • 它仅在找到用户帐户时才输出一个对象,因此您不会看到它找到的没有帐户的文件夹的任何内容。