2017-10-10 70 views
0

我不想爲每個終止的用戶運行腳本,我假設我需要使用do while循環,因此它會一直循環,直到ESC選項。終止用戶腳本 - 使單個腳本適合多個用戶運行?

我一直在試圖在PowerShell中做到這一點,但我是新手,並且仍在學習。你有什麼主意嗎?

$wshell = New-Object -ComObject Wscript.Shell 
$wshell.Popup("Please enter in your Domain Admin credentials. Please remember it should be in the form of DOMAIN\username.",0,"Credentials Needed!",0x0) 
$creds = Get-Credential 
$PSDefaultParameterValues = @{"*-AD*:Credential"=$creds} 

#Here we create the connection to the exchange server. Edit with your mailserver info 
$ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://**editmemithyourwebmailservername**/PowerShell 
Import-PSSession $ExchangeSession 

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
[void] [System.Windows.Forms.Application]::EnableVisualStyles() 

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Terminated Employee Process Form" 
$objForm.Size = New-Object System.Drawing.Size(500,400) 
$objForm.StartPosition = "CenterScreen" 
$objForm.MaximizeBox = $False 


$objForm.KeyPreview = $True 
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
    {$userinput=$UserTextBox.Text;$forwardemail=$ForwardingTextBox.Text;$ticketnumber=$TicketTextBox.Text;$disableuser=$DisableUserCheckbox.Checked;$objForm.Close()}}) 
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
    {$objForm.Close()}}) 

$Font = New-Object System.Drawing.Font("Verdana",8,[System.Drawing.FontStyle]::Bold) 
#$objForm.Font = $Font 
#VERSION NUMBER 
$VersionLabel = New-Object System.Windows.Forms.Label 
$VersionLabel.Location = New-Object System.Drawing.Size(450,10) 
$VersionLabel.Size = New-Object System.Drawing.Size(120,20) 
$VersionLabel.Font = $Font 
$VersionLabel.Text = "V1" 
$objForm.Controls.Add($VersionLabel) 

#OK AND CANCEL BUTTONS 
$OKButton = New-Object System.Windows.Forms.Button 
$OKButton.Location = New-Object System.Drawing.Size(75,320) 
$OKButton.Size = New-Object System.Drawing.Size(75,23) 
$OKButton.Text = "OK" 
$OKButton.Add_Click({$userinput=$UserTextBox.Text;$ticketnumber=$TicketTextBox.Text;$forwardemail=$ForwardingTextBox.Text;$disableuser=$DisableUserCheckbox.Checked;$objForm.Close()}) 
$objForm.Controls.Add($OKButton) 


#USERNAME LABEL 
$UserLabel = New-Object System.Windows.Forms.Label 
$UserLabel.Location = New-Object System.Drawing.Size(10,20) 
$UserLabel.Size = New-Object System.Drawing.Size(280,20) 
$UserLabel.Text = "Username of Terminated Employee" 
$objForm.Controls.Add($UserLabel) 
#USERNAME TEXT BOX 
$UserTextBox = New-Object System.Windows.Forms.TextBox 
$UserTextBox.Location = New-Object System.Drawing.Size(10,40) 
$UserTextBox.Size = New-Object System.Drawing.Size(180,20) 
$objForm.Controls.Add($UserTextBox) 

#DISABLE USER CHECKBOX CONTROL 
$DisableUserCheckbox = New-Object System.Windows.Forms.Checkbox 
$DisableUserCheckbox.Location = New-Object System.Drawing.Size(220,30) 
$DisableUserCheckbox.Size = New-Object System.Drawing.Size(120,40) 
$DisableUserCheckbox.Text = "Disable The User?" 
$objForm.Controls.Add($DisableUserCheckbox) 

#FORWARD EMAIL LABEL 
$FowardEmailLabel = New-Object System.Windows.Forms.Label 
$FowardEmailLabel.Location = New-Object System.Drawing.Size(10,80) 
$FowardEmailLabel.Size = New-Object System.Drawing.Size(280,20) 
$FowardEmailLabel.Text = "Forward Email to Manager? If Yes, Type In Email Address" 
$objForm.Controls.Add($FowardEmailLabel) 

#FORWARD EMAIL TEXT BOX 
$ForwardingTextBox = New-Object System.Windows.Forms.TextBox 
$ForwardingTextBox.Location = New-Object System.Drawing.Size(10,100) 
$ForwardingTextBox.Size = New-Object System.Drawing.Size(180,40) 
$objForm.Controls.Add($ForwardingTextBox) 

#ENTER TICKET NUMBER TEXT LABEL 
$TicketLabel = New-Object System.Windows.Forms.Label 
$TicketLabel.Location = New-Object System.Drawing.Size(10,150) 
$TicketLabel.Size = New-Object System.Drawing.Size(80,20) 
$TicketLabel.Text = "Issue Number" 
$objForm.Controls.Add($TicketLabel) 

$TicketTextBox = New-Object System.Windows.Forms.TextBox 
$TicketTextBox.Location = New-Object System.Drawing.Size(10,170) 
$TicketTextBox.Size = New-Object System.Drawing.Size(40,250) 
$objForm.Controls.Add($TicketTextBox) 


#CANCEL BUTTONS 
$CancelButton = New-Object System.Windows.Forms.Button 
$CancelButton.Location = New-Object System.Drawing.Size(350,320) 
$CancelButton.Size = New-Object System.Drawing.Size(75,23) 
$CancelButton.Text = "Cancel" 
$CancelButton.Add_Click({$objForm.Close(); $cancel = $true}) 
$objForm.Controls.Add($CancelButton) 


$objForm.Topmost = $True 
$objForm.Add_Shown({$objForm.Activate()}) 
[void] $objForm.ShowDialog() 
if ($cancel) {return} 
#$OKButton.Add_Click({$userinput=$UserTextBox.Text;$ticketnumber=$TicketTextBox.Text;$forwardemail=$ForwardingTextBox.Text;$disableuser=$DisableUserCheckbox.Checked;$objForm.Close()}) 
#$CancelButton.Add_Click({$objForm.Close()}) 

#COMMON GLOBAL VARIABLES 
$disableusercheckbox=$DisableUserCheckbox.Checked 
$userinput=$UserTextBox.Text 
$forwardemail=$ForwardingTextBox.Text 
$ticketnumber=$TicketTextBox.Text 

$Month = Get-Date -format MM 
$Day = Get-Date -format dd 
$Year = Get-Date -format yyyy 




If ($OKButton.Add_Click) { 


######## 
#ACTIVE DIRECTORY ACTIONS 
######### 

#DISABLE THE USER 
If ($disableusercheckbox -eq $true) 
{ 
    Disable-ADAccount -Identity $userinput 
    $disabled = $userinput + " has been disabled" 
} else { 
    $notdisabled = $userinput + " has not been disabled at this time" 
} 

#GETS ALL GROUPS USER WAS PART OF BEFORE BLOWING THEM OUT 
    $User = $userinput 
    [email protected]() 
    $Groups = Get-ADUser -Identity $User -Properties * | select -ExpandProperty memberof 
    foreach($i in $Groups){ 
    $i = ($i -split ',')[0] 
    $List += "`r`n" + ($i -creplace 'CN=|}','') 
    } 

#BLOW OUT GROUPS OF USER EXCEPT DOMAIN USERS 
(get-aduser $userinput -properties memberof).memberof|remove-adgroupmember -member $userinput -Confirm:$False 

#SETS THE USERS TITLE,COMPANY/MANAGER TO DISABLED 
set-aduser -identity $userinput -title "CompanyName - Disabled $Month/$Day/$Year" 
set-aduser -identity $userinput -company $null 
set-aduser -identity $userinput -manager $null 
set-aduser -identity $userinput -department $null 
set-aduser -identity $userinput -description "CompanyName - Disabled $Month/$Day/$Year per Issue# $ticketnumber" 

#CHANGES THE USERS PASSWORD 
$newpwd = ConvertTo-SecureString -String "[email protected]" -AsPlainText –Force 
Set-ADAccountPassword $userinput –NewPassword $newpwd -Reset 

#MOVES THE USER TO DISABLED USERS 
Get-ADUser -Filter { samAccountName -like $userinput } | Move-ADObject –TargetPath "OU=Disabled Users,OU=User Accounts,DC=domain,DC=com" 


#HIDES USER FROM GLOBAL ADDRESS BOOK and configures forwarding 
Set-Mailbox -Identity $userinput -ForwardingAddress $forwardemail -HiddenFromAddressListsEnabled $true 

#REMOVES THE SESSION 
Remove-PSsession $ExchangeSession 

Start-Sleep -s 2 

} 
+0

會有幾種方法做到這一點,但沒有一個簡單的初學者。使用窗體,您可以填充ListView以供選擇,或者像現在一樣,但有一個添加按鈕可添加到列表中。如果你想爲了簡單而直覺地將視覺直接拋出窗外,你可以使用多行文本框和逗號來分隔參數。正如Robin所說,你也可以完全丟棄表格並使用電子表格,甚至可以通過命令行輸入。無論如何,除了「AD動作」代碼之外,它需要全部重寫。 –

+0

謝謝。那麼我們如何使用多行文本框來做到這一點?請澄清我。 – Arbelac

+0

這是一個我不太願意花時間的答案。不過,我可以指出你在這個在PowerShell中使用WPF表單的非常酷的教程,它應該指向正確的方向https://foxdeploy.com/2015/04/16/part-ii-deploying-powershell-guis-in-分鐘-使用視覺工作室/ –

回答

0

表單文本框都將是棘手的處理多個用戶。相反,使用CSV文件作爲輸入文件,用戶名和ticketnumber作爲標題。

$CSVList = Import-csv C:\Temp\InputFile.csv

通過在CSV每行使用foreach循環來循環。

Foreach ($Line.Username in $CSVList) 
{ 
    #your termination code goes here 
    #if at any point you need to use the ticket number for this user 

    $TicketNo = $Line.TicketNumber 
    #This is the Header you used in the csv file. 
} 

用於顯示,創建一個對象表(很像一個CSV文件),並使用Datagridview或更簡單Out-Gridview,而不是標籤,所以你可以支持顯示多用戶顯示它。 (Out-Gridview將創建自己的窗口雖然)