显示来自Get-Credentials的值

问题描述 投票:0回答:2

我以为这很简单,但似乎无法正确显示一个值。在Powershell脚本中,我使用Get-Credentials提示来允许某人输入其提升的帐户详细信息,以在AD和Exchange中执行用户创建。我为脚本的每次运行写一个注销,并带有日期和名称信息。我尝试捕获的字段之一是在凭据阶段输入的人员的用户名。

这里是我正在使用的

$CurrentUser = Get-Credential
Add-Content $outputfile "Running as $CurrentUser.UserName"

并且我正在日志中找到此行

作为System.Management.Automation.PSCredential.UserName运行

有人可以让我知道我应该在哪里放置显示在提示符下输入的用户名。

非常感谢

S。

powershell credentials
2个回答
0
投票

您需要将其放在sub-expression中,以使其从.UserName属性中输出字符串。

# Subexpression:
Add-Content $outputfile "Running as $($CurrentUser.UserName)"

0
投票
$CurrentUser = Get-Credential
Add-Content $outputfile ("Running as " + $CurrentUser.UserName)

您不应将命令放在字符串中

© www.soinside.com 2019 - 2024. All rights reserved.