如何在标签中显示此输出?

格雷格·贝里尔

我正在尝试显示一个脚本的输出,该脚本在域控制器中搜索已锁定到我创建的 GUI 中的标签中并遇到问题的帐户。我已经能够做到这一点,而我使用的其他脚本没有问题,但是这个有点不同,我是一个完整的新手,并且一直在努力弄清楚。在 Powershell 中单独运行脚本时,它会以美观、易于准备的格式输出信息,但我无法将这些信息放入我的标签中。

#LOCKOUT TRACE BUTTON
$Button16                         = New-Object system.Windows.Forms.Button
$Button16.text                    = "Lockout Trace"
$Button16.width                   = 85
$Button16.height                  = 30
$Button16.location                = New-Object 
System.Drawing.Point(105,113)
$ErrorActionPreference = 'SilentlyContinue'
$Button16.Add_Click(
{
$Label2.Text = ""
Import-Module ActiveDirectory
$UserName = $($TextBox1.Text)
#Get DCs
$PDC = (Get-ADDomainController -Discover -Service PrimaryDC).Name
$DCs = (Get-ADDomainController -Filter *).Name
#Get user info
$UserInfo = Get-ADUser -Identity $UserName
#Search PDC for lockout events with ID 4740
$LockedOutEvents = foreach ($DC in $DCs) { Get-WinEvent -ComputerName $DC 
-FilterHashtable @{LogName='Security';Id=4740} -ErrorAction Stop | Sort- 
Object -Property TimeCreated -Descending }
#Parse and filter out lockout events
Foreach($Event in $LockedOutEvents)
{
If($Event | Where {$_.Properties[2].value -match $UserInfo.SID.Value})
{

$Event | Select-Object -Property @( 
    @{Label = 'User'; Expression = {$_.Properties[0].Value}}
    @{Label = 'DomainController'; Expression = {$_.MachineName}}
    @{Label = 'EventId'; Expression = {$_.Id}}
    @{Label = 'LockoutTimeStamp'; Expression = {$_.TimeCreated}}
    @{Label = 'Message'; Expression = {$_.Message -split "`r" | Select - 
    First 1}}
    @{Label = 'LockoutSource'; Expression = {$_.Properties[1].Value}}
  )

}}
$Label2.Text = ("User = $($_.Properties[0].Value)
DomainController = $($_.MachineName)
EventID = $($_.ID)
LockoutTimeStamp = $($_.TimeCreated)
Message = $($_.Message -split "`r" | Select -First 1)
LockoutSource = $($_.Properties[1].Value)")
}
)
$main_form.Controls.Add($Button16)

在提供的代码中,它按预期在自己的行上返回 "User =" "DomainController =" "Event ID =" 等,只是没有数据跟随。如果我将“$Label2.Text =”放在“Foreach($Event in $LockedOutEvents)”之前,所有数据都将在标签中返回,但它们都在一条连续线上。我希望它在创建的标签中在自己的单独行上输出数据点,所以我向天才寻求帮助。这个网站已经回答了我在这次旅程中的不止一个问题,我希望还有另一个问题。提前致谢。

跟随

您显示的代码有时会在任意位置用换行符中断。我希望这只是试图在问题中格式化它。

至于我认为你想要什么,最好的办法可能是不使用 Label 对象在表单中显示结果,而是使用带有滚动条的多行 TextBox,这样您就知道返回的值将始终适合表单。

现在,我保持$Label2原样,但将您改造$Button16.Add_Click()成:

$Button16.Add_Click({
    $Label2.Text = ""
    $UserName = $TextBox1.Text
    # Try and get the user object from the name entered in the textbox
    $UserInfo = Get-ADUser -Identity $UserName
    if (!$UserInfo) {
        $Label2.Text = "$UserName not found!"
        return
    }

    # Get DCs
    ## $PDC = (Get-ADDomainController -Discover -Service PrimaryDC).Name
    $DCs = (Get-ADDomainController -Filter *).Name
    # Search DC's for lockout events with ID 4740
    $LockedOutEvents = foreach ($DC in $DCs) { 
        Get-WinEvent -ComputerName $DC -FilterHashtable @{LogName='Security';Id=4740} -ErrorAction SilentlyContinue | 
        Where {$_.Properties[2].value -match $UserInfo.SID} |
        Sort-Object -Property TimeCreated -Descending 
    }
    if ($LockedOutEvents) {
        # Parse and filter out lockout events. Collect the outputted string(s) in a variable
        # use 'Format-List | Out-String' to collect nicely formatted text
        $events = $LockedOutEvents | ForEach-Object {
            ($_ | Select-Object -Property @( 
                @{Name = 'User'; Expression = {$_.Properties[0].Value}}
                @{Name = 'DomainController'; Expression = {$_.MachineName}}
                @{Name = 'EventId'; Expression = {$_.Id}}
                @{Name = 'LockoutTimeStamp'; Expression = {$_.TimeCreated}}
                @{Name = 'Message'; Expression = {$_.Message -split '\r?\n' | Select-Object -First 1}}
                @{Name = 'LockoutSource'; Expression = {$_.Properties[1].Value}}
              ) | Format-List | Out-String).Trim()
        }
        # join the collected output and insert that into the label
        $Label2.Text = $events -join ([Environment]::NewLine * 2)
    }
    else {
        $Label2.Text = "No Lockout events found for user $UserName"
    }
})

Import-Module ActiveDirectory行可以向上移动到脚本的第一行。

希望这可以帮助

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在C中解决此输出?

来自分类Dev

如何在C中解决此输出?

来自分类Dev

如何在MySQL中查询此输出?

来自分类Dev

此代码如何显示此输出?

来自分类Dev

如何在标签中显示GET请求

来自分类Dev

如何在Emacs中显示标签

来自分类Dev

如何在Mutt中显示GMail标签?

来自分类Dev

如何在标签文本中显示计数?

来自分类Dev

如何在标签中显示日期

来自分类Dev

如何在标签文本中显示计数?

来自分类Dev

如何在Android中显示默认标签?

来自分类Dev

如何在双引号中显示标签?

来自分类Dev

如何在表格中显示PHP输出?

来自分类Dev

如何在 Xcode 中显示 cppcheck 输出?

来自分类Dev

如何在android中实现此标签样式?

来自分类Dev

如何在Firefox中启用此小标签功能?

来自分类Dev

如何在android中实现此标签样式?

来自分类Dev

如何在Rstudio中查看此动画代码的输出

来自分类Dev

如何在变量中捕获此mysql输出消息?

来自分类Dev

如何在 SQL Server 中查询此输出?

来自分类Dev

如何在 Javascript 中获取此数字模式输出

来自分类Dev

如何使用spss语法在输出中显示类别标签的数值

来自分类Dev

如何在 Ruby on Rails 中显示此数组中的值

来自分类Dev

如何在php输出中添加id标签?

来自分类Dev

如何在输入标签中存储 jquery 输出的值

来自分类Dev

如何在HTML列表中显示此JS数组?

来自分类Dev

如何在DIV元素中显示此功能的结果?

来自分类Dev

如何在PrimeNG表中按json显示此组?

来自分类Dev

如何在打字稿中显示此useContext用户?