Powershell GUI-复制按钮

时间:2019-01-28 14:36:08

标签: powershell

我有一个Powershell GUI脚本,最后要做的就是添加一个按钮,以将用户凭据复制到剪贴板。

下面是相关代码。

$ResultsCopy                     = New-Object system.Windows.Forms.Button
$ResultsCopy.text                = "Copy"
$ResultsCopy.width               = 185
$ResultsCopy.height              = 29
$ResultsCopy.location            = New-Object System.Drawing.Point(160,349)
$ResultsCopy.Font                = 'Georgia,10'
$ResultsCopy.Add_Click({ ResultsCopy })

Function ResultsCopy
{"
 Display Name: $DisplayName
 Username: $SAMAccountName
 Email: $Mail 
 Password: $passwordvalue" | clip}

然后将其复制为剪贴板

 Display Name:
 Username:
 Email:
 Password:

由于某种原因,实际上没有获取任何数据。

应如下图所示

 Display Name: James Timms
 Username: James.Timms
 Email: James.Timms@Mail.com
 Password: SuperSecurePassword69

希望有人有主意

被要求澄清参数/变量,因此在下面我会这样做

$DisplayName = $UCFirstNameParam + " " + $UCsurNameParam
$Mail = $UCFirstNameParam + "." + $UCsurNameParam + "@" + "annodata.co.uk"
$MailAlias = $UCFirstNameParam + "." + $UCsurNameParam + "@" + $DNSRoot2

$SInitial = $UCsurNameParam[0]
$Initial = $UCFirstNameParam[0]
$SAMAccountName = $UCFirstNameParam + "." + $UCsurNameParam
$SAMAccountLower = $SAMAccountName.ToLower()
$UserPrincipalName = $UCFirstNameParam + "." + $UCsurNameParam

$defaultname = $SAMAccountName
$email = $UCFirstNameParam + "." + $UCsurNameParam
$i = 1

$NewUserParams = @{
path                  = "OU=Bracknell,OU=Sites,DC=annodata,DC=co,DC=uk"
SamAccountName        = $SAMAccountName
Name                  = $SAMAccountName
DisplayName           = $DisplayName
GivenName             = $UCFirstNameParam
Surname               = $UCSurnameParam
EmailAddress          = $Mail
UserPrincipalName     = "$SAMAccountName@annodata.co.uk"
Description           = $UCRoleParam
ChangePasswordAtLogon = $true
PasswordNeverExpires  = $false
AccountPassword       = $password
Enabled               = $true

$UCFirstNameParamLong                 = New-Object System.Windows.Forms.Label
$UCFirstNameParamLong.Text            = $UCFirstName.Text
$UCFirstNameParamLong.Location        = New-Object System.Drawing.Point(180,100)
$UCFirstNameParamLong.AutoSize        = $true
$UCFirstNameParamLong.Font            = 'Georgia,10'

$UCSurnameParamLong                 = New-Object System.Windows.Forms.Label
$UCSurnameParamLong.Text            = $UCSurname.Text
$UCSurnameParamLong.Location        = New-Object System.Drawing.Point(180,150)
$UCSurnameParamLong.AutoSize        = $true
$UCSurnameParamLong.Font            = 'Georgia,10'

$UCRoleParamLong                 = New-Object System.Windows.Forms.Label
$UCRoleParamLong.Text            = $UCRole.Text
$UCRoleParamLong.Location        = New-Object System.Drawing.Point(180,200)
$UCRoleParamLong.AutoSize        = $true
$UCRoleParamLong.Font            = 'Georgia,10'

$UCLocationParamLong                 = New-Object System.Windows.Forms.Label
$UCLocationParamLong.Text            = $UCSiteSelection.Text
$UCLocationParamLong.Location        = New-Object System.Drawing.Point(180,250)
$UCLocationParamLong.AutoSize        = $true
$UCLocationParamLong.Font            = 'Georgia,10'

$UCFirstNameParam = $UCFirstNameParamLong.Text
$UCSurnameParam = $UCSurnameParamLong.Text
$UCRoleParam = $UCRoleParamLong.Text
$UCLocationParam = $UCLocationParamLong.Text

3 个答案:

答案 0 :(得分:0)

包含

$UCFirstNameParam = $UCFirstNameParamLong.Text
$UCSurnameParam = $UCSurnameParamLong.Text
$UCRoleParam = $UCRoleParamLong.Text
$UCLocationParam = $UCLocationParamLong.Text

在您的点击事件/功能中:

Function ResultsCopy
{
    $UCFirstNameParam = $UCFirstNameParamLong.Text
    $UCSurnameParam = $UCSurnameParamLong.Text
    $UCRoleParam = $UCRoleParamLong.Text
    $UCLocationParam = $UCLocationParamLong.Text
     "
     Display Name: $DisplayName
     Username: $SAMAccountName
     Email: $Mail 
     Password: $passwordvalue
     " | clip
}

答案 1 :(得分:0)

我现在想通了。

原来我做的很愚蠢。

由于我正在使用函数,因此我忘记了必须定义一个全局变量。

我声明了这些,现在可以正常使用

答案 2 :(得分:0)

所以问题是您如何根据您粘贴变量的方式声明变量。 在Powershell中,内容是从上到下阅读的。

这是乱序变量调用的主要示例

$DisplayName = $UCFirstNameParam + " " + $UCSurNameParam
$UCFirstNameParam = $UCFirstNameParamLong.Text
$UCSurnameParam = $UCSurnameParamLong.Text

为什么这不起作用。 $ DisplayName不会等于任何东西,但“”是因为您尚未声明$ UCFirstNameParam和$ UCSurNameParam相等。

解决方法是在为变量分配值时创建变量

$UCFirstNameParam = $UCFirstNameParamLong.Text
$UCSurnameParam = $UCSurnameParamLong.Text
$DisplayName = $UCFirstNameParam + " " + $UCSurNameParam