小编Raz*_*anu的帖子

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

我有一个 Azure 文件共享,并希望在我的 Azure VM 中使用它 - 在使用 cmdkey 在 VM 上保留凭据并使用网络使用挂载之后。这是通过在 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)
Run Code Online (Sandbox Code Playgroud)

和错误:

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

替换成cmdkey …

remote-access powershell windows-server-2012-r2

14
推荐指数
1
解决办法
5576
查看次数

通过 Invoke-Command 远程执行 Powershell Add-Computer

设想:

在 Azure VM 本地执行时,成功将机器添加到 AD,然后重新启动。

$DomainName = "test.local"
$AdminUserName = "sysadmin"
$Password = <mypass>
$SecurePassword = ConvertTo-SecureString $Password -asplaintext -force
$Credential = New-Object -Typename System.Management.Automation.PSCredential -Argumentlist $AdminUserName, $SecurePassword
$Credential

Add-Computer -DomainName $DomainName -Credential $Credential -Restart -Passthru -Verbose
Run Code Online (Sandbox Code Playgroud)

题:

使用相同的变量,但现在在我的机器上运行脚本,以另一个 Azure VM 作为目标,通过远程 Powershell:

$ScriptBlockContent = { 
Param ($Arg1,$Arg2)
Add-Computer -DomainName $Arg1 -Credential $Arg2 -Restart -Passthru -Verbose}

$Session = New-PSSession -ConnectionUri $Uri -Credential $Credential
Invoke-Command -Session $Session -ScriptBlock $ScriptBlockContent -ArgumentList ($DomainName,$Credential)
Run Code Online (Sandbox Code Playgroud)

这在远程执行时失败。为什么 ?

PS C:\> Invoke-Command -Session $Session -ScriptBlock $ScriptBlockContent …
Run Code Online (Sandbox Code Playgroud)

powershell active-directory remote

4
推荐指数
1
解决办法
5427
查看次数