0

我想将我创建的现有 powershell 脚本包装到 GUI 中,但没有花几天时间试图弄清楚我想我在这里发布。我已经为脚本的外观创建了一个原始表单,但需要帮助来隐藏它。

我运行的脚本将计算机帐户添加到给定 3 个选项的安全组。针对 localhost、计算机列表或手动输入主机名运行脚本。此外,如果可能,我想选择一个组并根据选择进行操作,而不是手动输入组名。

我的主要脚本

   Import-Module ActiveDirectory
   cls
   $OU = "OU=TESTScripts,OU=Groups,OU=Company,DC=Company,DC=local"
   $AppGroupList = Get-ADGroup -Filter {GroupCategory -eq "security"} -SearchBase "$OU"
   $AppGroupList | Select-Object @{Name="AD Security Group";Expression={$_.Name}}

   $title = "Add manually entered computer, localhost or list of computers to SCCM        Application Install Group"
   $message = "Please select an option from below:-"

   $1 = New-Object System.Management.Automation.Host.ChoiceDescription "Enter &Hostname"

   $2 = New-Object System.Management.Automation.Host.ChoiceDescription "Add &List of Computers"

   $3 = New-Object System.Management.Automation.Host.ChoiceDescription "&Add localhost"

   $options = [System.Management.Automation.Host.ChoiceDescription[]]($1, $2, $3)
   $result = $host.ui.PromptForChoice($title, $message, $options, 0)


switch ($result)
    {
        0 {$Computers = Read-Host "Enter Hostname>"}                          

        1 {$Computers = get-content "c:\scripts\Computers.csv"}                   

        2 {$Computers = $env:computername}

    }

    $AppGroup = Read-Host "Application Security Group Name>"
    $Group = Get-ADGroup -Identity $AppGroup     

 foreach ($Hostname in $Computers){

        $member = Get-ADComputer -Identity "$Hostname" -Properties MemberOf | Select-Object MemberOf
        $pc = Get-ADComputer "$Hostname"

            if ($member.Memberof -like "$Group"){
                Write-Host ""   
                Write-Host $Hostname is already a member of $AppGroup -ForegroundColor "Yellow"     
            }

            else {
                Add-ADGroupMember $AppGroup $pc
                Write-Host ""
                Write-Host $Hostname has been added to $AppGroup -ForegroundColor "Green"   


            }
}   

`

我的表单代码如下所示:-

    #Generated Form Function
    function GenerateForm {
    ########################################################################
    # Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0
    # Generated On: 22/03/2013 11:17 AM
    ########################################################################

    #region Import the Assemblies
    [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
    [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
    #endregion

    #region Generated Form Objects
    $form1 = New-Object System.Windows.Forms.Form
    $label3 = New-Object System.Windows.Forms.Label
    $Hostname = New-Object System.Windows.Forms.Label
    $Hostname = New-Object System.Windows.Forms.TextBox
    $Install = New-Object System.Windows.Forms.Button
    $label1 = New-Object System.Windows.Forms.Label
    $Browse = New-Object System.Windows.Forms.Button
    $checkBox1 = New-Object System.Windows.Forms.CheckBox
    $ListApps = New-Object System.Windows.Forms.ComboBox
    $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
    #endregion Generated Form Objects

    #----------------------------------------------
    #Generated Event Script Blocks
    #----------------------------------------------
    #Provide Custom Code for events specified in PrimalForms.
    $handler_textBox1_TextChanged= 
    {
    #TODO: Place custom script here

    }

    $Browse_OnClick= 
    {
    #TODO: Place custom script here

    }

    $handler_label2_Click= 
    {
    #TODO: Place custom script here

    }

    $handler_form1_Load= 
    {
    #TODO: Place custom script here

    Import-Module ActiveDirectory

    $OU = "OU=TESTScripts,OU=Groups,OU=Company,DC=Company,DC=local"
    $AppGroupList = Get-ADGroup -Filter {GroupCategory -eq "security"} -SearchBase "$OU"
    $AppGroupList | Select-Object @{Name="AD Security Group";Expression={$_.Name}}


    }

    $Install_OnClick= 
    {
    #TODO: Place custom script here

    }

    $handler_checkBox1_CheckedChanged= 
    {
    #TODO: Place custom script here

    }

    $OnLoadForm_StateCorrection=
    {#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
    }

    #----------------------------------------------
    #region Generated Form Code
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 423
    $System_Drawing_Size.Width = 284
    $form1.ClientSize = $System_Drawing_Size
    $form1.DataBindings.DefaultDataSourceUpdateMode = 0
    $form1.Name = "form1"
    $form1.Text = "Install Software"
    $form1.add_Load($handler_form1_Load)

    $label3.DataBindings.DefaultDataSourceUpdateMode = 0

    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 12
    $System_Drawing_Point.Y = 76
    $label3.Location = $System_Drawing_Point
    $label3.Name = "label3"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 20
    $System_Drawing_Size.Width = 185
    $label3.Size = $System_Drawing_Size
    $label3.TabIndex = 7
    $label3.Text = "Application Install Groups:-"

    $form1.Controls.Add($label3)

    $Hostname.DataBindings.DefaultDataSourceUpdateMode = 0

    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 206
    $System_Drawing_Point.Y = 146
    $Hostname.Location = $System_Drawing_Point
    $Hostname.Name = "Hostname"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 29
    $System_Drawing_Size.Width = 66
    $Hostname.Size = $System_Drawing_Size
    $Hostname.TabIndex = 6
    $Hostname.Text = "Enter Hostname"
    $Hostname.add_Click($handler_label2_Click)

    $form1.Controls.Add($Hostname)

    $Hostname.DataBindings.DefaultDataSourceUpdateMode = 0
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 91
    $System_Drawing_Point.Y = 155
    $Hostname.Location = $System_Drawing_Point
    $Hostname.Name = "Hostname"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 20
    $System_Drawing_Size.Width = 102
    $Hostname.Size = $System_Drawing_Size
    $Hostname.TabIndex = 5
    $Hostname.add_TextChanged($handler_textBox1_TextChanged)

    $form1.Controls.Add($Hostname)


    $Install.DataBindings.DefaultDataSourceUpdateMode = 0

    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 10
    $System_Drawing_Point.Y = 246
    $Install.Location = $System_Drawing_Point
    $Install.Name = "Install"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 38
    $System_Drawing_Size.Width = 259
    $Install.Size = $System_Drawing_Size
    $Install.TabIndex = 4
    $Install.Text = "Install"
    $Install.UseVisualStyleBackColor = $True
    $Install.add_Click($Install_OnClick)

    $form1.Controls.Add($Install)

    $label1.DataBindings.DefaultDataSourceUpdateMode = 0

    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 91
    $System_Drawing_Point.Y = 206
    $label1.Location = $System_Drawing_Point
    $label1.Name = "label1"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 18
    $System_Drawing_Size.Width = 164
    $label1.Size = $System_Drawing_Size
    $label1.TabIndex = 3
    $label1.Text = "Browse to add list of computers"

    $form1.Controls.Add($label1)


    $Browse.DataBindings.DefaultDataSourceUpdateMode = 0

    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 10
    $System_Drawing_Point.Y = 201
    $Browse.Location = $System_Drawing_Point
    $Browse.Name = "Browse"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 23
    $System_Drawing_Size.Width = 75
    $Browse.Size = $System_Drawing_Size
    $Browse.TabIndex = 2
    $Browse.Text = "Browse"
    $Browse.UseVisualStyleBackColor = $True
    $Browse.add_Click($Browse_OnClick)

    $form1.Controls.Add($Browse)


    $checkBox1.DataBindings.DefaultDataSourceUpdateMode = 0

    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 12
    $System_Drawing_Point.Y = 135
    $checkBox1.Location = $System_Drawing_Point
    $checkBox1.Name = "checkBox1"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 60
    $System_Drawing_Size.Width = 75
    $checkBox1.Size = $System_Drawing_Size
    $checkBox1.TabIndex = 1
    $checkBox1.Text = "Tick LocalHost"
    $checkBox1.UseVisualStyleBackColor = $True
    $checkBox1.add_CheckedChanged($handler_checkBox1_CheckedChanged)

    $form1.Controls.Add($checkBox1)

    $ListApps.DataBindings.DefaultDataSourceUpdateMode = 0
    $ListApps.FormattingEnabled = $True
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 12
    $System_Drawing_Point.Y = 108
    $ListApps.Location = $System_Drawing_Point
    $ListApps.Name = "ListApps"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 21
    $System_Drawing_Size.Width = 260
    $ListApps.Size = $System_Drawing_Size
    $ListApps.TabIndex = 0

    $form1.Controls.Add($ListApps)

    #endregion Generated Form Code

    #Save the initial state of the form
    $InitialFormWindowState = $form1.WindowState
    #Init the OnLoad event to correct the initial state of the form
    $form1.add_Load($OnLoadForm_StateCorrection)
    #Show the Form
    $form1.ShowDialog()| Out-Null

    } #End Function

    #Call the Function
    GenerateForm
4

3 回答 3

1

这个问题太宽泛了。请询问具体问题并自行尝试。如果你知道基本的 powerhsell 和/或基本的 c#,你会走得更远。为了帮助您根据需要填充应用程序组列表,请尝试:

$handler_form1_Load= 
{
#TODO: Place custom script here
#I would move this import line out of the form and to the top of the script 
Import-Module ActiveDirectory

$OU = "OU=TESTScripts,OU=Groups,OU=Metro Trains,DC=Metrotrains,DC=local"
$AppGroupList = Get-ADGroup -Filter {GroupCategory -eq "security"} -SearchBase "$OU" | Select -expand Name

#Fill the list with the groupnames  
$AppGroupList | % { $ListApps.Items.Add($_) }

}

您应该考虑在使用某个控件时禁用某些控件。前任。如果选中 localhost 复选框,请禁用文本框和浏览按钮(如果您不需要同时使用这三个按钮)。将事件处理程序添加click到安装按钮,该按钮从表单中获取当前值并运行“将计算机添加到组”代码。

于 2013-03-22T10:18:41.760 回答
0

当涉及到 Windows Presentation Foundation 或 WinForms 时,我并不擅长。我喜欢 PowerShell 照顾我。我希望这是你可以使用的东西:

$OU = Get-ADOrganizationalUnit -Filter * | Out-GridView -PassThru -Title 'Choose OU'
于 2013-03-22T02:16:16.027 回答
0

这是将上述脚本转换为 GUI 的完整代码。

#Generated Form Function
function GenerateForm {
########################################################################
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0
# Generated On: 25/03/2013 4:56 PM
# Generated By: MrMeaner
########################################################################

Import-Module ActiveDirectory

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
#endregion

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$radioButton3 = New-Object System.Windows.Forms.RadioButton
$radioButton2 = New-Object System.Windows.Forms.RadioButton
$radioButton1 = New-Object System.Windows.Forms.RadioButton
$label3 = New-Object System.Windows.Forms.Label
$textHostname = New-Object System.Windows.Forms.Label
$txtHostname = New-Object System.Windows.Forms.TextBox
$Install = New-Object System.Windows.Forms.Button
$label1 = New-Object System.Windows.Forms.Label
$Browse = New-Object System.Windows.Forms.Button
$ListApps = New-Object System.Windows.Forms.ComboBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events.

$Browse_OnClick= 
{
#When Browse button is clicked
$FileName = Select-FileDialog
$Computers = Get-Content $FileName
$Install.enabled = $true

}

$Install_OnClick= 
{
#Add computer to security group script

    if (!$ListApps.SelectedItem) {[System.Windows.Forms.MessageBox]::Show("Please select an SCCM application install group name from the drop down menu")

       }
    else {

        $AppGroup = $ListApps.SelectedItem.ToString()

        $Group = Get-ADGroup -Identity $AppGroup 

            foreach ($Hostname in $Computers){

                $member = Get-ADComputer -Identity "$Hostname" -Properties MemberOf | Select-Object MemberOf
                $pc = Get-ADComputer "$Hostname"

                if ($member.Memberof -like "$Group"){
                    [System.Windows.Forms.MessageBox]::Show("$Hostname is already a member of $AppGroup")     
                }

                else {
                    Add-ADGroupMember $AppGroup $pc
                    [System.Windows.Forms.MessageBox]::Show("$Hostname has been added to $AppGroup")  

                }
            }  
    }

}

$handler_radioButton1_CheckedChanged= 
{
#Localhost radio button
$txtHostname.enabled = $false
$Browse.enabled = $false
$Computers = $env:computername
$Install.enabled = $true

}

$handler_radioButton2_CheckedChanged= 
{
#Enter Hostname radiobutton
$txtHostname.enabled = $true
$Browse.enabled = $false
}

$handler_radioButton3_CheckedChanged= 
{
#Load list of computers radiobutton
$Browse.enabled = $true
$txtHostname.enabled = $false

}

$handler_textBox1_TextChanged= 
{
#Hostname textbox
$Computers = $txtHostname.Text.ToString()
$Install.enabled = $true

}

$handler_form1_Load= 
{
#Runs when script is loaded

$Install.enabled = $false
$OU = "OU=TESTScripts,OU=Groups,OU=Company,DC=Company,DC=local"
$AppGroupList = Get-ADGroup -Filter {GroupCategory -eq "security"} -SearchBase "$OU" | Select -expand Name


#Fill the list with the groupnames  
$AppGroupList | % { $ListApps.Items.Add($_) }
}


$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
    $form1.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 271
$System_Drawing_Size.Width = 284
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"
$form1.Text = "Install Software"
$form1.add_Load($handler_form1_Load)


$radioButton3.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 188
$System_Drawing_Point.Y = 58
$radioButton3.Location = $System_Drawing_Point
$radioButton3.Name = "radioButton3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 51
$System_Drawing_Size.Width = 88
$radioButton3.Size = $System_Drawing_Size
$radioButton3.TabIndex = 10
$radioButton3.TabStop = $True
$radioButton3.Text = "Load List of Computers"
$radioButton3.UseVisualStyleBackColor = $True
$radioButton3.add_CheckedChanged($handler_radioButton3_CheckedChanged)

$form1.Controls.Add($radioButton3)


$radioButton2.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 102
$System_Drawing_Point.Y = 54
$radioButton2.Location = $System_Drawing_Point
$radioButton2.Name = "radioButton2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 58
$System_Drawing_Size.Width = 80
$radioButton2.Size = $System_Drawing_Size
$radioButton2.TabIndex = 9
$radioButton2.TabStop = $True
$radioButton2.Text = "Enter Hostname"
$radioButton2.UseVisualStyleBackColor = $True
$radioButton2.add_CheckedChanged($handler_radioButton2_CheckedChanged)

$form1.Controls.Add($radioButton2)


$radioButton1.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 7
$System_Drawing_Point.Y = 59
$radioButton1.Location = $System_Drawing_Point
$radioButton1.Name = "radioButton1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 48
$System_Drawing_Size.Width = 76
$radioButton1.Size = $System_Drawing_Size
$radioButton1.TabIndex = 8
$radioButton1.TabStop = $True
$radioButton1.Text = "LocalHost"
$radioButton1.UseVisualStyleBackColor = $True
$radioButton1.add_CheckedChanged($handler_radioButton1_CheckedChanged)

$form1.Controls.Add($radioButton1)

$label3.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 7
$System_Drawing_Point.Y = 9
$label3.Location = $System_Drawing_Point
$label3.Name = "label3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 185
$label3.Size = $System_Drawing_Size
$label3.TabIndex = 7
$label3.Text = "SCCM Application Install Groups:-"

$form1.Controls.Add($label3)

$textHostname.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 140
$System_Drawing_Point.Y = 113
$textHostname.Location = $System_Drawing_Point
$textHostname.Name = "textHostname"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 27
$System_Drawing_Size.Width = 105
$textHostname.Size = $System_Drawing_Size
$textHostname.TabIndex = 6
$textHostname.Text = "Enter Hostname"
$textHostname.add_Click($handler_label2_Click)

$form1.Controls.Add($textHostname)

$txtHostname.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 7
$System_Drawing_Point.Y = 113
$txtHostname.Location = $System_Drawing_Point
$txtHostname.Name = "txtHostname"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 124
$txtHostname.Size = $System_Drawing_Size
$txtHostname.TabIndex = 5
$txtHostname.add_Leave($handler_textBox1_TextChanged)

$form1.Controls.Add($txtHostname)


$Install.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 7
$System_Drawing_Point.Y = 181
$Install.Location = $System_Drawing_Point
$Install.Name = "Install"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 82
$System_Drawing_Size.Width = 269
$Install.Size = $System_Drawing_Size
$Install.TabIndex = 4
$Install.Text = "Install"
$Install.UseVisualStyleBackColor = $True
$Install.add_Click($Install_OnClick)

$form1.Controls.Add($Install)

$label1.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 140
$System_Drawing_Point.Y = 155
$label1.Location = $System_Drawing_Point
$label1.Name = "label1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 115
$label1.Size = $System_Drawing_Size
$label1.TabIndex = 3
$label1.Text = "Load computer list"

$form1.Controls.Add($label1)


$Browse.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 7
$System_Drawing_Point.Y = 150
$Browse.Location = $System_Drawing_Point
$Browse.Name = "Browse"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 124
$Browse.Size = $System_Drawing_Size
$Browse.TabIndex = 2
$Browse.Text = "Browse"
$Browse.UseVisualStyleBackColor = $True
$Browse.add_Click($Browse_OnClick)

$form1.Controls.Add($Browse)

$ListApps.DataBindings.DefaultDataSourceUpdateMode = 0
$ListApps.FormattingEnabled = $True
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 7
$System_Drawing_Point.Y = 32
$ListApps.Location = $System_Drawing_Point
$ListApps.Name = "ListApps"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 21
$System_Drawing_Size.Width = 269
$ListApps.Size = $System_Drawing_Size
$ListApps.TabIndex = 0

$form1.Controls.Add($ListApps)

#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null

} #End Function


#Select File Explorer Function
function Select-FileDialog {

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$ofd = New-Object System.Windows.Forms.OpenFileDialog
$ofd.InitialDirectory = "d:\scripts"
$ofd.ShowHelp=$true
if($ofd.ShowDialog() -eq "OK") { $ofd.FileName }

} #End Function


#Call the Function
GenerateForm
于 2013-03-26T00:57:45.713 回答