【问题标题】:script going to each IP on host file, then execute script脚本转到主机文件上的每个 IP,然后执行脚本
【发布时间】:2016-05-08 19:48:14
【问题描述】:

我一直在做很多研究,但找不到一种方法来获取一个 powershell 脚本来查看主机文件,然后转到每个主机名并提取日志并将它们保存在一个地方,我有一个脚本正在运行这将提取日志,但仅在本地主机上。我该怎么做才能让脚本转到每台机器并执行我的脚本?

这就是它的牛肉我还有其他步骤将它放入文件只是想知道我如何使用您提到的命令跳转到每台机器并收集这些文件。

$computers = get-content C:\MyScripts\MachineNames.txt

foreach ($computer in $computers) 
{
$Date = [DateTime]::Now.AddDays(-7)
$Date.tostring("MM-dd-yyyy"), $env:Computername
Get-EventLog "Security" -After $Date 

| Where -FilterScript {$_.EventID -eq 4624 -and $_.ReplacementStrings[4].Length -gt 10 -and $_.ReplacementStrings[5] -notlike "*$"} `
| foreach-Object 
$row = "" | Select UserName, LoginTime
$row.UserName = $_.ReplacementStrings[5]
$row.LoginTime = $_.TimeGenerated
$row # this will put the current row to the pipeline 
} | Export-Csv 'c:\output.csv

现在我得到:在 line:9 char:1 + |其中 -FilterScript {$.EventID -eq 4624 -and $.ReplacementStrings[4].Length ... + ~ 不允许使用空的管道元素。 在行:15 字符:3 + } |导出-CSV 'c:\output.csv + ~ 不允许使用空的管道元素。 在行:15 字符:16 + } |导出-CSV 'c:\output.csv + ~~~~~~~~~~~~~~ 该字符串缺少终止符:'。 + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : EmptyPipeElement

【问题讨论】:

    标签: powershell scripting


    【解决方案1】:

    使用 psexec.exe 或扩展你的代码来读取远程机器

    您想共享您的脚本以便有人查看并扩展功能吗?

    【讨论】:

    • Frédéric Bonneau 为您提供了一个很好的答案。但 $env:computername 是本地主机。你的脚本也没有到每台机器上,只是在本地机器上重复。
    【解决方案2】:

    这是正确使用的脚本

    $computers = get-content C:\MyScripts\MachineNames.txt
    
    $Result = foreach ($computer in $computers)  {
    
    $Date = [DateTime]::Now.AddDays(-1)
    $Date.tostring("MM-dd-yyyy"), $env:Computername
    Get-EventLog "Security" -After $Date | Where-object -FilterScript {$_.EventID -eq 4624 -and $_.ReplacementStrings[4].Length -gt 10 -and $_.ReplacementStrings[5] -notlike "*$"} |
    foreach-Object {
    $row = "" | Select UserName, LoginTime
    $row.UserName = $_.ReplacementStrings[5]
    $row.LoginTime = $_.TimeGenerated
    $row # this will put the current row to the pipeline 
    
                     } 
    }
    $Result | Export-Csv C:\MyScripts\output.csv
    

    【讨论】:

      猜你喜欢
      • 2020-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      • 2014-02-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多