用于更新广告描述和计算机描述的脚本

问题描述 投票:0回答:1

我要创建的是带有GUI的Powershell脚本,该脚本将更新计算机描述并镜像AD中的描述。例如,如果我输入“ PC-Hallway”之类的计算机名称并将其描述更新为“ location 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)
powershell
1个回答
0
投票

这似乎表明您是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

然后将其传递给按钮的click事件。

更明智的做法是创建一个要测试的功能,对其进行完全测试,然后将该功能分配给按钮单击事件。这样,您的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, 20)
$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(10, 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(10, 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(10, 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

© www.soinside.com 2019 - 2024. All rights reserved.