简体   繁体   English

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

[英]How do I display this output in a label?

I'm trying to display the output of a script that searches domain controllers for accounts which have been locked into a label in a GUI I've created and running in to problems. 我正在尝试显示一个脚本的输出,该脚本在域控制器中搜索在我创建的GUI中已锁定到标签中且存在问题的帐户中。 I've been able to do this without problems with other scripts I've utilized, but this one is a little different and I am a complete novice and have been pulling my hair out trying to figure it out. 我已经能够做到这一点,而我使用的其他脚本没有任何问题,但是这个脚本有些不同,我是一个完全新手,并且一直在努力寻找答案。 When running the script by itself in Powershell, it outputs the information in a beautiful, easy to ready format but I can't get it to put that information into my label. 在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)

In the code provided, it returns "User =" "DomainController =" "Event ID =" etc. on their own lines as expected, just without the data following. 在提供的代码中,它按预期在自己的行上返回“ User =“” DomainController =“” Event ID =“等。 If I place "$Label2.Text =" before "Foreach($Event in $LockedOutEvents)", all the data is returned in the Label however it's all on one continuous line. 如果我将“ $ Label2.Text =”放在“ Foreach($ LockedOutEvents)中的$ Event”之前,则所有数据都将返回到Label中,但是它们都连续一行。 I'd like it to output the data points on their own individual lines in the created label so I'm turning to the geniuses for help. 我希望在创建的标签中在自己的单独行上输出数据点,因此我想向天才们寻求帮助。 This site has answered more than one of my questions on this journey and I'm hoping for another. 这个网站回答了我有关此旅程的多个问题,希望能有另一个问题。 Thanks in advance. 提前致谢。

The code you show sometimes breaks at arbitrary places with newlines. 您显示的代码有时会在换行符的任意位置中断。 I hope that is just from trying to format it in the question. 我希望这只是尝试在问题中进行格式化。

As for what I think you want, probably the best thing is to not use a Label object to show the results in the form, but instead use a multiline TextBox with scrollbars so you know the returned value wil always fit the form. 至于我想您想要的东西,可能最好的办法是不使用Label对象在表单中显示结果,而是使用带有滚动条的多行TextBox,以便您知道返回的值始终适合表单。

For now, I've left the $Label2 as is, but remodelled your $Button16.Add_Click() to this: 现在,我将$Label2保留$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"
    }
})

The Import-Module ActiveDirectory line can be moved up to the top line in your script. 可以将Import-Module ActiveDirectory行移到脚本的顶行。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM