Powershell:将PsCredentials的用户名设置为$Null,密码设置为$Null

Powershell:将PsCredentials的用户名设置为$Null,密码设置为$Null,powershell,Powershell,如何创建$Null用户名和$Null密码PScredentials对象 根据本文,null PSCredential导致Powershell使用Windows身份验证,这似乎是在域设置中运行脚本的一种更简单的方法。不幸的是,我似乎不知道他在哪里/如何将其设置为空: 其他资源: 此答案指定$Null作为密码,但不允许$Null用户名。 谢谢。为什么需要$null PSCredential对象 根据文件 -Credential <PSCredential> Specifies a

如何创建$Null用户名和$Null密码PScredentials对象

根据本文,null PSCredential导致Powershell使用Windows身份验证,这似乎是在域设置中运行脚本的一种更简单的方法。不幸的是,我似乎不知道他在哪里/如何将其设置为空:

其他资源: 此答案指定$Null作为密码,但不允许$Null用户名。


谢谢。

为什么需要$null PSCredential对象

根据文件

-Credential <PSCredential>

Specifies a user account that has permission to perform this action. The default is the current user. 


常量
[PSCredential]::Empty
(又称
[System.Management.Automation.PSCredential]::Empty
)为您提供类型为
PSCredential
的有效对象,但
用户名
密码
都设置为null

但是,这不是当前用户的凭据;相反,它的意思是“没有证书”。您调用的函数中可能有逻辑将其作为模拟点(即,当
$credentials-eq[PSCredential]::Empty时,该函数的逻辑表示使用当前安全上下文),但在某些上下文中,此相同的值可能用于其他目的(例如,表示您希望使用匿名身份验证)

如果不提示输入当前用户的凭据或以其他方式显式分配凭据,则无法获取当前用户的凭据;否则,这将带来安全风险

#create a credential object for demo purposes.  
#Imagine that instead of this line we were saying `$credential = Get-CurrentUserCredential` 
#(you have to imagine this, since no such function exists).
$credential = [PSCredential]::new('myUsername', ('superSecretPassword' | ConvertTo-SecureString -AsPlainText -Force))

#we can now see this user's password; 
#someone malicious could have this script run under the current user's 
#profile & have this information reported back to them.
$credential.GetNetworkCredential().Password
如果您想以当前用户的身份运行某些东西,那么您已经是(即,因此他们是当前用户);所以你不需要拿到他们的证件

也就是说,在有些情况下,拥有他们的证书会很好;e、 g.如果访问不使用当前用户上下文的资源/需要显式凭据。在这种情况下,您必须提示输入凭据(
获取凭据$env:username
),或者从某些资源中读取凭据(请参阅)


这里有一个非常全面的凭证说明:;绝对值得一读。

嗨,我尝试了各种方法让windows auth正常工作,也许这个问题是我们公司独有的。我们有多个具有域信任的域,因此我可以使用“母舰”域信任来导航其他系统。然而,它似乎并没有转化为powershell进入pssession。所以,我试图使用空pscreds来欺骗它。如果我不能,我将不得不为我连接到的每台服务器使用特定于域的Cred。这会使事情变得复杂。需要与Windows登录凭据对应的“null”凭据有很多原因。假设有人给我一个脚本来做某事,脚本的一个参数是执行该操作时要使用的凭据。我可能希望在一些非完整的管理脚本中使用此脚本,并使用管理脚本运行时使用的凭据。不确定是否真正理解,但我编辑了我的答案。
Get-WmiObject Win32_Process -Credential (Get-Credential)
#create a credential object for demo purposes.  
#Imagine that instead of this line we were saying `$credential = Get-CurrentUserCredential` 
#(you have to imagine this, since no such function exists).
$credential = [PSCredential]::new('myUsername', ('superSecretPassword' | ConvertTo-SecureString -AsPlainText -Force))

#we can now see this user's password; 
#someone malicious could have this script run under the current user's 
#profile & have this information reported back to them.
$credential.GetNetworkCredential().Password