使用当前Powershell凭证进行远程调用

问题描述:

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

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

我已经试过几件事情要纠正这种情况,并做了一些研究,在PowerShell中的Invoke-Command命令行和Windows批量的runas命令。 runas命令要求输入用户密码,这是不可接受的,因为这是一个自动脚本。除了进行初始START或STOP呼叫之外,是否有人有任何想法可以使我的工作干净利落,无需用户干预?

+0

使用Credssp的'Invoke-Command'是最常用的方法。看到我对[这个问题]的答案(http://stackoverflow.com/questions/24903878/powershell-server-network-drive)。 – 2014-12-04 05:01:11

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 
</#> 
+0

谢谢。我使用此解决方案将密码存储在从脚本调用的服务器上。然后我修改我的脚本以使用我创建的会话调用.bat文件。 – sfedak 2014-12-18 20:58:53

听起来像是double hop problem。这些都是非常难以解决的,因为您将通过的凭据无法通过第二个系统进行身份验证。

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

+0

这是一个有用的建议。我决定采用稍微不同的路线,并在远程调用的powershell脚本中创建一个会话。 – sfedak 2014-12-18 21:00:37