Parameters 硬代码在Powershell中作为加密凭据运行
在您的帮助下,我终于可以使用这个powershell脚本了Parameters 硬代码在Powershell中作为加密凭据运行,parameters,hashtable,powershell-2.0,runas,Parameters,Hashtable,Powershell 2.0,Runas,在您的帮助下,我终于可以使用这个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){
$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
但我仍在试图找出如何使用这些凭据运行脚本,我认为关键点(使用备用凭据的点)是Get-EventLog cmdlet。此cmdlet不支持-Credential,但您可以改用Get-WinEvent。这可能意味着您需要更改过滤器的位置。我还建议移动过滤器的一部分(全部?)以获得WinEvent cmdlet:
Get-WinEvent -FilterHashtable @{
ID = 4624
LogName = 'Security'
} -ComputerName $Sender_IP -Credential $Credential
顺便说一句:如果您使用convertTo/From SecureString存储凭据,它们将仅对执行该操作的帐户有效(因此,如果您的第三方工具在不同凭据下运行,它将无法处理您生成的凭据)