【问题标题】:Script to update AD description and computer description用于更新广告描述和计算机描述的脚本
【发布时间】:2020-05-22 00:49:55
【问题描述】:

我想要创建的是一个带有 GUI 的 powershell 脚本,它将更新计算机描述并镜像 AD 中的描述。例如,如果我要输入一个计算机名称,如“PC-Hallway”并将其描述更新为“位置走廊”,那么信息将填充在 AD 和 PC 描述字段中。我有一些非常接近我想要附加的东西,但无法将输入到字段中的信息传递给“set-adcomputer”或“-description”。

function button ($title,$mailbx, $WF, $TF) {


[void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
[void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)

$form = New-Object “System.Windows.Forms.Form”;
$form.Width = 500;
$form.Height = 150;
$form.Text = $title;
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;

$textLabel1 = New-Object “System.Windows.Forms.Label”;
$textLabel1.Left = 25;
$textLabel1.Top = 15;

$textLabel1.Text = $mailbx;



$textLabel2 = New-Object “System.Windows.Forms.Label”;
$textLabel2.Left = 25;
$textLabel2.Top = 50;

$textLabel2.Text = $WF;



$textLabel3 = New-Object “System.Windows.Forms.Label”;
$textLabel3.Left = 25;
$textLabel3.Top = 85;

$textLabel3.Text = $TF;

$textBox1 = New-Object “System.Windows.Forms.TextBox”;
$textBox1.Left = 150;
$textBox1.Top = 10;
$textBox1.width = 200;


$textBox2 = New-Object “System.Windows.Forms.TextBox”;
$textBox2.Left = 150;
$textBox2.Top = 50;
$textBox2.width = 200;


$button = New-Object “System.Windows.Forms.Button”;
$button.Left = 360;
$button.Top = 85;
$button.Width = 100;
$button.Text = “Ok”;


$eventHandler = [System.EventHandler]{
$textBox1.Text;
$textBox2.Text;
$textBox3.Text;
$form.Close();};

$button.Add_Click($eventHandler) ;

$form.Controls.Add($button);
$form.Controls.Add($textLabel1);
$form.Controls.Add($textLabel2);
$form.Controls.Add($textLabel3);
$form.Controls.Add($textBox1);
$form.Controls.Add($textBox2);
$ret = $form.ShowDialog();


return $textBox1.Text, $textBox2.txt
}

$return= button “Computer info” “Enter Asset info” “Asset location”


$return[0]
$return[1]
$return[2]

set-adcomputer $return[1] -description $return[2]
$mydescription = $return[2]
Invoke-Command -ComputerName $return[1] -ScriptBlock {$OSWMI=getwmiobject -class win32_operatingsystem;$OSWMI.description=$args[0];$OSWMI.put() } -ArgumentList($mydescription)

【问题讨论】:

  • 这和批处理文件有什么关系?
  • 你为什么要为这个简单的任务编写一个 GUI?即使你想使用 GUI,你也可以避免所有的代码,并使用 Powershell 帮助系统来创建一个简单的 GUI。 PoorMan's GUI。您的代码已损坏,您有未使用的元素等,您在变量中捕获信息并将其传递。

标签: powershell


【解决方案1】:

这似乎表明您是 GUI 设计的新手。这更好,但实际上你应该接受一些关于这个主题的培训。 Youtube is your friend.

您从表单中收集信息...

Import-Module ActiveDirectory
$ComputerName = 'SomeCurrentHostName'
$Location     = 'NA/HQ/Building A'
$Description  = 'I am a Windows 10 Computer'
Set-ADComputer -Identity $ComputerName -Location $Location  -Description $Description

并将其传递给按钮的单击事件。

更谨慎的做法是创建一个函数进行测试,对其进行全面测试,然后将该函数分配给按钮单击事件。这样,您的 GUI 代码和代码隐藏是分开的,并且是隐藏的代码。

例如,对您展示的内容进行重构,如下所示。请注意,我只是将它作为演示输出到屏幕,显示从表单中捕获的值。真的没有理由输出到屏幕上。

###############################################################################
#region Begin initialize environment                                          #
###############################################################################

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

###############################################################################
#endregion End initialize environment                                         #
###############################################################################


###############################################################################
#region Begin Code library                                                    #
###############################################################################

Function Update-ComputerDetails
{
    Write-Host $txtNewComputerName.Text
    Write-Host $txtNewComputerLocation.Text
    Write-Host $txtNewComputerDescription.Text

<#
    Import-Module ActiveDirectory

    $paramblock = @{
        Identity    = $txtNewComputerName.Text
        Location    = $txtNewComputerLocation.Text
        Description = $txtNewComputerDescription.Text
    }

    Set-ADComputer @paramblock
#>

}


Function Exit-ComputerDetails
{
    $frmComputerInfo.Close()
}

###############################################################################
#endregion End Code library                                                   #
###############################################################################


###############################################################################
#region Begin GUI code                                                        #
###############################################################################

$frmComputerInfo                       = New-Object system.Windows.Forms.Form
$frmComputerInfo.ClientSize            = '489,157'
$frmComputerInfo.text                  = "Update Active Directory Computer Information"
$frmComputerInfo.TopMost               = $false

$lblNewComputerName                    = New-Object system.Windows.Forms.Label
$lblNewComputerName.text               = "New Computer Name"
$lblNewComputerName.AutoSize           = $true
$lblNewComputerName.width              = 25
$lblNewComputerName.height             = 10
$lblNewComputerName.location           = New-Object System.Drawing.Point(36,34)
$lblNewComputerName.Font               = 'Microsoft Sans Serif,10'

$txtNewComputerName                    = New-Object system.Windows.Forms.TextBox
$txtNewComputerName.multiline          = $false
$txtNewComputerName.width              = 253
$txtNewComputerName.height             = 20
$txtNewComputerName.location           = New-Object System.Drawing.Point(189,29)
$txtNewComputerName.Font               = 'Microsoft Sans Serif,10'

$lblNewComputerLocation                = New-Object system.Windows.Forms.Label
$lblNewComputerLocation.text           = "New Computer Location"
$lblNewComputerLocation.AutoSize       = $true
$lblNewComputerLocation.width          = 25
$lblNewComputerLocation.height         = 10
$lblNewComputerLocation.location       = New-Object System.Drawing.Point(36,58)
$lblNewComputerLocation.Font           = 'Microsoft Sans Serif,10'

$txtNewComputerLocation                = New-Object system.Windows.Forms.TextBox
$txtNewComputerLocation.multiline      = $false
$txtNewComputerLocation.width          = 252
$txtNewComputerLocation.height         = 20
$txtNewComputerLocation.location       = New-Object System.Drawing.Point(189,55)
$txtNewComputerLocation.Font           = 'Microsoft Sans Serif,10'

$lblNewComputerDescription             = New-Object system.Windows.Forms.Label
$lblNewComputerDescription.text        = "New Description"
$lblNewComputerDescription.AutoSize    = $true
$lblNewComputerDescription.width       = 25
$lblNewComputerDescription.height      = 10
$lblNewComputerDescription.location    = New-Object System.Drawing.Point(36,85)
$lblNewComputerDescription.Font        = 'Microsoft Sans Serif,10'

$txtNewComputerDescription             = New-Object system.Windows.Forms.TextBox
$txtNewComputerDescription.multiline   = $false
$txtNewComputerDescription.width       = 252
$txtNewComputerDescription.height      = 20
$txtNewComputerDescription.location    = New-Object System.Drawing.Point(189,79)
$txtNewComputerDescription.Font        = 'Microsoft Sans Serif,10'

$btnOK                                 = New-Object system.Windows.Forms.Button
$btnOK.text                            = "OK"
$btnOK.width                           = 60
$btnOK.height                          = 30
$btnOK.location                        = New-Object System.Drawing.Point(306,107)
$btnOK.Font                            = 'Microsoft Sans Serif,10'

$btnCancel                             = New-Object system.Windows.Forms.Button
$btnCancel.text                        = "Cancel"
$btnCancel.width                       = 60
$btnCancel.height                      = 30
$btnCancel.location                    = New-Object System.Drawing.Point(379,107)
$btnCancel.Font                        = 'Microsoft Sans Serif,10'

$frmComputerInfo.controls.AddRange(@(
    $lblNewComputerName,
    $txtNewComputerName,
    $lblNewComputerLocation,
    $txtNewComputerLocation,
    $lblNewComputerDescription,
    $txtNewComputerDescription,
    $btnOK,
    $btnCancel
    )
)

###############################################################################
#endregion End GUI code                                                       #
###############################################################################


###############################################################################
#region Begin logic execution code                                            #
###############################################################################

$btnOK.Add_Click({ Update-ComputerDetails })
$btnCancel.Add_Click({ Exit-ComputerDetails })
[void]$frmComputerInfo.ShowDialog()

###############################################################################
#endregion End Logic execution code                                           #
###############################################################################

或者这样更直接和简化:

# Custom Input Box

#region Begin environment initialization
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
#endregion

#region Begin GUI code
$form                      = New-Object -TypeName System.Windows.Forms.Form
$form.Text                 = 'Data Entry Form'
$form.Size                 = New-Object -TypeName System.Drawing.Size(300, 200)
$form.StartPosition        = 'CenterScreen'

$label                     = New-Object -TypeName System.Windows.Forms.Label
$label.Location            = New-Object -TypeName System.Drawing.Size(10, 20)
$label.Size                = New-Object -TypeName System.Drawing.Size(280, 25)
$label.Text                = 'Please enter the computername, location name, and description information in the spaces below'

$textBox1                  = New-Object -TypeName System.Windows.Forms.TextBox
$textBox1.Location         = New-Object -TypeName System.Drawing.Size(13, 50)
$textBox1.Size             = New-Object -TypeName System.Drawing.Size(260, 20)

$textBox2                  = New-Object -TypeName System.Windows.Forms.TextBox
$textBox2.Location         = New-Object -TypeName System.Drawing.Size(13, 70)
$textBox2.Size             = New-Object -TypeName System.Drawing.Size(260, 20)

$textBox3                  = New-Object -TypeName System.Windows.Forms.TextBox
$textBox3.Location         = New-Object -TypeName System.Drawing.Size(13, 90)
$textBox3.Size             = New-Object -TypeName System.Drawing.Size(260, 20)

$OkButton                  = New-Object -TypeName System.Windows.Forms.Button
$OkButton.Location         = New-Object -TypeName System.Drawing.Size(75, 120)
$OkButton.Size             = New-Object -TypeName System.Drawing.Size(75, 23)
$OkButton.Text             = 'OK'
$OkButton.DialogResult     = [System.Windows.Forms.DialogResult]::OK

$CancelButton              = New-Object -TypeName System.Windows.Forms.Button
$CancelButton.Location     = New-Object -TypeName System.Drawing.Size(150, 120)
$CancelButton.Size         = New-Object -TypeName System.Drawing.Size(75, 23)
$CancelButton.Text         = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel

$form.Controls.AddRange(@(
        $label,
        $textBox1,
        $textBox2,
        $textBox3,
        $OkButton,
        $CancelButton
    )
)

$form.add_Shown({$textBox.Select()})
#endregion

# Beging GUI presentation
$form.TopMost      = $true
$form.AcceptButton = $OkButton
$form.CancelButton = $CancelButton
#endregion

#region Begin logic execution
$result = $form.ShowDialog()


If ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $textBox1.Text
    $textBox2.Text
    $textBox3.Text
}
Else{'Cancel was pressed. Application Exited.'}
#endregion

有几个预先构建的脚本可以满足您的需求,而无需 GUI。见这些:

Powershell script to set AD Computer description during OSD --- Download: SetComputerDesc.zip

Update Computer Description–PowerShell --- Download: Update_Comptuer_Description.txt

How to rename a domain computer with Windows PowerShell

更新

顺便说一句,我在评论中谈到的 OGV 是这样的: (不,不是很漂亮 - 但可以使用)。

Start-Process -FilePath (
    Get-ChildItem -Path 'd:\scripts' -Filter '*.ps1' | 
    Sort-Object -Property Name | 
    Out-GridView -Title 'Select a script to run' -OutputMode Single
).FullName -Wait

还有一些方法可以嵌套 OGV 响应以获得结果流。看到这个:

Creating a Simplistic GUI Interface with Out-GridView 和这个... How To Use Out-Gridview As A GUI In PowerShell

【讨论】:

  • 感谢您的建议和信息。对此,我真的非常感激。是的,我对 powershell 比较陌生,我写的大多数 PS 脚本都是因为 youtube 和网络上的其他资源。我想为这个任务(以及未来的其他任务)创建一个 GUI,因为进入我办公室的员工几乎没有 PS 经验。他们可以快速轻松地单击图标更新信息并完成。如果所有这些工作都是我自己完成的,那么我对使用 PS 脚本的默认方式没有任何问题。
  • 不用担心,但是为每个脚本创建 GUI 确实不是您需要做的事情。创建一个脚本库,并利用 Out-GridView 预设一个脚本列表供您的用户选择和运行。因此,一个简单的无需维护的 UI。您在用户桌面上创建一个快捷方式来运行启动脚本并让您的脚本完成它的工作。只需为预期目的命名您的脚本,使用 Get-ChildItem 读取该文件夹并发送到 Out-Grid 视图供他们选择,所有输入都只是控制台提示符。
  • 另外,如果这解决了您的用例,请记住将此标记为您接受的答案,以使其他发现自己有相同需求的人受益。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多