我已经编写了一个脚本来更改Office 365用户的职位名称,但是我想要这样做,所以有两个用户提示框要求输入电子邮件地址然后输入新职位名称,而不是我更改用户的电子邮件和职位每次。
$user = Read-Host -Prompt 'Input the users email address'
$job = Read-Host -Prompt 'Enter the new Job Title'
Set-MsolUser -UserPrincipalName $user -Title $job
答案 0 :(得分:0)
这不太困难!
这是一个小的辅助函数,可用于在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
如果您想知道这是怎么做的,请查看以下两个来源之一:
答案 1 :(得分:0)
与以往一样,无论是通过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
}