Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 使用当前Powershell凭据进行远程呼叫_Windows_Powershell_Batch File - Fatal编程技术网

Windows 使用当前Powershell凭据进行远程呼叫

Windows 使用当前Powershell凭据进行远程呼叫,windows,powershell,batch-file,Windows,Powershell,Batch File,我有一个Powershell脚本,用于远程调用其他服务器上的其他Powershell脚本。该脚本用于关闭和启动不同服务器上的服务。Powershell脚本的设置方式是,我所要做的就是通过调用serverStartStop[START | STOP]来调用它,它会自动、有条理地转到服务器列表,并关闭每个服务器上的服务列表 我最近升级了系统,需要在启动一些服务后运行批处理脚本。我可以远程调用批处理脚本,但批处理脚本调用另一个命令,该命令尝试访问网络上的共享。该命令失败,因为用于调用该命令的任何用户都

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

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


我尝试了几种方法来纠正这种情况,并对Powershell中的
Invoke命令
commandlet和Windows批处理中的
runas
命令进行了一些研究。
runas
命令要求用户输入密码,这是不可接受的,因为这是一个自动脚本。有没有人知道我怎样才能在没有用户交互的情况下干净地完成这项工作,而不是进行初始启动或停止调用?

调用命令上的-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
</#>
添加类型-程序集系统.安全性
#字符串到密码
$passwordASCII=读取主机-提示“输入密码”
#字符串到整数数组
$enc=[system.text.encoding]::Unicode
$clearPWD_ByteArray=$enc.GetBytes($passwordASCII.tochararray())
#加密
$secLevel=[System.Security.Cryptography.DataProtectionScope]::LocalMachine
$bakCryptedPWD_ByteArray=[System.Security.Cryptography.ProtectedData]::Protect($clearPWD_ByteArray,$null,$secLevel)
#以Base 64形式存储
$B64PWD_ByteArray=[Convert]:ToBase64String($bakCryptedPWD_ByteArray)
设置内容-文字路径c:\Temp\pass.txt-值$B64PWD_ByteArray
使用。。。
添加类型-程序集System.Security
$resCryptedPWD_ByteArray=[Convert]::FromBase64String((获取内容-文字路径“$Password_文件”))
$secLevel=[System.Security.Cryptography.DataProtectionScope]::LocalMachine
$clearPWD_ByteArray=[System.Security.Cryptography.ProtectedData]::取消保护($resCryptedPWD_ByteArray,$null,$secLevel)
$enc=[system.text.encoding]::Unicode
…从$password\u文件检索密码
然后使用。。。
$enc.GetString($clearPWD_ByteArray)
…作为您的密码
听起来像是一场灾难。众所周知,解决这些问题非常困难,因为您要传递的凭据无法通过第二个系统的身份验证


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

Invoke Command
使用Credssp是最常用的方法。请看我的答案。谢谢。我使用此解决方案将密码存储在从脚本调用的服务器上。然后,我修改了脚本,使用我创建的会话调用.bat文件。这是一个有用的建议。我决定采用稍微不同的方法,在powershell脚本中创建一个名为远程的会话。