ad常用脚本.docx
《ad常用脚本.docx》由会员分享,可在线阅读,更多相关《ad常用脚本.docx(8页珍藏版)》请在冰豆网上搜索。
ad常用脚本
'******************************************程序开始**************************************************
DB_Path="\\bj-mgr-02\SHARE\DB_Mail\count.accdb" '设置数据库路径信息
Setfso=Createobject("Scripting.FileSystemObject")
setWrite_list=fso.Getfile("result.ps1")
setW_Data=Write_list.OpenAsTextStream(2,0)
OptType=InputBox("请输入您要做的事的编号:
"&Chr(10)&"1创建邮箱"&Chr(10)&"2核查账户"&Chr(10)&"3创建账户"&Chr(10)&"4删除邮箱或账户"&Chr(10)&"5邮箱扩容"&Chr(10)&"6用户加组","参数输入")
User_list=Get_user()
Select CaseOptType
Case"1"
Create_Mailbox()
Case"2"
Check_User()
Case"3"
Create_User()
Case"4"
Delete_MBX()
Case"5"
Expend_Content()
Case"6"
Add_Group()
Caseelse
Msgbox"请选择您要进行的操作!
"
WScript.quit
EndSelect
Msgbox"success!
"
W_Data.close
Setadocon=Nothing
'*****************************************程序结束***************************************************
'*****************************************获取用户列表***********************************************
FunctionGet_user()
setGet_list=fso.Getfile("usrlist.txt")
setRead_list=Get_list.OpenAsTextStream(1,0)
Str_user=Read_list.ReadAll
ifRight(Str_user,1)=Chr(10)then
Str_user=left(Str_user,Len(Str_user)-2)
Endif
Get_user=Split(Str_user,Chr(13)&Chr(10))
Read_list.close
EndFunction
'****************************************检索用户所属企业********************************************
FunctionCheck_Mark()
PPoint=InStrRev(User_list(0),".")
IfPPoint<>0then
Check_Mark=Right(User_list(0),Len(User_list(0))-InStrRev(User_list(0),"."))
else
Check_Mark="head"
Endif
EndFunction
'****************************************生成创建邮箱脚本********************************************
Function Create_Mailbox()
setadocon=Createobject("adodb.recordset")
connstr="Provider=Microsoft.ACE.OLEDB.12.0;DataSource="&DB_Path&";PersistSecurityInfo=False;"
ComMark=Check_Mark()
sql="selectDB_name,User_countfromMail_DBStorewhereE_namelike'%"&ComMark&"%'orderbyUser_countASC"
' msgboxsql
adocon.opensql,connstr,1,1
Ifadocon.eofthen
msgbox"未查到该企业邮箱所属DB!
"
Wscript.quit
Else
str_DB=Trim(adocon(0))
str_count=Trim(adocon
(1))
Endif
Int_str=CInt(str_count)
adocon.close
ifInt_str>500then
msgbox"企业所属所有DB中邮箱个数均超过500"
Wscript.quit
Else
pp=Int_str+Ubound(User_list)+1
Chsql="updateMail_DBStoresetUser_count='"&pp&"'WhereDB_name='"&str_DB&"'"
adocon.openChsql,connstr,2,2
Endif
For i=0ToUbound(User_list)
show_Str="Enable-Mailbox-identity"&Trim(User_list(i))&"@-Database"&str_DB&"|Get-Mailbox|selectname,windowsemailaddress,Database"&vbCrLf
W_Data.Writeshow_Str
Next
insert_DB="insertintoHxy_Opt_Markvalues('"&i&"','"&ComMark&"','1','"&now()&"');"
adocon.openinsert_DB,connstr,1,1
msgboxi&"条记录已处理!
"
EndFunction
'********************************************核查用户****************************************************
FunctionCheck_User()
setadocon=Createobject("adodb.recordset")
connstr="Provider=Microsoft.ACE.OLEDB.12.0;DataSource="&DB_Path&";PersistSecurityInfo=False;"
ComMark=Check_Mark()
User_list=Get_user()
strSearchAttribute="sAMAccountName"
For i=0ToUbound(User_list)
Re_user=Search_User(strSearchAttribute,User_list(i))
IfRe_user="None"Then
show_Str=User_list(i)&"不存在该用户"
Else
SetobjUser=GetObject(Re_user)
show_Str=objUser.displayname
Endif
W_Data.Writeshow_Str&vbCrLf
Next
insert_DB="insertintoHxy_Opt_Markvalues('"&i&"','"&ComMark&"','2','"&now()&"');"
adocon.openinsert_DB,connstr,1,1
msgboxi&"条记录已处理!
"
EndFunction
'********************************************创建用户*****************************************************
FunctionCreate_User()
setadocon=Createobject("adodb.recordset")
connstr="Provider=Microsoft.ACE.OLEDB.12.0;DataSource="&DB_Path&";PersistSecurityInfo=False;"
SetobjShell=CreateObject("Wscript.Shell")
strPath=Wscript.ScriptFullName
SetobjFile=fso.GetFile(strPath)
strPath=fso.GetParentFolderName(objFile)
SetobjExcel=CreateObject("Excel.Application")
SetobjWorkbook=objExcel.Workbooks.Open(strPath&"\users.xlsx")
PPoint=InStrRev(objExcel.Cells(2,1).Value,".")
IfPPoint<>0then
ComMark=Right(objExcel.Cells(2,1).Value,Len(objExcel.Cells(2,1).Value)-InStrRev(objExcel.Cells(2,1).Value,"."))
else
ComMark="head"
Endif
SetobjOU=GetObject("LDAP:
//ou=lastimp,dc=sinopec,dc=ad")
intRow=2
DoUntilobjExcel.Cells(intRow,1).Value=""
SamAcc=Trim(objExcel.Cells(intRow,1).Value)
strSearchAttribute="sAMAccountName"
IsNumber=Search_User(strSearchAttribute,SamAcc)
IfIsNumber="None"Then
SetobjUser=objOU.Create("User","cn="&SamAcc)
objUser.sAMAccountName=SamAcc
objUser.userPrincipalName=SamAcc&"@"
objUser.GivenName=objExcel.Cells(intRow,4).Value
objUser.SN=objExcel.Cells(intRow,3).Value
objUser.Displayname=objExcel.Cells(intRow,2).Value
objUser.Mail=objExcel.Cells(intRow,1).Value&"@"
objUser.Company=objExcel.Cells(intRow,5).Value
objUser.department=objExcel.Cells(intRow,6).Value
objUser.SetInfo
objUser.SetPasswordSamAcc
objUser.put"userAccountControl",66048
objUser.AccountDisabled=FALSE
objUser.SetInfo
intRow=intRow+1
Else
show_Str="第"&intRow-1&"个用户已存在,忽略!
"&VbCrLf&"账户名:
"&SamAcc&VbCrLf&"中文名:
"&objExcel.Cells(intRow,2).Value&VbCrLf&"#############################################"&VbCrLf
W_Data.Writeshow_Str
intRow=intRow+1
Endif
Loop
objExcel.Quit
intRow=intRow-2
insert_Mark="insertintoHxy_Opt_Markvalues('"&intRow&"','"&ComMark&"','3','"&now()&"');"
adocon.openinsert_Mark,connstr,1,1
msgboxintRow&"条记录已处理!
"
EndFunction
'********************************************生成删除邮箱脚本*****************************************************
FunctionDelete_MBX()
setadocon=Createobject("adodb.recordset")
connstr="Provider=Microsoft.ACE.OLEDB.12.0;DataSource="&DB_Path&";PersistSecurityInfo=False;"
DelType=InputBox("请输入您要做的事的编号:
"&Chr(10)&"1仅删除邮箱属性"&Chr(10)&"2删除邮箱及账户","参数输入")
Select CaseDelType
Case"1"
Del_Str="Disable-MailBox"
Case"2"
Del_Str="Remove-MailBox"
Caseelse
Msgbox"未识别的删除类型,请确认删除类型!
"
Wscript.quit
EndSelect
ComMark=Check_Mark()
User_list=Get_user()
For i=0ToUbound(User_list)
Delete_Usr=Del_Str&"-identity"&User_list(i)&"@-Confirm:
$false"&VbCrLf
W_Data.WriteDelete_Usr
Next
insert_DB="insertintoHxy_Opt_Markvalues('"&i&"','"&ComMark&"','4','"&now()&"');"
adocon.openinsert_DB,connstr,1,1
msgboxi&"条记录已处理!
"
EndFunction
'***********************************************邮箱扩容**************************************************************
FunctionExpend_Content()
setadocon=Createobject("adodb.recordset")
connstr="Provider=Microsoft.ACE.OLEDB.12.0;DataSource="&DB_Path&";PersistSecurityInfo=False;"
DelType=InputBox("请选择您扩容后邮箱大小(单位MB):
"&Chr(10)&"1512MB"&Chr(10)&"21024MB"&Chr(10)&"32048MB","参数输入")
Select CaseDelType
Case"1"
ProhibitSendQuota="491680KB"
ProhibitSendReceiveQuota="512000KB"
IssueWarningQuota="471440KB"
Case"2"
ProhibitSendQuota="983040KB"
ProhibitSendReceiveQuota="1024000KB"
IssueWarningQuota="972800KB"
Case"3"
ProhibitSendQuota="1945600KB"
ProhibitSendReceiveQuota="2048000KB"
IssueWarningQuota="1894400KB"
Caseelse
Msgbox"未识别的删除类型,请确认删除类型!
"
Wscript.quit
EndSelect
ComMark=Check_Mark()
User_list=Get_user()
For i=0ToUbound(User_list)
Ex_Con="Set-mailbox"&User_list(i)&"@-UseDatabaseQuotaDefaults$false-ProhibitSendQuota"&ProhibitSendQuota&"-ProhibitSendReceiveQuota"&ProhibitSendReceiveQuota&"-IssueWarningQuota"&IssueWarningQuota&VbCrLf
W_Data.WriteEx_Con
Next
insert_DB="insertintoHxy_Opt_Markvalues('"&i&"','"&ComMark&"','5','"&now()&"');"
adocon.openinsert_DB,connstr,1,1
msgboxi&"条记录已处理!
"
EndFunction
'*****************************************************检索用户是否存在*******************************************************
FunctionSearch_User(ByvalstrSearchAttribute,strSearchValue)
DIMobjRoot
DIMgetUserCn,getUserCmd,getUserRS
onerrorresumenext
setobjRoot=getobject("LDAP:
//RootDSE")
setgetUserCn=createobject("ADODB.Connection")
setgetUserCmd=createobject("ADODB.Command")
setgetUserRS=createobject("ADODB.Recordset")
getUserCn.open"Provider=ADsDSOObject;"
getUserCmd.activeconnection=getUserCn
getUserCmandtext="//"&objRoot.get("defaultNamingContext")&">;"&"(&(objectCategory=person)(objectClass=user)("&strSearchAttribute&"="&strSearchValue&"));"&"adsPath;subtree"
setgetUserRs=getUserCmd.execute
ifgetUserRS.recordcount=0then
Search_User="None"
elseifgetUserRS.recordcount=1then
Search_User=getUserRS(0)
else
Msgbox"存在未知错误,请查询该用户!
用户名为:
"&strSearchValue
endif
getUserCn.close
Endfunction
'**************************************************用户加组********************************************************************
FunctionAdd_Group()
setadocon=Createobject("adodb.recordset")
connstr="Provider=Microsoft.ACE.OLEDB.12.0;DataSource="&DB_Path&";PersistSecurityInfo=False;"
strSearchAttribute="sAMAccountName"
ConstADS_PROPERTY_APPEND=3
ComMark=Check_Mark()
User_list=Get_user()
Group_Type=InputBox("请输入您要加入组的编号:
"&Chr(10)&"1加入G-tmsk组"&Chr(10)&"2加入G-zjqyk组"&Chr(10)&"3加入G-jyzfk组"&Chr(10)&"4加入G-bc-hq-clien组"&Chr(10)&"5加入G-zjqy组","参数输入")
Select CaseGroup_Type
Case"1"
SetobjGroup=GetObject("LDAP:
//CN=g-tmsk,OU=财务公司,OU=总部,OU=组织机构,DC=sinopec,DC=ad")
Case"2"
SetobjGroup=GetObject("LDAP:
//CN=g-zjqyk,OU=财务公司,OU=总部,OU=组织机构,DC=sinopec,DC=ad")
Case"3"
Set