【问题标题】:Hardcode run-as encrypted credentials in PowershellPowershell 中的硬编码作为加密凭据运行
【发布时间】:2013-08-08 13:40:51
【问题描述】:

在你的帮助下,我终于得到了这个 powershell 脚本!!!!

$line_array = @()
$multi_array = @()
[hashtable]$my_hash = @{}

foreach ($i in $args){
   $line_array+= $i.split(" ")
}

foreach ($j in $line_array){
    $multi_array += ,@($j.split("="))
}

foreach ($k in $multi_array){
    $my_hash.add($k[0],$k[1])
}

$Sender_IP = $my_hash.Get_Item("sender-ip")

$eventList = @()
Get-EventLog "Security" -computername $Sender_IP `
    | Where -FilterScript {$_.EventID -eq 4624 -and $_.ReplacementStrings[4].Length -gt 10 -and $_.ReplacementStrings[5] -notlike "*$"} `
    | Select-Object -First 2 `
    | foreach-Object {
        $row = "" | Select UserName, LoginTime
        $row.UserName = $_.ReplacementStrings[5]
        $row.LoginTime = $_.TimeGenerated
        $eventList += $row
        }
$userId = $eventList[0].UserName
$userId

可以调用

.\FOO.ps1 sender-ip=10.2.23.40 sender-name=joe sender-id=djoe

但是现在,我需要为这个第三方程序调用它做准备。只有当它作为服务帐户运行时,该脚本才会产生输出(即,对于给定的 IP 地址,查找最后登录的用户)。这意味着当我测试脚本时,我可以选择“运行方式”并输入服务帐户凭据。

第三方程序不使用服务凭据运行,这意味着我必须在程序端进行。

如何让这个程序作为服务帐户自动运行? 我将如何对其进行硬编码? 并且用户名和密码必须加密。

你能指出我正确的方向吗?

编辑:我正在阅读此链接,http://blogs.technet.com/b/robcost/archive/2008/05/01/powershell-tip-storing-and-using-password-credentials.aspx

它还展示了如何将加密的密码输入到文件中

PS C:\> read-host -assecurestring | convertfrom-securestring | out-file C:\cred.txt

然后把它带回脚本

PS C:\> $password = get-content C:\cred.txt | convertto-securestring

并创建凭证对象

PS C:\> $credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "myusername",$password

但我仍在尝试弄清楚如何使用这些凭据使脚本运行

【问题讨论】:

    标签: parameters hashtable powershell-2.0 runas


    【解决方案1】:

    我假设关键点(使用备用凭据的点)是 Get-EventLog cmdlet。此 cmdlet 不支持 -Credential,但您可以改用 Get-WinEvent。这可能意味着您需要更改过滤器位置。我建议还将过滤器的一部分(全部?)移动到 Get-WinEvent cmdlet:

    Get-WinEvent -FilterHashtable @{ 
        ID = 4624
        LogName = 'Security' 
    } -ComputerName $Sender_IP -Credential $Credential
    

    顺便说一句:如果您使用 convertTo/From-SecureString 存储凭据,它们将仅适用于执行操作的帐户(因此,如果您的第 3 方工具在不同的凭据下运行,它将无法处理您生成的凭据)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-08
      • 2011-09-14
      • 2011-02-10
      • 1970-01-01
      相关资源
      最近更新 更多