[英]In-memory caching of password across Powershell cmdlet invocations
I have some custom cmdlets that each require a user name and password to execute. 我有一些自定义cmdlet,每个cmdlet都需要用户名和密码才能执行。 At the moment I have to enter my credentials each and every time I run any of these cmdlets.
目前,每次运行任何这些cmdlet时,我都必须输入我的凭据。 The usage pattern is such that I typically run different combinations of these commands within a short window of time, maybe once every couple days.
这种用法模式通常使我在很短的时间内(可能每两天一次)运行这些命令的不同组合。 This means I have to enter my password several times within this short period.
这意味着我必须在此短时间内多次输入密码。
Is there any way I can tell Powershell to cache the credentials in-memory for some period of time? 有什么办法可以告诉Powershell在一段时间内将凭据缓存在内存中? I realize this is a security trade-off, but it's one I'm willing to make, especially considering the password is stored as a
SecureString
. 我意识到这是一种安全性的折衷,但是我愿意这样做,尤其是考虑到密码是以
SecureString
形式存储的。 If there was some kind of persistent "session" object in which I could store data across cmdlet invocations, that would also work, but I've been unable to identify such a feature. 如果存在某种持久的“会话”对象,我可以在其中跨cmdlet调用存储数据,那也可以,但是我一直无法识别这种功能。
UPDATE UPDATE
I forgot to mention I'm targetting .NET core and have been unable to use the PSCredential
type. 我忘了提我以.NET核心为目标,但一直无法使用
PSCredential
类型。 I have this in my base cmdlet class: 我的基础cmdlet类中有以下内容:
[Parameter(
Mandatory = true,
HelpMessage = "Credentials for access.",
ValueFromPipelineByPropertyName = true)]
public PSCredential Credentials
{
get;
set;
}
But attempting to dereference the Password
property gives a compiler error: 但是尝试取消引用
Password
属性会导致编译器错误:
Reference to type 'SecureString' claims it is defined in 'mscorlib', but it could not be found Contented.PowerShell..NETStandard,Version=v1.6
Relevant dependencies of my project are: 我项目的相关依赖项是:
"dependencies": {
"Microsoft.PowerShell.5.ReferenceAssemblies": "1.0.0-*",
"System.Security.SecureString": "4.3.0"
},
$cred = Get-Credentials
And then pass $cred
to the commands you are invoking. 然后将
$cred
传递给您正在调用的命令。 Or (if we are talking about PS Sessions). 或者(如果我们正在谈论PS会话)。 This way you can reuse session to invoke many commands.
这样,您可以重用会话来调用许多命令。
$session = New-PSSession
Invoke-Command -Session $session -ScriptBlock {}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.