将凭据存储在按钮上
Store credentials on button
为了让新员工更容易通过 powershell 连接到 Exchange,我正在使用 powershell:winforms.
编写一个快速的小工具
该工具将只包含几个按钮、文本字段和信息文本,因此用户可以通过一个 UI.
管理 Exchange envoirement
#1 Set Credentials Button
$Set_Credential = New-Object System.Windows.Forms.Button
$Set_Credential.Top = "5"
$Set_Credential.Left = "5"
$Set_Credential.Anchor = "Left,Top"
$Set_Credential.Text = 'Set Credentials'
$Set_Credential.add_Click({
$UserCredential = Get-Credential
})
$Exchange_Manager.Controls.Add($Set_Credential)
#3 Connect to Exchange Online
$Exchange_Connect = New-Object System.Windows.Forms.Button
$Exchange_Connect.Top = "5"
$Exchange_Connect.Left = "150"
$Exchange_Connect.Anchor ="Left,Top"
$Exchange_Connect.Text = 'Connect'
$Exchange_Connect.add_Click({
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential "$UserCredential" -Authentication Basic -AllowRedirection
Import-PSSession $Session
})
$Exchange_Manager.Controls.Add($Exchange_Connect)
如您所见,第一个 'Set Credentials' 按钮允许用户通过默认的 get-credentials 输入凭据。
第二个按钮 'Connect to Exchange' 使用 $UserCredentials 登录新的 PSSession。
我面临的问题是,当按下连接按钮时,我收到一条错误消息,指出 -credential 值为 Null。
查询 $UserCredential 也会得到一个空行。
似乎虽然输入了凭据,但它们并没有被存储,因为它们是通过按钮功能调用的。
我试着查了一下,但大多数答案都很模糊。
完整代码:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$Exchange_Manager = New-Object Windows.Forms.Form
#Session Initation 1
#1 Set Credentials Button
$Set_Credential = New-Object System.Windows.Forms.Button
$Set_Credential.Top = "5"
$Set_Credential.Left = "5"
$Set_Credential.Anchor = "Left,Top"
$Set_Credential.Text = 'Set Credentials'
$Set_Credential.add_Click({
$UserCredential = Get-Credential
})
$Exchange_Manager.Controls.Add($Set_Credential)
#2 Current Credentials
#3 Connect to Exchange Online
$Exchange_Connect = New-Object System.Windows.Forms.Button
$Exchange_Connect.Top = "5"
$Exchange_Connect.Left = "150"
$Exchange_Connect.Anchor ="Left,Top"
$Exchange_Connect.Text = 'Connect'
$Exchange_Connect.add_Click({
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential "$UserCredential" -Authentication Basic -AllowRedirection
Import-PSSession $Session
})
$Exchange_Manager.Controls.Add($Exchange_Connect)
#Information
#1 Set Credentials Button
$Mailboxes = New-Object System.Windows.Forms.Button
$Mailboxes.Top = "50"
$Mailboxes.Left = "5"
$Mailboxes.Anchor = "Left,Top"
$Mailboxes.Text = 'Get All Mailboxes'
$Mailboxes.add_Click({
Get-Mailbox |Select-Object DisplayName, Alias | Sort-Object -Property DisplayName | Out-gridview
})
$Exchange_Manager.Controls.Add($Mailboxes)
$Exchange_Manager.ShowDialog()
您需要将 $UserCredential
设为全局。
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$Exchange_Manager = New-Object Windows.Forms.Form
$Exchange_Manager.TopMost = $true
# create a global empty credential object
$Global:UserCredential = [System.Management.Automation.PSCredential]::Empty
#Session Initation 1
#1 Set Credentials Button
$Set_Credential = New-Object System.Windows.Forms.Button
$Set_Credential.Top = "5"
$Set_Credential.Left = "5"
$Set_Credential.Anchor = "Left,Top"
$Set_Credential.Text = 'Set Credentials'
$Set_Credential.add_Click({
$Global:UserCredential = Get-Credential
})
$Exchange_Manager.Controls.Add($Set_Credential)
#2 Current Credentials
#3 Connect to Exchange Online
$Exchange_Connect = New-Object System.Windows.Forms.Button
$Exchange_Connect.Top = "5"
$Exchange_Connect.Left = "150"
$Exchange_Connect.Anchor ="Left,Top"
$Exchange_Connect.Text = 'Connect'
$Exchange_Connect.add_Click({
if ($Global:UserCredential -ne [System.Management.Automation.PSCredential]::Empty) {
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential "$Global:UserCredential" -Authentication Basic -AllowRedirection
Import-PSSession $Session
}
})
$Exchange_Manager.Controls.Add($Exchange_Connect)
#4 Show the dialog and dispose of it afterwards
$Exchange_Manager.ShowDialog()
$Exchange_Manager.Dispose()
为了让新员工更容易通过 powershell 连接到 Exchange,我正在使用 powershell:winforms.
编写一个快速的小工具该工具将只包含几个按钮、文本字段和信息文本,因此用户可以通过一个 UI.
管理 Exchange envoirement#1 Set Credentials Button
$Set_Credential = New-Object System.Windows.Forms.Button
$Set_Credential.Top = "5"
$Set_Credential.Left = "5"
$Set_Credential.Anchor = "Left,Top"
$Set_Credential.Text = 'Set Credentials'
$Set_Credential.add_Click({
$UserCredential = Get-Credential
})
$Exchange_Manager.Controls.Add($Set_Credential)
#3 Connect to Exchange Online
$Exchange_Connect = New-Object System.Windows.Forms.Button
$Exchange_Connect.Top = "5"
$Exchange_Connect.Left = "150"
$Exchange_Connect.Anchor ="Left,Top"
$Exchange_Connect.Text = 'Connect'
$Exchange_Connect.add_Click({
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential "$UserCredential" -Authentication Basic -AllowRedirection
Import-PSSession $Session
})
$Exchange_Manager.Controls.Add($Exchange_Connect)
如您所见,第一个 'Set Credentials' 按钮允许用户通过默认的 get-credentials 输入凭据。
第二个按钮 'Connect to Exchange' 使用 $UserCredentials 登录新的 PSSession。
我面临的问题是,当按下连接按钮时,我收到一条错误消息,指出 -credential 值为 Null。
查询 $UserCredential 也会得到一个空行。
似乎虽然输入了凭据,但它们并没有被存储,因为它们是通过按钮功能调用的。
我试着查了一下,但大多数答案都很模糊。
完整代码:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$Exchange_Manager = New-Object Windows.Forms.Form
#Session Initation 1
#1 Set Credentials Button
$Set_Credential = New-Object System.Windows.Forms.Button
$Set_Credential.Top = "5"
$Set_Credential.Left = "5"
$Set_Credential.Anchor = "Left,Top"
$Set_Credential.Text = 'Set Credentials'
$Set_Credential.add_Click({
$UserCredential = Get-Credential
})
$Exchange_Manager.Controls.Add($Set_Credential)
#2 Current Credentials
#3 Connect to Exchange Online
$Exchange_Connect = New-Object System.Windows.Forms.Button
$Exchange_Connect.Top = "5"
$Exchange_Connect.Left = "150"
$Exchange_Connect.Anchor ="Left,Top"
$Exchange_Connect.Text = 'Connect'
$Exchange_Connect.add_Click({
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential "$UserCredential" -Authentication Basic -AllowRedirection
Import-PSSession $Session
})
$Exchange_Manager.Controls.Add($Exchange_Connect)
#Information
#1 Set Credentials Button
$Mailboxes = New-Object System.Windows.Forms.Button
$Mailboxes.Top = "50"
$Mailboxes.Left = "5"
$Mailboxes.Anchor = "Left,Top"
$Mailboxes.Text = 'Get All Mailboxes'
$Mailboxes.add_Click({
Get-Mailbox |Select-Object DisplayName, Alias | Sort-Object -Property DisplayName | Out-gridview
})
$Exchange_Manager.Controls.Add($Mailboxes)
$Exchange_Manager.ShowDialog()
您需要将 $UserCredential
设为全局。
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$Exchange_Manager = New-Object Windows.Forms.Form
$Exchange_Manager.TopMost = $true
# create a global empty credential object
$Global:UserCredential = [System.Management.Automation.PSCredential]::Empty
#Session Initation 1
#1 Set Credentials Button
$Set_Credential = New-Object System.Windows.Forms.Button
$Set_Credential.Top = "5"
$Set_Credential.Left = "5"
$Set_Credential.Anchor = "Left,Top"
$Set_Credential.Text = 'Set Credentials'
$Set_Credential.add_Click({
$Global:UserCredential = Get-Credential
})
$Exchange_Manager.Controls.Add($Set_Credential)
#2 Current Credentials
#3 Connect to Exchange Online
$Exchange_Connect = New-Object System.Windows.Forms.Button
$Exchange_Connect.Top = "5"
$Exchange_Connect.Left = "150"
$Exchange_Connect.Anchor ="Left,Top"
$Exchange_Connect.Text = 'Connect'
$Exchange_Connect.add_Click({
if ($Global:UserCredential -ne [System.Management.Automation.PSCredential]::Empty) {
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential "$Global:UserCredential" -Authentication Basic -AllowRedirection
Import-PSSession $Session
}
})
$Exchange_Manager.Controls.Add($Exchange_Connect)
#4 Show the dialog and dispose of it afterwards
$Exchange_Manager.ShowDialog()
$Exchange_Manager.Dispose()