无法处理来自窗口窗体的内容的电源外壳功能


function CalendarShare {
    Add-MailboxFolderPermission -Identity ${FromUser.Text} -AccessRights Editor -User ${ToUser.Text}
}

当程序运行时,它会一直工作,直到它处理共享日历按钮。它指出

无法绑定参数参数标识,因为它为 null。

我不知道是什么原因造成的

完整代码:

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

相关内容

  • 没有找到相关文章

最新更新