0

我仍然是 powershell 的初学者,但我喜欢学习和研究它可以做的所有事情。

因此,我希望创建一个将输出到 datagridview 表的脚本。我创建了一个足够简单的表单,它有一个多行文本框,您可以在其中输入服务器列表,然后只需一个简单的“检查”按钮,该按钮调用我的函数来实际查询每个服务器的服务并将该信息输出到它下面的datagridview。

那部分对我来说很容易创建。我的事情是,我希望它执行以下操作“:

  • 查询每台服务器并拉取该服务器上所有服务的列表
  • 然后在datagridview中,我希望它在第1列中显示服务名称,然后在第2列中显示有多少台服务器拥有该服务

希望从那时起。无法真正提供任何代码,因为我不知道如何正确编写它!我在想有办法生成完整的列表,然后做一个循环和计数类型的事情,但不确定。

我感谢您对此提供的任何帮助。一直在疯狂地搜索和阅读。要么不正确的搜索条件,要么就是不适合我。

更新 - 添加当前脚本我有:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

$xForm = 800
$yForm = 800

$SVCForm = New-Object System.Windows.Forms.Form 
$SVCForm.Text = "Automatic Services Query"
$SVCForm.Size = New-Object System.Drawing.Size($xForm,$yForm)
$SVCForm.FormBorderStyle = "FixedSingle" 
$SVCForm.StartPosition = "CenterScreen"
$SVCForm.ControlBox = $true
$SVCForm.KeyPreview = $True
$SVCForm.ShowIcon = $false
$SVCForm.MinimizeBox = $True
$SVCForm.MaximizeBox = $false

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(365,720)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$x=$CancelButton.Text;$SVCForm.Close()})
$SVCForm.Controls.Add($CancelButton)

$ServerListGroup = New-Object System.Windows.Forms.GroupBox 
$ServerListGroup.Location = New-Object System.Drawing.Size(5,10)
$ServerListGroup.size = New-Object System.Drawing.Size(780,300)
$ServerListGroup.text = "Enter list of servers you want to check:"
$SVCForm.Controls.Add($ServerListGroup)

$ServerList = New-object System.Windows.Forms.TextBox
$ServerList.Location = New-object System.Drawing.Size(5,25)
$ServerList.Size = New-Object System.Drawing.Size(280,270)
$ServerList.Multiline = $True
$ServerList.ScrollBars = "Vertical"
#$ServerList.add_TextChanged({ONValButton})
$ServerListGroup.Controls.Add($ServerList)

$SVCButton = New-Object System.Windows.Forms.Button
$SVCButton.Location = New-Object System.Drawing.Size(505,140)
$SVCButton.Size = New-Object System.Drawing.Size(180,22)
$SVCButton.Text = "SVC Check"
$SVCButton.Enabled = $True
$SVCButton.Add_Click({SvcCheckCount})
$ServerListGroup.Controls.Add($SVCButton)


$SvcListGroup = New-Object System.Windows.Forms.GroupBox 
$SvcListGroup.Location = New-Object System.Drawing.Size(5,330)
$SvcListGroup.size = New-Object System.Drawing.Size(780,380)
$SvcListGroup.text = "Automatic Running/Not-Running Services are listed below:"
$SVCForm.Controls.Add($SvcListGroup)

#$SvcList = New-object System.Windows.Forms.TextBox
#$SvcList.Location = New-object System.Drawing.Size(5,25)
#$SvcList.Size = New-Object System.Drawing.Size(765,350)
#$SvcList.Multiline = $True
#$SvcList.ScrollBars = "Vertical"
#$SvcList.Readonly= $True
#$SvcListGroup.Controls.Add($SvcList)

$SvcGrid = New-Object System.Windows.Forms.DataGridView
$SvcGrid.Location = New-Object System.Drawing.Size(5,15)
$SvcGrid.Size = New-Object System.Drawing.Size(760,360)
$SvcGrid.ColumnHeadersBorderStyle = [System.Windows.Forms.DataGridViewHeaderBorderStyle]::Single
$SvcGrid.CellBorderStyle = [System.Windows.Forms.DataGridViewCellBorderStyle]::Single
$SvcGrid.ColumnHeadersHeightSizeMode = [System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode]::DisableResizing
$SvcGrid.GridColor = [System.Drawing.Color]::Black
$SvcGrid.RowHeadersVisible = $false
$SvcGrid.AllowUserToAddRows = $false
$SvcGrid.AllowUserToResizeColumns = $False
$SvcGrid.AllowUserToResizeRows = $false
$SvcGrid.ColumnHeadersHeight = 23
$SvcGrid.SelectionMode = [System.Windows.Forms.DataGridViewSelectionMode]::FullRowSelect
$Scroll = New-Object System.Windows.Forms.VScrollBar
$Scroll.Dock = [System.Windows.Forms.DockStyle]::Right
$Scroll.width = 18
$Scroll.isAccessible = $false
$SvcGrid.Controls.Add($Scroll)                
$SvcGrid.Columns.Add("Name","Service Name") > $null
$SvcGrid.Columns["Name"].Width = 200
$SvcGrid.Columns["Name"].HeaderCell.Style.Alignment = [System.Windows.Forms.DataGridViewContentAlignment]::MiddleLeft
$SvcGrid.Columns["Name"].DefaultCellStyle.Alignment = [System.Windows.Forms.DataGridViewContentAlignment]::MiddleLeft
$SvcGrid.Columns["Name"].ReadOnly = $False
$SvcGrid.Columns.Add("StartMode", "Start Mode") > $null
$SvcGrid.Columns["StartMode"].Width = 180
$SvcGrid.Columns["StartMode"].HeaderCell.Style.Alignment = [System.Windows.Forms.DataGridViewContentAlignment]::MiddleLeft
$SvcGrid.Columns["StartMode"].DefaultCellStyle.Alignment = [System.Windows.Forms.DataGridViewContentAlignment]::MiddleLeft
$SvcGrid.Columns["StartMode"].ReadOnly = $False
$SvcGrid.Columns.Add("Running","Running") > $null
$SvcGrid.Columns["Running"].Width = 180
$SvcGrid.Columns["Running"].HeaderCell.Style.Alignment = [System.Windows.Forms.DataGridViewContentAlignment]::MiddleCenter
$SvcGrid.Columns["Running"].DefaultCellStyle.Alignment = [System.Windows.Forms.DataGridViewContentAlignment]::MiddleCenter
$SvcGrid.Columns["Running"].ReadOnly = $true
$SvcGrid.Columns.Add("NotRunng","Not Running")>$null
$SvcGrid.Columns["NotRunng"].Width = 180
$SvcGrid.Columns["NotRunng"].HeaderCell.Style.Alignment = [System.Windows.Forms.DataGridViewContentAlignment]::MiddleCenter
$SvcGrid.Columns["NotRunng"].DefaultCellStyle.Alignment = [System.Windows.Forms.DataGridViewContentAlignment]::MiddleCenter
$SvcGrid.Columns["NotRunng"].ReadOnly = $true
$SvcListGroup.Controls.Add($SvcGrid)




Function SvcCheckCount
{
   $SvcGrid.Rows.Clear()
   $Servername = $ServerList.Text.Split("`n")|%{$_.trim()}

    foreach($Server in $Servername)
           {
             $Server = $Server.ToUpper()
                $svctest = Get-WmiObject Win32_Service | Where-Object {$_.StartMode -eq 'Auto'} | Select-Object Name, Startmode, State

                Foreach ($svc in $svctest)
                {
                $name = $svc.Name
                $startmode = $svc.StartMode
                $state = $svc.State

                If($state-eq"Running"){$SvcGrid.Rows.add($name,$startmode,$state)}
                If($state-eq"Stopped"){$SvcGrid.Rows.add($name,$startmode,$null,$state)}
                }

              }  



}




$SVCForm.Topmost = $True

$SVCForm.Add_Shown({$SVCForm.Activate()})
[void] $SVCForm.ShowDialog()
4

1 回答 1

1

由于您已经拥有此功能的 GUI 部分,因此我将重点关注您要查找的数据的分组。进入这个我很模糊的一件事是为什么你有这些if陈述,但如果需要,我们可以解决这个问题。以下内容旨在代替您的foreach循环

$services = @()
foreach($Server in $Servername){
    $Server = $Server.ToUpper()
    $services += Get-WmiObject Win32_Service -ComputerName $Server -Filter 'StartMode="Auto"' |
            Select-Object Name, Startmode, State | 
            Add-Member -MemberType NoteProperty -Value $Server -Name "Server" -PassThru
}  
$services | Group-Object Name | ForEach-Object{
    $SvcGrid.Rows.add($_.Name,$_.Count)
}

第一件事:您没有$server在 wmi 调用中使用该变量,因此所有结果都来自您运行它的一台机器。另外,我把它移到了Where一个-Filter,这样可以加快远程机器上的处理速度。

将所有这些信息收集到一个数组中(添加计算机名称以防以后有用)。然后,使用该用途的结果Group-Object来获取您正在寻找的计数。将其输入另一个ForEach-Object以在您的网格中获得结果(我尚未测试过。)

旁注:如果您只想要网格中的信息,您可以使用Out-GridView

$services | Group-Object Name | Select name,count | Out-GridView
于 2014-12-16T16:29:12.677 回答