【问题标题】:Use current Powershell credentials for remote call使用当前的 Powershell 凭据进行远程调用
【发布时间】:2014-12-03 20:06:36
【问题描述】:

我有一个 Powershell 脚本,用于远程调用其他服务器上的其他 Powershell 脚本。该脚本用于关闭和启动不同服务器上的服务。 Powershell 脚本的设置方式是,我所要做的就是通过调用 serverStartStop [START|STOP] 来调用它,它会自动有条不紊地转到服务器列表并关闭每台服务器上的服务列表。

我最近升级了系统,需要在启动一些服务后运行批处理脚本。我可以远程调用批处理脚本,但是批处理脚本调用了另一个尝试访问网络共享的命令。该命令失败,因为用于调用该命令的任何用户都没有足够的权限来访问共享。

我尝试了几种方法来解决这种情况,并对 Powershell 中的 Invoke-Command commandlet 和 Windows Batch 中的 runas 命令进行了一些研究。 runas 命令要求用户输入密码,这是不可接受的,因为这是一个自动脚本。除了拨打初始 START 或 STOP 电话之外,是否有人对我如何能够干净利落地完成这项工作且无需用户交互有任何想法?

【问题讨论】:

  • Invoke-Command 使用 Credssp 是最常规的方法。请参阅我对this question 的回复。

标签: windows powershell batch-file


【解决方案1】:

Invoke-Command 上的 -Credential 方法可能是您想要的。我发现这对于存储用于以加密方式编写脚本的凭据集非常有用。

Add-Type -assembly System.Security

# String to Crypt
$passwordASCII = Read-Host -Prompt "Enter the Password"

# String to INT Array
$enc = [system.text.encoding]::Unicode
$clearPWD_ByteArray = $enc.GetBytes( $passwordASCII.tochararray())

# Crypting
$secLevel = [System.Security.Cryptography.DataProtectionScope]::LocalMachine
$bakCryptedPWD_ByteArray = [System.Security.Cryptography.ProtectedData]::Protect($clearPWD_ByteArray, $null, $secLevel)

# Store in Base 64 form
$B64PWD_ByteArray = [Convert]::ToBase64String($bakCryptedPWD_ByteArray)
Set-Content -LiteralPath c:\Temp\pass.txt -Value $B64PWD_ByteArray

<#>
Use...
Add-Type -assembly System.Security
$resCryptedPWD_ByteArray = [Convert]::FromBase64String((Get-Content -LiteralPath "$Password_File"))
$secLevel = [System.Security.Cryptography.DataProtectionScope]::LocalMachine
$clearPWD_ByteArray = [System.Security.Cryptography.ProtectedData]::Unprotect( $resCryptedPWD_ByteArray, $null, $secLevel )
$enc = [system.text.encoding]::Unicode

...To retrieve the password from $Password_File

Then use...

$enc.GetString($clearPWD_ByteArray)

...As your password
</#>

【讨论】:

  • 谢谢。我使用此解决方案将我的密码存储在从脚本调用的服务器上。然后我修改了我的脚本以使用我创建的会话调用 .bat 文件。
【解决方案2】:

听起来像double hop problem。众所周知,这些很难解决,因为您传递的凭据无法通过第二个系统进行身份验证。

CredSSP is a solution,但它确实会增加安全风险,因此请谨慎使用,确保您了解配置,并确保配置正确。

【讨论】:

  • 这是一个有用的建议。我决定走一条稍微不同的路线,并在远程调用的 powershell 脚本中创建一个会话。
猜你喜欢
  • 2015-02-20
  • 2014-08-23
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多