winforms - 无法使用 Windows 窗体中的内容处理 powershell 功能
问题描述
function CalendarShare {
Add-MailboxFolderPermission -Identity ${FromUser.Text} -AccessRights Editor -User ${ToUser.Text}
}
当程序运行时,它会一直工作,直到它处理共享日历按钮。它指出它
无法绑定自变量参数标识,因为它为空。
我不知道是什么原因造成的
完整代码:
Add-Type -AssemblyNAme System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#Declare Functions
function login {
$LiveCred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
Import-PSSession $Session
$btnlogin.Visible = $False
}
function quitprogram {
$Form.Close()
Exit-PSSession []
}
function CalendarShare {
Add-MailboxFolderPermission -Identity ${FromUser.Text} -AccessRights Editor -User ${ToUser.Text}
}
#Creates base form
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '400,400'
$Form.Text = 'Powershell GUI'
$Form.TopMost = $False
#Creates Login button
$btnlogin = New-Object System.windows.Forms.button
$btnlogin.text = 'Login'
$btnlogin.width = 80
$btnlogin.height = 40
$btnlogin.location = New-Object System.Drawing.Point(150,5)
#Add_Click defines what to do on click
$btnlogin.Add_Click( {
login #function previously defined. Line 6
})
#Creates label for From user
$FromUserLabel = New-Object system.Windows.Forms.Label
$FromUserLabel.text = 'Share Calendar From User'
$FromUserLabel.AutoSize = $true
$FromUserLabel.width = 25
$FromUserLabel.height = 10
$FromUserLabel.location = New-Object System.Drawing.Point(130,100) #Define where it appears on the map
#Create first input. From User
$FromUser = New-Object system.Windows.Forms.TextBox
$FromUser.multiline = $False
$FromUser.width - 200
$FromUser.height = 20
$FromUser.location = New-Object System.Drawing.Point(135,125)
#Create label for To user
$ToUserLabel = New-Object system.Windows.Forms.Label
$ToUserLabel.text = 'Share Calendar To User'
$ToUserLabel.AutoSize = $True
$ToUserLabel.width = 25
$ToUserLabel.height = 10
$ToUserLabel.location = New-Object System.Drawing.Point(135,175)
#Create second input. To User
$ToUser = New-Object system.Windows.Forms.TextBox
$ToUser.multiline = $False
$ToUser.width - 200
$ToUser.height = 20
$ToUser.location = New-Object System.Drawing.Point(135,200)
#Create Share Button Calendar
$btnsharecalendar = New-Object System.windows.Forms.button
$btnsharecalendar.text = 'Share Calendar'
$btnsharecalendar.width = 165
$btnsharecalendar.height = 30
$btnsharecalendar.location = New-Object System.Drawing.Point(105,275)
#Define Add_Click below
$btnsharecalendar.Add_Click({
CalendarShare
})
#Create button to close program
$btnquit = New-Object System.windows.Forms.button
$btnquit.text = 'Quit'
$btnquit.width = 80
$btnquit.height = 40
$btnquit.location = New-Object System.Drawing.Point(150,325)
#Add_Click defines what to do on click
$btnquit.Add_Click( {
quitprogram #function previously defined. Line 12
})
#Allows form to work
$Form.Controls.AddRange(@($btnlogin,$FromUserLabel,$FromUser,$ToUserLabel,$ToUser,$btnsharecalendar,$btnquit))
$Form.ShowDialog()
解决方案
注意:这是假设 $FromUser 和 $ToUser 都是文本框
您没有正确使用您的变量作为标识参数,应该是:
function CalendarShare {
Add-MailboxFolderPermission -Identity $FromUser.Text -AccessRights Editor -User $ToUser.Text}
}
推荐阅读
- pandas - 按以某个 str 开头的元素列表切片行 df
- arrays - BigQuery - 分组并使用数组字段作为过滤器
- windows-7 - 迁移到 AdoptOpenJDK 11 后 Windows 7 中的像素化 Java GUI
- jmeter - JMeter 综合报告生成不正确的数据
- python - 如何实现与列表搜索相反的字典/集合,以提高速度
- django - 具有两个 ForeignKey 关系的 CreateView 时出现 KeyError
- javascript - 无法从内存中删除已创建但从未附加到页面的画布元素
- list - 列表中的数字大于平均值
- node.js - 如何使用 Express 和 Puppeteer 对每个动态加载的页面进行截图?
- c# - 如果用户输入字符串而不是 int,我如何弹出错误消息