如何在远程Powershell会话中保留凭据?

我有一个Azure文件共享,并希望在我的Azure VM中使用它 – 在使用cmdkey在VM上保留凭据并使用net use挂载之后.通过在 Windows Server 2012 R2上的本地Powershell会话中运行这些命令来测试这一点.

但我需要将此步骤添加到Azure部署脚本中. Azure Powershell脚本从我的笔记本电脑运行,连接到Azure订阅并使用大量变量从头开始构建VM.

想出使用Invoke-Command将变量从Azure Powershell脚本传递到新创建的VM上的远程Powershell会话.

$Session = New-PSSession -ConnectionUri $Uri -Credential $DomainCredential

$ScriptBlockContent = { 
Param ($Arg1,$Arg2,$Arg3)
cmdkey /add:$Arg1 /user:$Arg2 /pass:$Arg3}

Invoke-Command -Session $Session -ScriptBlock $ScriptBlockContent -ArgumentList ($Share,$AccountName,$Key)

错误

PS C:\> Invoke-Command -Session $Session -ScriptBlock $ScriptBlockContent -ArgumentList ($Share,$Key)
CMDKEY: Credentials cannot be saved from this logon session.

替换为cmdkey / list以检查语法,并且没有错误.

PS C:\> Invoke-Command -Session $Session -ScriptBlock $ScriptBlockContent
Currently stored credentials:
* NONE *

一个类似的问题(并且无法修复它)与Windows Update PowerShell模块(Invoke-WUInstall),它在VM上的本地Powershell会话上运行良好,但在通过远程Powershell启动时不会更新.

有办法解决这个问题吗?

由于Windows处理身份验证的方式无法使用CMDKEY通过远程PowerShell会话设置凭据,因此在使用CMDKEY时必须以交互方式完成.

从线程中引用Don Jones来寻找与您类似的答案:

That’s a limitation of the Cmdkey command – not really a PowerShell thing. But it’s related to the way Remotig handles credentials. The remote session doesn’t actually get a credential,it gets a delegated ticket,so there’s no token to actually save. That’s all by design,and not something you can reconfigure.

相关文章

Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...
Windows文件操作基础代码 Windows下对文件进行操作使用的一段...
Winpcap基础代码 使用Winpcap进行网络数据的截获和发送都需要...
使用vbs脚本进行批量编码转换 最近需要使用SourceInsight查看...