繁体   English   中英

存储凭据以在PHP应用程序中运行PowerShell脚本(远程)

[英]Storing credentials to run PowerShell script in PHP application (remotely)

我正在运行带有XAMPP(php / mysql / apache)和PowerShell v3的Windows 2012 R2服务器。 在过去的几个月里,我的代码一直工作得很好,可以存储和检索加密的密码

我的问题是,如何存储我的Powershell脚本使用的密码? 当我在本地运行时脚本运行正常,但是当我从远程客户端上的PHP应用程序运行它们时,我得到“服务器已拒绝客户端凭据”。 错误。

我试过这样创建一个密码文件:

$File = "D:\path\to\password.txt"
[Byte[]] $key = (1..16)
$Password = "myPassword" | ConvertTo-SecureString -AsPlainText -Force
$Password | ConvertFrom-SecureString -key $key | Out-File $File

并检索和使用它像这样:

$PasswordFile = "D:\path\to\password.txt"
[Byte[]] $key = (1..16)
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "domain\username", (Get-Content $PasswordFile | ConvertTo-SecureString -Key $key)

然后像这样使用它:

get-wmiobject Win32_Service -ComputerName $dc.name -credential $credential | select systemname,name,startmode,caption,state

这里使用略有不同的命令

也许你想尝试他们的方式

$SecureString = Read-Host -AsSecureString
$EncryptedPW = ConvertFrom-SecureString -SecureString $SecureString
Set-Content -Path "C:\tmp\mypw.txt" -Value $EncryptedPW

和检索

$EncryptedPW = Get-Content -Path "C:\tmp\mypw.txt"
$SecureString = ConvertTo-SecureString -String $EncryptedPW
$Credentials = New-Object System.Management.Automation.PSCredential "User", $SecureString

我还没试过。 但值得一试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM