更改 O365 脚本以通过用户输入提示更改职位?

Change O365 script to change job title with user prompts for input?

我制作了一个脚本来更改 Office 365 用户的工作 title.However 我想要它,所以有两个用户提示框要求输入电子邮件地址,然后输入新的职位名称,而不是我更改用户每次都使用电子邮件和标题。

$user = Read-Host -Prompt 'Input the users email address'
$job = Read-Host -Prompt 'Enter the new Job Title'
Set-MsolUser -UserPrincipalName $user  -Title $job

这个不算太难吧!

这是一个小帮助函数,您可以使用它在 PowerShell 中创建世界上最基本的文本输入 GUI,并使用参数来自定义显示给用户的标题和消息。

Function Get-TextInput {
Param($Description='Please enter the information in the space below:',$Title="Data Entry Form")
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing

    $form = New-Object System.Windows.Forms.Form
    $form.Text = $Title
    $form.Size = New-Object System.Drawing.Size(300,200)
    $form.StartPosition = 'CenterScreen'

    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Point(75,120)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = 'OK'
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $form.AcceptButton = $OKButton
    $form.Controls.Add($OKButton)

    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Point(150,120)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text = 'Cancel'
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $form.CancelButton = $CancelButton
    $form.Controls.Add($CancelButton)

    $label = New-Object System.Windows.Forms.Label
    $label.Location = New-Object System.Drawing.Point(10,20)
    $label.Size = New-Object System.Drawing.Size(280,20)
    $label.Text = $Description
    $form.Controls.Add($label)

    $textBox = New-Object System.Windows.Forms.TextBox
    $textBox.Location = New-Object System.Drawing.Point(10,40)
    $textBox.Size = New-Object System.Drawing.Size(260,20)
    $form.Controls.Add($textBox)

    $form.Topmost = $true

    $form.Add_Shown({$textBox.Select()})
    $result = $form.ShowDialog()

    if ($result -eq [System.Windows.Forms.DialogResult]::OK)
    {
        $x = $textBox.Text
        $x
    }
}

给你一个 UI 这样的:

以下是您的使用方法:

#Include the whole function in the body of the script here
$UserPrincipleName = Get-TextInput -Description "Enter User Name" -Title "MSOL Script"
$UserTitle = Get-TextInput -Description "Enter Title For this position" -Title "MSOL Script"
Set-MsolUser -UserPrincipalName $UserPrincipleName -Title $UserTitle

如果您想知道这是如何制作的,请查看以下两个来源之一:

与往常一样,当使用用户输入时,无论是来自 Read-Host 命令还是使用输入框,您都应该从不 相信它是正确的。

始终对用户输入进行某种检查,例如:

while ($true) {
    Clear-Host
    $upn = Read-Host -Prompt 'Input the users UserPrincipalName. Type Q to quit.'
    if ($upn -eq 'Q') {break}

    # test if we can find a user with the entered UserPrincipalName
    $user = Get-MsolUser -UserPrincipalName $upn
    if ($user) {
        $job = Read-Host -Prompt 'Enter the new Job Title'
        Write-Host "Updating title for user $($user.DisplayName) from '$($user.Title)' to '$job'"
        $user | Set-MsolUser -Title $job
    }
    else {
        Write-Warning "User with UserPrincipleName $upn not found. Please try again."
    }
    Start-Sleep -Seconds 3
}