Invoke-WebRequest 是否可以透明地使用用户 运行 脚本的用户凭据进行身份验证?

Is it possible for Invoke-WebRequest to transparently authenticate with the user credentials of the user running the script?

我有一个基本上可以在许多系统上创建用户帐户的脚本,包括 Active Directory 和一个针对同一个 Active Directory 域进行身份验证的 IIS 网络服务器。

现在,如果我只是执行 Invoke-WebRequest 而没有指定凭据,我会收到 401 错误,如果我使用凭据开关,我必须指定或提示输入凭据。但是,当它创建 Active Directory 帐户时,PowerShell 足够聪明,可以使用用户的凭据 运行 脚本来创建帐户,而无需指定或提示输入凭据。

是否可以用 Invoke-WebRequest 做同样的事情?

根据 TechNet for Invoke-WebRequest-UseDefaultCredentials 开关应该完全符合您的要求:

Indicates that the cmdet uses the credentials of the current user to send the web request.