【发布时间】: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 地址,查找最后登录的用户)。这意味着当我测试脚本时,我可以选择“运行方式”并输入服务帐户凭据。
第三方程序不使用服务凭据运行,这意味着我必须在程序端进行。
如何让这个程序作为服务帐户自动运行? 我将如何对其进行硬编码? 并且用户名和密码必须加密。
你能指出我正确的方向吗?
它还展示了如何将加密的密码输入到文件中
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