Lor*_*azz 6 powershell directoryservices
我有一个 PowerShell 脚本,它在启动过程中检查当前登录的用户。我使用 .Net 通过添加程序集来完成此操作:
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$cUser = [System.DirectoryServices.AccountManagement.UserPrincipal]::Current
Run Code Online (Sandbox Code Playgroud)
对于几乎每个人来说,这都工作正常,我得到了一个可以在其他地方使用的 UserPrincipal 对象,但是有一些用户在运行它时遇到以下错误:
Unable to cast object of type 'System.DirectoryServices.AccountManagement.GroupPrincipal' to type 'System.DirectoryServices.AccountManagement.UserPrincipal'. At line:2 char:1
+ [System.DirectoryServices.AccountManagement.UserPrincipal]::Current
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], InvalidCastException
+ FullyQualifiedErrorId : System.InvalidCastException
Run Code Online (Sandbox Code Playgroud)
这是在标准 Windows 10 20H2 安装上进行的,无论 PowerShell 是否运行提升都无关紧要。我以前从未见过此调用返回除 UserPrincipal 之外的任何内容,因此我将不胜感激任何建议:这是我可以在代码中处理的东西,还是返回此异常的机器存在一些潜在问题?
小智 3
我不知道这是不是你要找的:
$cUser = Get-ADUser $Env:Username -Properties *
Run Code Online (Sandbox Code Playgroud)
它具有最重要的属性,您以后可以将其用于任何您想要的用途。它提供了与您发布的代码一样多的属性,但该代码对某些用户不起作用。
| 归档时间: |
|
| 查看次数: |
460 次 |
| 最近记录: |