为什么Windows Form程序很容易导致PowerShell ISE无响应? 经常导致无响应,必须要强制关掉才行?重新打开后不定时的有无响应。
在Powershell ISE 2.0 上一直都是好的。没有这个问题, 升级Powershell 到了 5.0和5.1的时候一直无响应。
代码如下:
CLS
$OU=’ou=Nankang,dc=cnnk,dc=com’
$GlobalOU=’dc=cnnk,dc=com’
$LocalDC=’CNNK-DC01.cnnk.com’
###########################################################################FORM BEGIN############################################################################
Import-Module ActiveDirectory
$LOGTotalDate = $NULL
$LOGCurrentDate = $NULL
$LOGTotalDate = “{0:yyyy-MM-dd}” -f(Get-Date)
$LOGCurrentDate = “{0:yyyy-MM-dd HH:mm:ss}” -f(Get-Date)
if(Test-Path C:\POWERSHELL\LOG\$LOGTotalDate.log)
{
#Log file exist
}
else
{
#Create the log file
echo “*****************************************START LOG*****************************************” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
echo “Today is $LOGTotalDate, log starts…” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
}
Function Set_Log_Time()
{
echo “$LOGCurrentDate” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
}
$Account_FRM_InPut_Value = $NULL #Get the Form input account information, including the LAN ID, email ,SAP, DisplayName
$LockedOut_FRM_InPut_Value = $NULL
$CHK_LOCKOUT_STATUS = $NULL
$CHK_UNLOCK_ACCOUNT = $NULL
$CHK_RESET_PWD = $NULL
$EmployeeIDabel = $NULL
$SecurityGroup = $NULL
$New_EmployeeID_FRM_Input_Value = $NULL
$Keywords_RAW = $NULL
$checkUser =$NULL
$groupsVV = $NULL
$objUserVV = $NULL
$strSIDVV = $NULL
$resultVV = $NULL
$groupsvvv = $NULL
################################# Check if a user is in a group####################
function get_adgroups($usernameVVV)
{
$groupsvvv = @()
([System.Security.Principal.WindowsIdentity]$usernameVVV).Groups | %{ $Groupsvvv += $_.Value } #Get a LAN ID’s all group SID info
return $groupsvvv
}
#
################################# Check if a user is in a group####################
if(Test-Path C:\POWERSHELL\LOG) #Check if a log folder is exist
{}
else
{mkdir -Path C:\POWERSHELL\LOG}
echo “” > C:\POWERSHELL\LOG\opt.log #Record what is click in the UI, LAN ID/DISPLAY NAME /EMAIL OR SAP
echo “” > C:\POWERSHELL\LOG\Account_FRM_InPut_Value.log #Record the input account informaiton
#echo “”> C:\POWERSHELL\LOG\New_Group_FRM_Input_Value.log
echo “” > C:\POWERSHELL\LOG\NeedUpdateGroup.log #Record if the need update checkbox is checked
echo “” > C:\POWERSHELL\LOG\Keywords.log #Record the search keyword
if(Test-Path C:\POWERSHELL\LOG\New_Group_FRM_Input_Value.log) #To record the group that need add user to
{}
else
{
echo “” > C:\POWERSHELL\LOG\New_Group_FRM_Input_Value.log
}
$GroupString = Get-Content C:\POWERSHELL\LOG\New_Group_FRM_Input_Value.log #Get the group name that need add user to
################Form initial BEGIN ###################
[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)|out-null
[System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)|out-null
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = “EUS Group Search & Permission Toolkit v1.0 “
$objForm.Size = New-Object System.Drawing.Size(800,400)
$objForm.StartPosition = “CenterScreen”
$objForm.KeyPreview = $True
$objForm.MaximizeBox = $false
$SearchGroup = New-Object System.Windows.Forms.textbox #Search Text
$SearchGroup.Location = New-Object System.Drawing.Size(10,10)
$SearchGroup.Size = New-Object System.Drawing.Size(450,20)
$SearchGroup.Text = “”
$SearchGroup.ForeColor = “Blue”
$objForm.Controls.Add($SearchGroup)
$SearchButton = New-Object System.Windows.Forms.Button #Search button
$SearchButton.Location = New-Object System.Drawing.Size(500,10)
$SearchButton.Size = New-Object System.Drawing.Size(205,23)
$SearchButton.Text = “Search Group Descrption”
$SearchButton.ForeColor = “Blue”
$SearchButton.Add_Click(
{
$Keywords_RAW = $SearchGroup.Text.ToString()
&Set_Log_Time
echo “Searching keyword is $Keywords_RAW” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
$Keywords_RAW = $Keywords_RAW.Replace(“\”,”\5c”) #Convert \ to 5c due to the AD search don’t support special character ‘\’
echo $Keywords_RAW > C:\POWERSHELL\LOG\Keywords.log #Record the keyword after converting
$objForm.Close()
#Get-ADGroup -Server $LocalDC -Searchbase $OU -Filter {description -like “*\5ccorp\5cCNKC\5cAPPS_LOB\5cPMS-LOB\5cLodging\5cPROD*”} -Properties * | Select Name,Description,managedby |fl
})
$objForm.Controls.Add($SearchButton)
$TEXTFIELD = New-Object System.Windows.Forms.textbox #TEXT FOR THE INPUT ACCOUNT
$TEXTFIELD.Location = New-Object System.Drawing.Size(10,50)
$TEXTFIELD.Size = New-Object System.Drawing.Size(230,20)
$TEXTFIELD.Text = “”
$TEXTFIELD.Tabstop = $True
$TEXTFIELD.ForeColor = “Blue”
$objForm.Controls.Add($TEXTFIELD)
$TEXTFIELDLBL = New-Object System.Windows.Forms.label #Reminder the end user to type LAN ID/DISPLAYNAME /EMAIL /SAP (CNKC OU DON’T SUPPORT SAP searching)
$TEXTFIELDLBL.Location = New-Object System.Drawing.Size(241,50)
$TEXTFIELDLBL.Size = New-Object System.Drawing.Size(300,20)
$TEXTFIELDLBL.Text = “(***LAN ID /Display Name or Email Address***)”
$TEXTFIELDLBL.Tabstop = $True
$TEXTFIELDLBL.ForeColor = “Blue”
$objForm.Controls.Add($TEXTFIELDLBL)
#########################################################################
$Check_If_Need_Add_To_Group = New-Object System.Windows.Forms.CheckBox
$Check_If_Need_Add_To_Group.Location = New-Object System.Drawing.Size(10,160)
$Check_If_Need_Add_To_Group.Size = New-Object System.Drawing.Size(300,20)
$Check_If_Need_Add_To_Group.Text = “Add user to following group”
$Check_If_Need_Add_To_Group.ForeColor = “Blue”
$objForm.Controls.Add($Check_If_Need_Add_To_Group)
$SecurityGroup = New-Object System.Windows.Forms.TextBox #TEXT FOR THE GROUP THAT WILL ADD USER TO
$SecurityGroup.Location = New-Object System.Drawing.Size(10,180)
$SecurityGroup.Size = New-Object System.Drawing.Size(450,20)
$SecurityGroup.Text = $GroupString
$SecurityGroup.ForeColor = “Blue”
$objForm.Controls.Add($SecurityGroup)
$SecurityGroupLBL = New-Object System.Windows.Forms.label # GROUP REMINDER
$SecurityGroupLBL.Location = New-Object System.Drawing.Size(460,180)
$SecurityGroupLBL.Size = New-Object System.Drawing.Size(150,20)
$SecurityGroupLBL.Text = “(*Group Name*)”
$SecurityGroupLBL.ForeColor = “Blue”
$objForm.Controls.Add($SecurityGroupLBL)
#########################################################################
$SEL_LANID = New-Object System.Windows.Forms.RadioButton # LAN ID RadioButton
$SEL_LANID.Location = New-Object System.Drawing.Size(10,80)
$SEL_LANID.Size = New-Object System.Drawing.Size(300,20)
$SEL_LANID.Text=”LAN ID”
#$SEL_LANID.Checked = $True
$objForm.Controls.Add($SEL_LANID)
$SEL_DISPLAYNAME = New-Object System.Windows.Forms.RadioButton #DisplayName RadioButton
$SEL_DISPLAYNAME.Location = New-Object System.Drawing.Size(10,100)
$SEL_DISPLAYNAME.Size = New-Object System.Drawing.Size(300,20)
$SEL_DISPLAYNAME.Text=”Display Name”
$SEL_DISPLAYNAME.Checked = $True
$objForm.Controls.Add($SEL_DISPLAYNAME)
$SEL_EMAIL_Address = New-Object System.Windows.Forms.RadioButton # Email Address RadioButton
$SEL_EMAIL_Address.Location = New-Object System.Drawing.Size(10,120)
$SEL_EMAIL_Address.Size = New-Object System.Drawing.Size(300,20)
$SEL_EMAIL_Address.Text=”Email Address”
$objForm.Controls.Add($SEL_EMAIL_Address)
$SEL_SAP_Number = New-Object System.Windows.Forms.RadioButton #SAP Number RadioButton
$SEL_SAP_Number.Location = New-Object System.Drawing.Size(10,140)
$SEL_SAP_Number.Size = New-Object System.Drawing.Size(300,20)
$SEL_SAP_Number.Text=”SAP Number”
$objForm.Controls.Add($SEL_SAP_Number)
$objForm.Add_KeyDown({if ($_.KeyCode -eq “Enter”) #Action on press Enter key
{
$Account_FRM_InPut_Value = $TEXTFIELD.Text.ToString() #Record input account
$NewGroup_FRM_Input_Value = $SecurityGroup.Text.ToString() #Record the input security group that will add user to
if(Test-Path C:\POWERSHELL\LOG)
{}
else
{mkdir -Path C:\POWERSHELL\LOG}
if($SEL_LANID.checked -eq $True) #LAN ID RadioButton clicked
{
echo 1 > C:\POWERSHELL\LOG\opt.log #Use number 1 to record as LAN ID radiobutton is clicked
echo $Account_FRM_InPut_Value > C:\POWERSHELL\LOG\Account_FRM_InPut_Value.log
echo $NewGroup_FRM_Input_Value > C:\POWERSHELL\LOG\New_Group_FRM_Input_Value.log
}
if($SEL_DISPLAYNAME.checked -eq $True) #Display name radiobutton clicked
{
echo 2 > C:\POWERSHELL\LOG\opt.log #Use number 2 to record as display name radio button is clicked.
echo $Account_FRM_InPut_Value > C:\POWERSHELL\LOG\Account_FRM_InPut_Value.log
echo $NewGroup_FRM_Input_Value > C:\POWERSHELL\LOG\New_Group_FRM_Input_Value.log
}
if($SEL_EMAIL_Address.checked -eq $True) #Email address radiobutton is clicked.
{
echo 3 > C:\POWERSHELL\LOG\opt.log #use number 3 to record email address RadioButton is clicked.
echo $Account_FRM_InPut_Value > C:\POWERSHELL\LOG\Account_FRM_InPut_Value.log
echo $NewGroup_FRM_Input_Value > C:\POWERSHELL\LOG\New_Group_FRM_Input_Value.log
}
if($SEL_SAP_Number.checked -eq $True) #SAP number RadioButton is clicked.
{
echo 4 > C:\POWERSHELL\LOG\opt.log #use number 4 to record SAP number is clicked.
echo $Account_FRM_InPut_Value > C:\POWERSHELL\LOG\Account_FRM_InPut_Value.log
echo $NewGroup_FRM_Input_Value > C:\POWERSHELL\LOG\New_Group_FRM_Input_Value.log
}
if($Check_If_Need_Add_To_Group.Checked -eq $true) #To record if the need update checkbox checked.
{
echo 1 > C:\POWERSHELL\LOG\NeedUpdateGroup.log #use number 1 to record it’s checked.
}
$objForm.Close() #Close the form after press Enter
}
})
$objForm.Add_KeyDown({if ($_.KeyCode -eq “Escape”) #Handle the ESC Key is pressed
{
$objForm.Close()
echo 999 > C:\Powershell\LOG\opt.log #set the number 999 to record the ESC key is pressed.
}})
$OKButton = New-Object System.Windows.Forms.Button #Draw the OK button.
$OKButton.Location = New-Object System.Drawing.Size(95,260)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = “OK”
$objForm.Controls.Add($OKButton)
$OKButton.Add_Click( #Same action as Enter key pressed.
{
$Account_FRM_InPut_Value = $TEXTFIELD.Text.ToString()
$NewGroup_FRM_Input_Value = $SecurityGroup.Text.ToString()
if(Test-Path C:\POWERSHELL\LOG)
{}
else
{mkdir -Path C:\POWERSHELL\LOG}
if($SEL_LANID.checked -eq $True)
{
echo 1 > C:\POWERSHELL\LOG\opt.log
echo $Account_FRM_InPut_Value > C:\POWERSHELL\LOG\Account_FRM_InPut_Value.log
echo $NewGroup_FRM_Input_Value > C:\POWERSHELL\LOG\New_Group_FRM_Input_Value.log
}
if($SEL_DISPLAYNAME.checked -eq $True)
{
echo 2 > C:\POWERSHELL\LOG\opt.log
echo $Account_FRM_InPut_Value > C:\POWERSHELL\LOG\Account_FRM_InPut_Value.log
echo $NewGroup_FRM_Input_Value > C:\POWERSHELL\LOG\New_Group_FRM_Input_Value.log
}
if($SEL_EMAIL_Address.checked -eq $True)
{
echo 3 > C:\POWERSHELL\LOG\opt.log
echo $Account_FRM_InPut_Value > C:\POWERSHELL\LOG\Account_FRM_InPut_Value.log
echo $NewGroup_FRM_Input_Value > C:\POWERSHELL\LOG\New_Group_FRM_Input_Value.log
}
if($SEL_SAP_Number.checked -eq $True)
{
echo 4 > C:\POWERSHELL\LOG\opt.log
echo $Account_FRM_InPut_Value > C:\POWERSHELL\LOG\Account_FRM_InPut_Value.log
echo $NewGroup_FRM_Input_Value > C:\POWERSHELL\LOG\New_Group_FRM_Input_Value.log
}
if($Check_If_Need_Add_To_Group.Checked -eq $true)
{
echo 1 > C:\POWERSHELL\LOG\NeedUpdateGroup.log
}
$objForm.Close()
})
$CancelButton = New-Object System.Windows.Forms.Button #Handle the cancel button is clicked.
$CancelButton.Location = New-Object System.Drawing.Size(370,260)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = “Cancel”
$CancelButton.Add_Click(
{$objForm.Close()
echo 888 > C:\Powershell\LOG\opt.log #Record cancel button is clicked
}
)
$objForm.Controls.Add($CancelButton)
$objForm.Topmost = $False
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog() #Show the FORM
###########################################################################FORM END############################################################################
#Handle the action after press Search Description button
$keyword = Get-Content C:\POWERSHELL\LOG\Keywords.log #Get user input keyword
#&Set_Log_Time
$keyword = ‘*’ + $keyword + ‘*’
#echo “Keyword is $keyword” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
$Current_Fix_Time = “{0:yyyy-MM-dd-HH-mm-ss}” -f(Get-Date)
if($keyword -ne ‘**’)
{
Get-ADGroup -Server $LocalDC -Searchbase $OU -Filter {description -like $keyword} -Properties * | Select Description,managedby,Name |sort-object Description |fl
Get-ADGroup -Server $LocalDC -Searchbase $OU -Filter {description -like $keyword} -Properties * | `
Select Description,managedby,Name |sort-object Description |export-csv C:\POWERSHELL\LOG\$Current_Fix_Time–SearchGroup–$env:Computername.csv
&Set_Log_Time
echo “Export search result to $Current_Fix_Time–SearchGroup–$env:Computername.csv” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
$keyword = $NULL
echo “” > C:\POWERSHELL\LOG\Keywords.log #Make sure the Get-ADGroup won’t run if there is nothing type in the search text field. So reset blank value to log
#&Set_Log_Time
# echo “Write blank to the keyword ” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
break
}
##############################################################################XXXXXXXXXXXXXXXXXXXXXXXXX###############################################
$Input_Opt = Get-Content C:\Powershell\LOG\opt.log
$Input_Group = Get-Content C:\Powershell\LOG\New_Group_FRM_Input_Value.log
$Is_Need_Update_Group = Get-Content C:\Powershell\LOG\NeedUpdateGroup.log
$Input_Account = Get-Content C:\Powershell\LOG\Account_FRM_InPut_Value.log
#$keyword = Get-Content C:\POWERSHELL\LOG\Keywords.log
if($Input_Account -eq “” )
{
#Write-Host “Please input the account information!!” -ForegroundColor Red
&Set_Log_Time
echo “The input account is blank .Exit….” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
break
}
if($Is_Need_Update_Group -ne “1”)
{
Write-Host “Add user to following group is not checked !!Exit…” -ForegroundColor Red
&Set_Log_Time
echo “Add user to following group is not checked !!Exit” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
break
}
if($Input_Group -eq “”)
{
Write-Host “You don’t type in any group for grant permission on it..Exit” -ForegroundColor Red
&Set_Log_Time
echo “You don’t type in any group for grant permission on it..Exit” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
break
}
if($Input_Group -ne “” -and $Input_Group -ne $NULL)
{ #######################################################################Check if run as 3 account ###########################
&Set_Log_Time
echo “Input group $Input_Group” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
$checkUser =whoami
&Set_Log_Time
echo “Current operator is $checkUser” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
#$checkUser
if(($checkUser.StartsWith(“apac\3”)))
{
}
else
{
Write-Host “Please run as 3 account for this Toolkit …Exit..” -ForegroundColor Red
&Set_Log_Time
echo “Current runas user is not 3 account , exit …” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
break
}
#######################################################################Check if run as 3 account ###########################
if($Input_Opt -eq “1”) #Select LAN ID
{
#Write-Host ‘LAN id’
&Set_Log_Time
echo “Check if LAN ID $Input_Account in the Active Directory ” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
$RResult = Get-ADuser -Server $LocalDC -searchbase $GlobalOU -Filter {sAMAccountName -eq $Input_Account } -Properties * | Select-Object sAMAccountName,name
if($RResult -eq $NULL)
{
Write-Host “The LAN ID can not be found !” -ForegroundColor Red
&Set_Log_Time
echo “LAN ID $Input_Account is not exist ,exit ….” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
break
}
################Check if the User is already in the Group#####################
$groupsVV = get_adgroups($Input_Account)
$objUserVV = New-Object System.Security.Principal.NTAccount($Input_Group)
$strSIDVV = $objUserVV.Translate([System.Security.Principal.SecurityIdentifier])
$resultVV = $groupsVV -contains $strSIDVV.Value
&Set_Log_Time
echo “Check if the user $Input_Account in the group $Input_Group” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
if($resultVV)
{
Write-Host “User is already in the group ..Exit..`n`n” -ForegroundColor Magenta
&Set_Log_Time
echo “Find the user $Input_Account is already in the group $Input_Group ” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
break
}
################Check if the User is already in the Group#####################
if($RResult -ne $NULL)
{
try
{
Add-ADGroupMember -Identity $Input_Group -Members $Input_Account
Write-Host “Add User $Input_Account To Group $Input_Group Success !” -ForegroundColor Green
&Set_Log_Time
echo “Add User $Input_Account To Group $Input_Group Success !” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
}
Catch
{
try {$CheckGroup= Get-ADGroup -Server $LocalDC -searchbase $GlobalOU -Filter {name -eq $Input_Group } -Properties * | Select-Object name}
catch{}
if($CheckGroup -eq $NULL )
{
Write-Host “The Group is not exist !!! Exit…” -ForegroundColor Red
&Set_Log_Time
echo “The group $Input_Group is not exist !!!Exit” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
break
}
Write-Host “The User $Input_Account is already in the group $Input_Group …” -ForegroundColor Magenta
&Set_Log_Time
echo “The User is already in the group …” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
}
#Write-Host “Add User $Input_Account To Group $Input_Group Success !” -ForegroundColor Green
}
}
if($Input_Opt -eq “2”) #Display Name
{
#Write-Host ‘DisplayName’
$RResult = Get-ADuser -Server $LocalDC -searchbase $GlobalOU -Filter {displayName -eq $Input_Account } -Properties * | Select-Object sAMAccountName,name
&Set_Log_Time
echo “Check is Display Name $Input_Account in the Active Directory ” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
if($RResult -eq $NULL)
{
Write-Host “The Display Name can not be found !” -ForegroundColor Red
&Set_Log_Time
echo “The Display Name $Input_Account can not be found in the Active Directory v_v !!” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
break
}
################Check if the User is already in the Group#####################
################Check if the User is already in the Group#####################
$SSAM = $RResult.sAMAccountName.ToString()
$groups = get_adgroups($SSAM)
$objUser = New-Object System.Security.Principal.NTAccount($Input_Group)
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
$resultVV = $groups -contains $strSID.Value
if($resultVV)
{
Write-Host “User is already in the group ..Exit..`n`n” -ForegroundColor Magenta
&Set_Log_Time
echo “User $Input_Account is already in the group $Input_Group ..Exit..” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
break
}
################Check if the User is already in the Group#####################
################Check if the User is already in the Group#####################
if($RResult -ne $NULL)
{
$Result=Get-ADuser -Server $LocalDC -searchbase $GlobalOU -Filter {displayName -eq $Input_Account } -Properties * | Select-Object sAMAccountName
&Set_Log_Time
echo “Convert Display Name $Input_Account to LAN ID ” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
$UserID = $Result.sAMAccountName.ToString()
try
{
Add-ADGroupMember -Identity $Input_Group -Members $UserID
Write-Host “Add User $Input_Account To Group $Input_Group Success !” -ForegroundColor Green
&Set_Log_Time
echo “Add User $Input_Account To Group $Input_Group Success !” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
}
Catch
{
try {$CheckGroup= Get-ADGroup -Server $LocalDC -searchbase $GlobalOU -Filter {name -eq $Input_Group } -Properties * | Select-Object name}
catch{}
if($CheckGroup -eq $NULL )
{
Write-Host “The Group is not exist !!! Exit…” -ForegroundColor Red
&Set_Log_Time
echo “The Group $Input_Group is not exist !!! Exit…” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
}
else
{
Write-Host “The User is already in the group …” -ForegroundColor Magenta
&Set_Log_Time
echo “The User is already in the group …” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
}
}
}
}
if($Input_Opt -eq “3”) #Email Address
{
#Write-Host ‘Email Address’
$RResult = Get-ADuser -Server $LocalDC -searchbase $GlobalOU -Filter {mail -eq $Input_Account } -Properties * | Select-Object sAMAccountName,name
&Set_Log_Time
echo “Check if the email address $Input_Account in the Active Directory” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
if($RResult -eq $NULL)
{
Write-Host “The mail can not be found !” -ForegroundColor Red
&Set_Log_Time
echo “The mail $Input_Account can not be found V_V !!” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
break
}
################Check if the User is already in the Group#####################
$SSAM = $RResult.sAMAccountName.ToString()
$groups = get_adgroups($SSAM)
$objUser = New-Object System.Security.Principal.NTAccount($Input_Group)
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
$resultVV = $groups -contains $strSID.Value
if($resultVV)
{
Write-Host “User is already in the group ..Exit..`n`n” -ForegroundColor Magenta
&Set_Log_Time
echo “The user $Input_Account is already in the group $Input_Group . Exit” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
break
}
################Check if the User is already in the Group#####################
if($RResult -ne $NULL)
{
$Result=Get-ADuser -Server $LocalDC -searchbase $GlobalOU -Filter {mail -eq $Input_Account } -Properties * | Select-Object sAMAccountName
&Set_Log_Time
echo “Convert email address $Input_Account to LAN ID ” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
$UserID = $Result.sAMAccountName.ToString()
try{
Add-ADGroupMember -Identity $Input_Group -Members $UserID
Write-Host “Add User $Input_Account To Group $Input_Group Success !” -ForegroundColor Green
&Set_Log_Time
echo “Add User $Input_Account To Group $Input_Group Success !” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
}
Catch
{
try {$CheckGroup= Get-ADGroup -Server $LocalDC -searchbase $GlobalOU -Filter {name -eq $Input_Group } -Properties * | Select-Object name}
catch{}
if($CheckGroup -eq $NULL )
{
Write-Host “The Group is not exist !!! Exit…” -ForegroundColor Red
&Set_Log_Time
echo “The Group $Input_Group is not exist !!! Exit…” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
}
else
{
Write-Host “The User is already in the group …” -ForegroundColor Magenta
&Set_Log_Time
echo “The User $Input_Account is already in the group $Input_Group…” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
}
}
}
}
if($Input_Opt -eq “4”) #Select SAP
{
#Write-Host ‘SAP’
$RResult = Get-ADuser -Server $LocalDC -searchbase $GlobalOU -Filter {extensionAttribute9 -eq $Input_Account } -Properties * | Select-Object sAMAccountName
&Set_Log_Time
echo “Check if the SAP $Input_Account in the Active Directory” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
if($RResult -eq $NULL)
{
Write-Host “The SAP can not be found !” -ForegroundColor Red
&Set_Log_Time
echo “The SAP $Input_Account can not be found !” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
break
}
################Check if the User is already in the Group#####################
$SSAM = $RResult.sAMAccountName.ToString()
$groups = get_adgroups($SSAM)
$objUser = New-Object System.Security.Principal.NTAccount($Input_Group)
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
$resultVV = $groups -contains $strSID.Value
if($resultVV)
{
Write-Host “User is already in the group ..Exit..`n`n” -ForegroundColor Magenta
&Set_Log_Time
echo “User $Input_Account is already in the group $Input_Group ..Exit..” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
break
}
################Check if the User is already in the Group#####################
if($RResult -ne $NULL)
{
$Result=Get-ADuser -Server $LocalDC -searchbase $GlobalOU -Filter {extensionAttribute9 -eq $Input_Account } -Properties * | Select-Object sAMAccountName
$UserID = $Result.sAMAccountName.ToString()#Convert SAP to LAN ID
try{
Add-ADGroupMember -Identity $Input_Group -Members $UserID
Write-Host “Add User $Input_Account To Group $Input_Group Success !” -ForegroundColor Green
&Set_Log_Time
echo “Add User $Input_Account To Group $Input_Group Success !” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
}
Catch
{
try {$CheckGroup= Get-ADGroup -Server $LocalDC -searchbase $GlobalOU -Filter {name -eq $Input_Group } -Properties * | Select-Object name} #check if the group is exist
catch{}
if($CheckGroup -eq $NULL )
{
Write-Host “The Group is not exist !!! Exit…” -ForegroundColor Red
&Set_Log_Time
echo “The Group $Input_Group is not exist !!! Exit…” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
}
else
{
Write-Host “The User is already in the group …” -ForegroundColor Magenta
&Set_Log_Time
echo “The User $Input_Account is already in the group $Input_Group …” >> C:\POWERSHELL\LOG\$LOGTotalDate.log
}
}
}
}
}
##############################################################################XXXXXXXXXXXXXXXXXXXXXXXXX###############################################
1 Answers
Best Answer
这是PowerShell已知的bug,目前没有解决方案,只能等待官方修复。
同样的问题也可参见:
PowerShell ISE Crashes after loading Winforms
PowerShell ISE Freezes after executing WinForm