简体   繁体   English

Powershell GUI-复制按钮

[英]Powershell GUI - Button To Copy

I have a Powershell GUI Script, and the final thing I need to do is add a button to copy user credentials to the clipboard. 我有一个Powershell GUI脚本,最后要做的就是添加一个按钮,将用户凭据复制到剪贴板。

Below, is the relevant code. 以下是相关代码。

$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}

It then copies to the clipboard as 然后将其复制到剪贴板

 Display Name:
 Username:
 Email:
 Password:

No Data is actually grabbed for some reason. 由于某种原因,实际上没有获取任何数据。

It should look like the following 它看起来应该如下所示

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

Hopefully somebody has an idea 希望有人有一个主意

Was asked to clarify the parameters / variables so below i will do so 被要求澄清参数/变量,所以下面我会这样做

$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

Include 包括

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

in your click event/function: 在您的点击事件/功能中:

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

I figured this out now. 我现在想通了。

Turns out that I did a stupid. 原来我做的很蠢。

Since i'm working with functions, i forgot that i had to define a global variable. 由于我正在使用函数,因此我忘记了必须定义一个全局变量。

I declared these, and now it works perfectly 我声明了这些,现在它可以正常工作了

So the issue is how u declared the variables based on how you pasted them. 因此,问题在于您如何根据粘贴方式声明变量。 In powershell things are read from top to bottom. 在Powershell中,内容是从上到下阅读的。

Here is a prime example of out of order variables calling 这是乱序变量调用的主要示例

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

Why this will not work. 为什么这行不通。 $DisplayName will not equal anything but the " " becuase you have not declared what $UCFirstNameParam and $UCSurNameParam equal. $ DisplayName不会等于任何东西,但“”是因为您尚未声明$ UCFirstNameParam和$ UCSurNameParam相等。

The fix would be to create the variables when they are assigned the value 解决方法是在为变量分配值时创建变量

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM