【问题标题】:Powershell output is empty [duplicate]Powershell输出为空[重复]
【发布时间】:2020-04-08 10:21:35
【问题描述】:

我正在做一些事情来从域控制器中检索帐户信息。

$Input = $InputBox1.Text

$InputBox10.Text = (Get-ADUser -Filter "UserPrincipalName -like '$Input'" -Server "server.com" -Properties UserPrincipalName | Select-Object @{Name='UPN'; Expression={if($Input -ne $_.UserPrincipalName){'No corresponding UPN'}else{$_.UserPrincipalName}}}).'UPN'

因此,如果该帐户在 server.com 中,我会收到该帐户的 UPN 作为输出,但如果该帐户不在 server.com 中,我希望接收“没有相应的 UPN”,而是接收该输出,它是空的。

有什么想法吗?

提前致谢。

亲切的问候, 拉尔夫

【问题讨论】:

标签: powershell


【解决方案1】:

正如评论,$Input 是一个错误的变量名,因为它是一个Automatic variable。您应该为此使用其他东西。
此外,试图将几乎所有的代码都塞进一行,你一定会遇到麻烦。只需添加一个额外的行,让事情变得更清楚:

$UpnInput = $InputBox1.Text

$user = Get-ADUser -Filter "UserPrincipalName -eq '$UpnInput'" -Server "server.com"
$InputBox10.Text = if ($user) { $user.UserPrincipalName } else { 'No corresponding UPN' }

希望对你有帮助

Get-ADUser 默认返回以下属性:DistinguishedName、Enabled、GivenName、Name、ObjectClass、ObjectGUID、SamAccountName、SID、Surname、UserPrincipalName。如果没有找到用户,则返回$null。

【讨论】:

  • 谢谢西奥!这确实是解决方案。不知道我不能使用 $Input。很高兴知道,将来会很有帮助。
猜你喜欢
  • 2015-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-23
  • 1970-01-01
  • 2020-07-02
  • 2015-11-17
  • 1970-01-01
相关资源
最近更新 更多