黑猫脚本怎么样硬件单头跟双头区别

发布时间:2020-06-27 来源:脚本之家 点击:

本节将创建并测试自己的ActiveXEXE程序
'********************************************************************
'*
'*File:Restart.vbs
'*Created:March1999
'*Version:1.0
'*
'*MainFunction:Shutsdown,PowerOff,LogOff,Restartsamachine.
'*
'*Restart.vbs/S<server>[/U<username>][/W<password>]
'*[/O<outputfile>][/L}[/P][/R][/Q][/F][/T<timeinseconds>]
'*
'*Copyright(C)1999MicrosoftCorporation
'*
'********************************************************************

OPTIONEXPLICIT

'Defineconstants
CONSTCONST_ERROR=0
CONSTCONST_WSCRIPT=1
CONSTCONST_CSCRIPT=2
CONSTCONST_SHOW_USAGE=3
CONSTCONST_PROCEED=4

'ShutdownMethodConstants
CONSTCONST_SHUTDOWN=1
CONSTCONST_LOGOFF=0
CONSTCONST_POWEROFF=8
CONSTCONST_REBOOT=2
CONSTCONST_FORCE_REBOOT=6
CONSTCONST_FORCE_POWEROFF=12
CONSTCONST_FORCE_LOGOFF=4
CONSTCONST_FORCE_SHUTDOWN=5

'Declarevariables
DimintOpMode,i
DimstrServer,strUserName,strPassword,strOutputFile
DimblnLogoff,blnPowerOff,blnReBoot,blnShutDown
DimblnForce
DimintTimer
DimUserArray(3)
DimMyCount

'Makesurethehostiscsript,ifnotthenabort
VerifyHostIsCscript()

'Parsethecommandline
intOpMode=intParseCmdLine(strServer,_
strUserName,_
strPassword,_
strOutputFile,_
blnLogoff,_
blnPowerOff,_
blnReBoot,_
blnShutdown,_
blnForce,_
intTimer)

SelectCaseintOpMode

CaseCONST_SHOW_USAGE
CallShowUsage()

CaseCONST_PROCEED
CallReboot(strServer,_
strOutputFile,_
strUserName,_
strPassword,_
blnReboot,_
blnForce,_
intTimer)

CallLogOff(strServer,_
strOutputFile,_
strUserName,_
strPassword,_
blnLogoff,_
blnForce,_
intTimer)

CallPowerOff(strServer,_
strOutputFile,_
strUserName,_
strPassword,_
blnPowerOff,_
blnForce,_
intTimer)

CallShutDown(strServer,_
strOutputFile,_
strUserName,_
strPassword,_
blnShutDown,_
blnForce,_
intTimer)

CaseCONST_ERROR
'DoNothing

CaseElse'Default--shouldneverhappen
CallWscript.Echo("Erroroccurredinpassingparameters.")

EndSelect


'********************************************************************
'*
'*SubReboot()
'*
'*Purpose:Rebootsamachine.
'*
'*Input:strServeramachinename
'*strOutputFileanoutputfilename
'*strUserNamethecurrentuser'sname
'*strPasswordthecurrentuser'spassword
'*blnForcespecifieswhethertoforcethelogoff
'*intTimerspecifiestheamountoftimetoperformthefunction
'*
'*Output:ResultsareeitherprintedonscreenorsavedinstrOutputFile.
'*
'********************************************************************
PrivateSubReboot(strServer,strOutputFile,strUserName,strPassword,blnReboot,blnForce,intTimer)


ONERRORRESUMENEXT

DimobjFileSystem,objOutputFile,objService,objEnumerator,objInstance
DimstrQuery,strMessage
DimintStatus
ReDimstrID(0),strName(0)

ifblnreboot=falsethen
ExitSub
Endif

ifintTimer>0then
wscript.echo"Rebootingmachine"&strServer&"in"&intTimer&"seconds..."
wscript.sleep(intTimer*1000)
Endif

'Openatextfileforoutputifthefileisrequested
IfNotIsEmpty(strOutputFile)Then
If(NOTblnOpenFile(strOutputFile,objOutputFile))Then
CallWscript.Echo("Couldnotopenanoutputfile.")
ExitSub
EndIf
EndIf

'Establishaconnectionwiththeserver.
IfblnConnect("root\cimv2",_
strUserName,_
strPassword,_
strServer,_
objService)Then
CallWscript.Echo("")
CallWscript.Echo("Pleasechecktheservername,"_
&"credentialsandWBEMCore.")
ExitSub
EndIf

strID(0)=""
strName(0)=""
strMessage=""
strQuery="Select*FromWin32_OperatingSystem"

SetobjEnumerator=objService.ExecQuery(strQuery,,0)
IfErr.NumberThen
Print"Error0x"&CStr(Hex(Err.Number))&"occurredduringthequery."
IfErr.Description<>""Then
Print"Errordescription:"&Err.Description&"."
EndIf
Err.Clear
ExitSub
EndIf

i=0
ForEachobjInstanceinobjEnumerator
IfblnForceThen
intStatus=objInstance.Win32ShutDown(CONST_FORCE_REBOOT)
Else
intStatus=objInstance.Win32ShutDown(CONST_REBOOT)
EndIf

IFintStatus=0Then
strMessage="Rebootamachine"&strServer&"."
Else
strMessage="Failedtorebootamachine"&strServer&"."
EndIf
CallWriteLine(strMessage,objOutputFile)
Next

IfIsObject(objOutputFile)Then
objOutputFile.Close
CallWscript.Echo("Resultsaresavedinfile"&strOutputFile&".")
EndIf
EndSub


'********************************************************************
'*
'*SubLogOff()
'*
'*Purpose:Logsofftheusercurrentlyloggedontoamachine.
'*
'*Input:strServeramachinename
'*strOutputFileanoutputfilename
'*strUserNamethecurrentuser'sname
'*strPasswordthecurrentuser'spassword
'*blnForcespecifieswhethertoforcethelogoff
'*intTimerspecifiestheamountoftimetopreformthefunction
'*
'*Output:ResultsareeitherprintedonscreenorsavedinstrOutputFile.
'*
'********************************************************************
PrivateSubLogOff(strServer,strOutputFile,strUserName,strPassword,blnLogoff,blnForce,intTimer)


ONERRORRESUMENEXT

DimobjFileSystem,objOutputFile,objService,objEnumerator,objInstance
DimstrQuery,strMessage
DimintStatus
ReDimstrID(0),strName(0)

Ifblnlogoff=falsethen
ExitSub
Endif

ifintTimer>1then
wscript.echo"Loggingoffmachine"&strServer&"in"&intTimer&"seconds..."
wscript.sleep(intTimer*1000)
Endif

'Openatextfileforoutputifthefileisrequested
IfNotIsEmpty(strOutputFile)Then
If(NOTblnOpenFile(strOutputFile,objOutputFile))Then
CallWscript.Echo("Couldnotopenanoutputfile.")
ExitSub
EndIf
EndIf

'Establishaconnectionwiththeserver.
IfblnConnect("root\cimv2",_
strUserName,_
strPassword,_
strServer,_
objService)Then
CallWscript.Echo("")
CallWscript.Echo("Pleasechecktheservername,"_
&"credentialsandWBEMCore.")
ExitSub
EndIf

strID(0)=""
strName(0)=""
strMessage=""
strQuery="Select*FromWin32_OperatingSystem"

SetobjEnumerator=objService.ExecQuery(strQuery,,0)
IfErr.NumberThen
Print"Error0x"&CStr(Hex(Err.Number))&"occurredduringthequery."
IfErr.Description<>""Then
Print"Errordescription:"&Err.Description&"."
EndIf
Err.Clear
ExitSub
EndIf

i=0
ForEachobjInstanceinobjEnumerator
IfblnForceThen
intStatus=objInstance.Win32ShutDown(CONST_FORCE_LOGOFF)
Else
intStatus=objInstance.Win32ShutDown(CONST_LOGOFF)
EndIf

IFintStatus=0Then
strMessage="Loggingoffthecurrentuseronmachine"&_
strServer&"..."
Else
strMessage="Failedtologoffthecurrentuserfrommachine"_
&strServer&"."
EndIf
CallWriteLine(strMessage,objOutputFile)
Next

IfIsObject(objOutputFile)Then
objOutputFile.Close
CallWscript.Echo("Resultsaresavedinfile"&strOutputFile&".")
EndIf
EndSub


'********************************************************************
'*
'*SubPowerOff()
'*
'*Purpose:Powersoffamachine.
'*
'*Input:strServeramachinename
'*strOutputFileanoutputfilename
'*strUserNamethecurrentuser'sname
'*strPasswordthecurrentuser'spassword
'*blnForcespecifieswhethertoforcethelogoff
'*intTimerspecifiestheamountoftimetoperformthefunction
'*
'*Output:ResultsareeitherprintedonscreenorsavedinstrOutputFile.
'*
'********************************************************************
PrivateSubPowerOff(strServer,strOutputFile,strUserName,strPassword,blnPowerOff,blnForce,intTimer)


ONERRORRESUMENEXT

DimobjFileSystem,objOutputFile,objService,objEnumerator,objInstance
DimstrQuery,strMessage
DimintStatus
ReDimstrID(0),strName(0)

ifblnPoweroff=falsethen
Exitsub
Endif

IfintTimer>0then
wscript.echo"Poweringoffmachine"&strServer&"in"&intTimer&"seconds..."
wscript.sleep(intTimer*1000)
Endif

'Openatextfileforoutputifthefileisrequested
IfNotIsEmpty(strOutputFile)Then
If(NOTblnOpenFile(strOutputFile,objOutputFile))Then
CallWscript.Echo("Couldnotopenanoutputfile.")
ExitSub
EndIf
EndIf

'Establishaconnectionwiththeserver.
IfblnConnect("root\cimv2",_
strUserName,_
strPassword,_
strServer,_
objService)Then
CallWscript.Echo("")
CallWscript.Echo("Pleasechecktheservername,"_
&"credentialsandWBEMCore.")
ExitSub
EndIf

strID(0)=""
strName(0)=""
strMessage=""
strQuery="Select*FromWin32_OperatingSystem"

SetobjEnumerator=objService.ExecQuery(strQuery,,0)
IfErr.NumberThen
Print"Error0x"&CStr(Hex(Err.Number))&"occurredduringthequery."
IfErr.Description<>""Then
Print"Errordescription:"&Err.Description&"."
EndIf
Err.Clear
ExitSub
EndIf

i=0
ForEachobjInstanceinobjEnumerator
IfblnForceThen
intStatus=objInstance.Win32ShutDown(CONST_FORCE_POWEROFF)
Else
intStatus=objInstance.Win32ShutDown(CONST_POWEROFF)
EndIf

IFintStatus=0Then
strMessage="Poweroffmachine"&strServer&"."
Else
strMessage="Failedtopoweroffmachine"&strServer&"."
EndIf
CallWriteLine(strMessage,objOutputFile)
Next

IfIsObject(objOutputFile)Then
objOutputFile.Close
CallWscript.Echo("Resultsaresavedinfile"&strOutputFile&".")
EndIf
EndSub


'********************************************************************
'*
'*SubShutdown()
'*
'*Purpose:Shutsdownamachine.
'*
'*Input:strServeramachinename
'*strOutputFileanoutputfilename
'*strUserNamethecurrentuser'sname
'*strPasswordthecurrentuser'spassword
'*blnForcespecifieswhethertoforcethelogoff
'*intTimerspecifiestheamountoftimetoperformthefunction
'*
'*Output:ResultsareeitherprintedonscreenorsavedinstrOutputFile.
'*
'********************************************************************
PrivateSubShutdown(strServer,strOutputFile,strUserName,strPassword,blnShutDown,blnForce,intTimer)


ONERRORRESUMENEXT

DimobjFileSystem,objOutputFile,objService,objEnumerator,objInstance
DimstrQuery,strMessage
DimintStatus
ReDimstrID(0),strName(0)

IfblnShutdown=Falsethen
ExitSub
Endif

ifintTimer>0then
wscript.echo"Shuttingdowncomputer"&strServer&"in"&intTimer&"seconds..."
wscript.sleep(intTimer*1000)
Endif


'Openatextfileforoutputifthefileisrequested
IfNotIsEmpty(strOutputFile)Then
If(NOTblnOpenFile(strOutputFile,objOutputFile))Then
CallWscript.Echo("Couldnotopenanoutputfile.")
ExitSub
EndIf
EndIf

'Establishaconnectionwiththeserver.
IfblnConnect("root\cimv2",_
strUserName,_
strPassword,_
strServer,_
objService)Then
CallWscript.Echo("")
CallWscript.Echo("Pleasechecktheservername,"_
&"credentialsandWBEMCore.")
ExitSub
EndIf

strID(0)=""
strName(0)=""
strMessage=""
strQuery="Select*FromWin32_OperatingSystem"

SetobjEnumerator=objService.ExecQuery(strQuery,,0)
IfErr.NumberThen
Print"Error0x"&CStr(Hex(Err.Number))&"occurredduringthequery."
IfErr.Description<>""Then
Print"Errordescription:"&Err.Description&"."
EndIf
Err.Clear
ExitSub
EndIf

i=0
ForEachobjInstanceinobjEnumerator
IfblnForceThen
intStatus=objInstance.Win32ShutDown(CONST_FORCE_SHUTDOWN)
Else
intStatus=objInstance.Win32ShutDown(CONST_SHUTDOWN)
EndIf

IFintStatus=0Then
strMessage="Shutsdownmachine"&strServer&"."
Else
strMessage="Failedtoshutdownmachine"&strServer&"."
EndIf
CallWriteLine(strMessage,objOutputFile)
Next

IfIsObject(objOutputFile)Then
objOutputFile.Close
CallWscript.Echo("Resultsaresavedinfile"&strOutputFile&".")
EndIf
EndSub



'********************************************************************
'*
'*FunctionintParseCmdLine()
'*
'*Purpose:Parsesthecommandline.
'*Input:
'*
'*Output:strServeraremoteserver(""=localserver")
'*strUserNamethecurrentuser'sname
'*strPasswordthecurrentuser'spassword
'*strOutputFileanoutputfilename
'*intTimeramountoftimeinseconds
'*
'********************************************************************
PrivateFunctionintParseCmdLine(ByRefstrServer,_
ByRefstrUserName,_
ByRefstrPassword,_
ByRefstrOutputFile,_
ByRefblnLogoff,_
ByRefblnShutdown,_
ByRefblnReboot,_
ByRefblnPowerOff,_
ByRefblnForce,_
ByRefintTimer)


ONERRORRESUMENEXT

DimstrFlag
DimintState,intArgIter
DimobjFileSystem

IfWscript.Arguments.Count>0Then
strFlag=Wscript.arguments.Item(0)
EndIf

IfIsEmpty(strFlag)Then'Noargumentshavebeenreceived
Wscript.Echo("ArgumentsareRequired.")
intParseCmdLine=CONST_ERROR
ExitFunction
EndIf

'Checkiftheuserisaskingforhelporisjustconfused
If(strFlag="help")OR(strFlag="/h")OR(strFlag="\h")OR(strFlag="-h")_
OR(strFlag="\?")OR(strFlag="/?")OR(strFlag="?")_
OR(strFlag="h")Then
intParseCmdLine=CONST_SHOW_USAGE
ExitFunction
EndIf

'Retrievethecommandlineandsetappropriatevariables
intArgIter=0
DoWhileintArgIter<=Wscript.arguments.Count-1
SelectCaseLeft(LCase(Wscript.arguments.Item(intArgIter)),2)

Case"/s"
intParseCmdLine=CONST_PROCEED
IfNotblnGetArg("Server",strServer,intArgIter)Then
intParseCmdLine=CONST_ERROR
ExitFunction
EndIf
intArgIter=intArgIter+1

Case"/o"
IfNotblnGetArg("OutputFile",strOutputFile,intArgIter)Then
intParseCmdLine=CONST_ERROR
ExitFunction
EndIf
intArgIter=intArgIter+1

Case"/u"
IfNotblnGetArg("UserName",strUserName,intArgIter)Then
intParseCmdLine=CONST_ERROR
ExitFunction
EndIf
intArgIter=intArgIter+1

Case"/w"
IfNotblnGetArg("UserPassword",strPassword,intArgIter)Then
intParseCmdLine=CONST_ERROR
ExitFunction
EndIf
intArgIter=intArgIter+1

Case"/f"
blnForce=True
intArgIter=intArgIter+1

Case"/r"
blnReBoot=True
userarray(0)=blnReBoot
intArgIter=intArgIter+1

Case"/q"
blnPowerOff=True
userarray(1)=blnPowerOff
intArgIter=intArgIter+1

Case"/l"
blnLogOff=True
userarray(2)=blnLogoff
intArgIter=intArgIter+1

Case"/p"
blnShutDown=True
userarray(3)=blnShutDown
intArgIter=intArgIter+1

Case"/t"
IfNotblnGetArg("Timer",intTimer,intArgIter)Then
intParseCmdLine=CONST_ERROR
ExitFunction
EndIf
intArgIter=intArgIter+1

CaseElse'Weshouldn'tgethere
CallWscript.Echo("Invalidormisplacedparameter:"_
&Wscript.arguments.Item(intArgIter)&vbCRLF_
&"Pleasechecktheinputandtryagain,"&vbCRLF_
&"orinvokewith'/?'forhelpwiththesyntax.")
Wscript.Quit

EndSelect

Loop'**intArgIter<=Wscript.arguments.Count-1

MyCount=0

fori=0to3
ifuserarray(i)=Truethen
MyCount=Mycount+1
Endif
Next

ifMycount>1then
intParseCmdLine=CONST_SHOW_USAGE
Endif

IfIsEmpty(intParseCmdLine)Then
intParseCmdLine=CONST_ERROR
Wscript.Echo("ArgumentsareRequired.")
EndIf

EndFunction

'********************************************************************
'*
'*SubShowUsage()
'*
'*Purpose:Showsthecorrectusagetotheuser.
'*
'*Input:None
'*
'*Output:Helpmessagesaredisplayedonscreen.
'*
'********************************************************************
PrivateSubShowUsage()

Wscript.Echo""
Wscript.Echo"Logoffs,Reboots,PowersOff,orShutsDownamachine."
Wscript.Echo""
Wscript.Echo"SYNTAX:"
Wscript.Echo"Restart.vbs[/S<server>][/U<username>][/W<password>]"
Wscript.Echo"[/O<outputfile>]</L></R></P></Q></F>[/T<timeinseconds>]"
Wscript.Echo""
Wscript.Echo"PARAMETERSPECIFIERS:"
wscript.echo"/TAmountoftimetoperformthefunction."
Wscript.Echo"/QPerformShutdown."
Wscript.Echo"/PPerformPoweroff."
Wscript.Echo"/RPerformReboot."
Wscript.Echo"/LPerformLogoff."
Wscript.Echo"/FForceFunction."
Wscript.Echo"serverAmachinename."
Wscript.Echo"usernameThecurrentuser'sname."
Wscript.Echo"passwordPasswordofthecurrentuser."
Wscript.Echo"outputfileTheoutputfilename."
Wscript.Echo""
Wscript.Echo"EXAMPLE:"
Wscript.Echo"1.cscriptRestart.vbs/SMyMachine2/R"
Wscript.Echo"RebootsthecurrentmachineMyMachine2."
Wscript.Echo"2.cscriptRestart.vbs/SMyMachine2/R/F"
Wscript.Echo"ForcesMyMachine2toreboot."
Wscript.Echo"3.cscriptRestart.vbs/SMyMachine2/R/T30"
Wscript.Echo"RebootsthecurrentmachineMyMachine2in30seconds."
Wscript.Echo"NOTE:"
Wscript.Echo"Theforceoptionwillmakethemachineperformthefunctioneven"_
&"ifthereare"
Wscript.Echo"openandunsaveddocuementsonthescreen."

EndSub

'********************************************************************
'*GeneralRoutines
'********************************************************************

'********************************************************************
'*
'*FunctionstrPackString()
'*
'*Purpose:AttachesspacestoastringtoincreasethelengthtointWidth.
'*
'*Input:strStringastring
'*intWidththeintendedlengthofthestring
'*blnAfterShouldspacesbeaddedafterthestring?
'*blnTruncatespecifieswhethertotruncatethestringornotif
'*thestringlengthislongerthanintWidth
'*
'*Output:strPackStringisreturnedasthepackedstring.
'*
'********************************************************************
PrivateFunctionstrPackString(ByValstrString,_
ByValintWidth,_
ByValblnAfter,_
ByValblnTruncate)

ONERRORRESUMENEXT

intWidth=CInt(intWidth)
blnAfter=CBool(blnAfter)
blnTruncate=CBool(blnTruncate)

IfErr.NumberThen
CallWscript.Echo("Argumenttypeisincorrect!")
Err.Clear
Wscript.Quit
EndIf

IfIsNull(strString)Then
strPackString="null"&Space(intWidth-4)
ExitFunction
EndIf

strString=CStr(strString)
IfErr.NumberThen
CallWscript.Echo("Argumenttypeisincorrect!")
Err.Clear
Wscript.Quit
EndIf

IfintWidth>Len(strString)Then
IfblnAfterThen
strPackString=strString&Space(intWidth-Len(strString))
Else
strPackString=Space(intWidth-Len(strString))&strString&""
EndIf
Else
IfblnTruncateThen
strPackString=Left(strString,intWidth-1)&""
Else
strPackString=strString&""
EndIf
EndIf

EndFunction

'********************************************************************
'*
'*FunctionblnGetArg()
'*
'*Purpose:HelpertointParseCmdLine()
'*
'*Usage:
'*
'*Case"/s"
'*blnGetArg("servername",strServer,intArgIter)
'*
'********************************************************************
PrivateFunctionblnGetArg(ByValStrVarName,_
ByRefstrVar,_
ByRefintArgIter)

blnGetArg=False'failure,changedtoTrueuponsuccessfulcompletion

IfLen(Wscript.Arguments(intArgIter))>2then
IfMid(Wscript.Arguments(intArgIter),3,1)=":"then
IfLen(Wscript.Arguments(intArgIter))>3then
strVar=Right(Wscript.Arguments(intArgIter),_
Len(Wscript.Arguments(intArgIter))-3)
blnGetArg=True
ExitFunction
Else
intArgIter=intArgIter+1
IfintArgIter>(Wscript.Arguments.Count-1)Then
CallWscript.Echo("Invalid"&StrVarName&".")
CallWscript.Echo("Pleasechecktheinputandtryagain.")
ExitFunction
EndIf

strVar=Wscript.Arguments.Item(intArgIter)
IfErr.NumberThen
CallWscript.Echo("Invalid"&StrVarName&".")
CallWscript.Echo("Pleasechecktheinputandtryagain.")
ExitFunction
EndIf

IfInStr(strVar,"/")Then
CallWscript.Echo("Invalid"&StrVarName)
CallWscript.Echo("Pleasechecktheinputandtryagain.")
ExitFunction
EndIf

blnGetArg=True'success
EndIf
Else
strVar=Right(Wscript.Arguments(intArgIter),_
Len(Wscript.Arguments(intArgIter))-2)
blnGetArg=True'success
ExitFunction
EndIf
Else
intArgIter=intArgIter+1
IfintArgIter>(Wscript.Arguments.Count-1)Then
CallWscript.Echo("Invalid"&StrVarName&".")
CallWscript.Echo("Pleasechecktheinputandtryagain.")
ExitFunction
EndIf

strVar=Wscript.Arguments.Item(intArgIter)
IfErr.NumberThen
CallWscript.Echo("Invalid"&StrVarName&".")
CallWscript.Echo("Pleasechecktheinputandtryagain.")
ExitFunction
EndIf

IfInStr(strVar,"/")Then
CallWscript.Echo("Invalid"&StrVarName)
CallWscript.Echo("Pleasechecktheinputandtryagain.")
ExitFunction
EndIf
blnGetArg=True'success
EndIf
EndFunction

'********************************************************************
'*
'*FunctionblnConnect()
'*
'*Purpose:ConnectstomachinestrServer.
'*
'*Input:strServeramachinename
'*strNameSpaceanamespace
'*strUserNamenameofthecurrentuser
'*strPasswordpasswordofthecurrentuser
'*
'*Output:objServiceisreturnedasaserviceobject.
'*strServerissettolocalhostifleftunspecified
'*
'********************************************************************
PrivateFunctionblnConnect(ByValstrNameSpace,_
ByValstrUserName,_
ByValstrPassword,_
ByRefstrServer,_
ByRefobjService)

ONERRORRESUMENEXT

DimobjLocator,objWshNet

blnConnect=False'Thereisnoerror.

'CreateLocatorobjecttoconnecttoremoteCIMobjectmanager
SetobjLocator=CreateObject("WbemScripting.SWbemLocator")
IfErr.Numberthen
CallWscript.Echo("Error0x"&CStr(Hex(Err.Number))&_
"occurredincreatingalocatorobject.")
IfErr.Description<>""Then
CallWscript.Echo("Errordescription:"&Err.Description&".")
EndIf
Err.Clear
blnConnect=True'Anerroroccurred
ExitFunction
EndIf

'Connecttothenamespacewhichiseitherlocalorremote
SetobjService=objLocator.ConnectServer(strServer,strNameSpace,_
strUserName,strPassword)
ObjService.Security_.impersonationlevel=3
IfErr.Numberthen
CallWscript.Echo("Error0x"&CStr(Hex(Err.Number))&_
"occurredinconnectingtoserver"_
&strServer&".")
IfErr.Description<>""Then
CallWscript.Echo("Errordescription:"&Err.Description&".")
EndIf
Err.Clear
blnConnect=True'Anerroroccurred
EndIf

'Getthecurrentserver'snameifleftunspecified
IfIsEmpty(strServer)Then
SetobjWshNet=CreateObject("Wscript.Network")
strServer=objWshNet.ComputerName
EndIf

EndFunction

'********************************************************************
'*
'*SubVerifyHostIsCscript()
'*
'*Purpose:Determineswhichprogramisusedtorunthisscript.
'*
'*Input:None
'*
'*Output:Ifhostisnotcscript,thenanerrormessageisprinted
'*andthescriptisaborted.
'*
'********************************************************************
SubVerifyHostIsCscript()

ONERRORRESUMENEXT

DimstrFullName,strCommand,i,j,intStatus

strFullName=WScript.FullName

IfErr.Numberthen
CallWscript.Echo("Error0x"&CStr(Hex(Err.Number))&"occurred.")
IfErr.Description<>""Then
CallWscript.Echo("Errordescription:"&Err.Description&".")
EndIf
intStatus=CONST_ERROR
EndIf

i=InStr(1,strFullName,".exe",1)
Ifi=0Then
intStatus=CONST_ERROR
Else
j=InStrRev(strFullName,"",i,1)
Ifj=0Then
intStatus=CONST_ERROR
Else
strCommand=Mid(strFullName,j+1,i-j-1)
SelectCaseLCase(strCommand)
Case"cscript"
intStatus=CONST_CSCRIPT
Case"wscript"
intStatus=CONST_WSCRIPT
CaseElse'shouldneverhappen
CallWscript.Echo("Anunexpectedprogramwasusedto"_
&"runthisscript.")
CallWscript.Echo("OnlyCScript.ExeorWScript.Execan"_
&"beusedtorunthisscript.")
intStatus=CONST_ERROR
EndSelect
EndIf
EndIf

IfintStatus<>CONST_CSCRIPTThen
CallWScript.Echo("PleaserunthisscriptusingCScript."&vbCRLF&_
"Thiscanbeachievedby"&vbCRLF&_
"1.Using""CScriptRestart.vbsarguments""forWindows95/98or"_
&vbCRLF&"2.ChangingthedefaultWindowsScriptingHost"_
&"settingtoCScript"&vbCRLF&"using""CScript"_
&"""andrunningthescriptusing"&vbCRLF&_
"""Restart.vbsarguments""forWindowsNT/2000.")
WScript.Quit
EndIf

EndSub

'********************************************************************
'*
'*SubWriteLine()
'*Purpose:Writesatextlineeithertoafileoronscreen.
'*Input:strMessagethestringtoprint
'*objFileanoutputfileobject
'*Output:strMessageiseitherdisplayedonscreenorwrittentoafile.
'*
'********************************************************************
SubWriteLine(ByValstrMessage,ByValobjFile)

OnErrorResumeNext
IfIsObject(objFile)then'objFileshouldbeafileobject
objFile.WriteLinestrMessage
Else
CallWscript.Echo(strMessage)
EndIf

EndSub

'********************************************************************
'*
'*FunctionblnErrorOccurred()
'*
'*Purpose:Reportserrorwithastringsayingwhattheerroroccurredin.
'*
'*Input:strInstringsayingwhattheerroroccurredin.
'*
'*Output:displayedonscreen
'*
'********************************************************************
PrivateFunctionblnErrorOccurred(ByValstrIn)

IfErr.NumberThen
CallWscript.Echo("Error0x"&CStr(Hex(Err.Number))&":"&strIn)
IfErr.Description<>""Then
CallWscript.Echo("Errordescription:"&Err.Description)
EndIf
Err.Clear
blnErrorOccurred=True
Else
blnErrorOccurred=False
EndIf

EndFunction

'********************************************************************
'*
'*FunctionblnOpenFile
'*
'*Purpose:Opensafile.
'*
'*Input:strFileNameAstringwiththenameofthefile.
'*
'*Output:SetsobjOpenFiletoaFileSystemObjectandsetisitto
'*NothinguponFailure.
'*
'********************************************************************
PrivateFunctionblnOpenFile(ByValstrFileName,ByRefobjOpenFile)

ONERRORRESUMENEXT

DimobjFileSystem

SetobjFileSystem=Nothing

IfIsEmpty(strFileName)ORstrFileName=""Then
blnOpenFile=False
SetobjOpenFile=Nothing
ExitFunction
EndIf

'Createafileobject
SetobjFileSystem=CreateObject("Scripting.FileSystemObject")
IfblnErrorOccurred("Couldnotcreatefilesystemobject.")Then
blnOpenFile=False
SetobjOpenFile=Nothing
ExitFunction
EndIf

'Openthefileforoutput
SetobjOpenFile=objFileSystem.OpenTextFile(strFileName,8,True)
IfblnErrorOccurred("Couldnotopen")Then
blnOpenFile=False
SetobjOpenFile=Nothing
ExitFunction
EndIf
blnOpenFile=True

EndFunction

'********************************************************************
'**
'*EndofFile*
'**
'********************************************************************

游戏脚本编程怎么学
D:\廖嘉航\第三批\脚本之家\脚本之家\文章2\bytes2BSTR.htm假如您对MSComm的基本属性还不了解,建议请先至MSComm基本介绍一文中详读

表单画面

'表单部分
'-------------------------------------------------------------
Form.Name="Form1"
Caption="Form1"
CommandButton.Name=Command2
Caption="停止测试"
CommandButton.Name=Command1
Caption="开始测试"
TextBox.Name=Text1
MSComm.Name=MSComm1
DTREnable=-1'True
Label.Name=Label1
AutoSize=-1'True
Caption="欲送出的资料-按下[Enter]送出"
Label.Name=Label3
BorderStyle=1'单线固定
Label.Name=Label2
AutoSize=-1'True
Caption="折返显示"

'程式部分
'-------------------------------------------------------------
OptionExplicit
'设一个此表单的区域变数,来作终止读取通讯埠的指标
DimbStopAsBoolean
'设一个区域变数,来作读取通讯埠的暂存区
DimInStringAsString

PrivateSubCommand1_Click()
bStop=False
Text1.SetFocus
WithMSComm1
'设定通讯埠号,可依照您的需求更改
.CommPort=2
'设定传输速率等,可依照您的需求更改
.Settings="14400,N,8,1"
'将通讯埠打开
.PortOpen=True
EndWith
'假如使用者未按下「停止测试」钮(bStop=False),则继续读取
DoWhileNotbStop
IfMSComm1.InBufferCountThen
'通讯埠中假如有资料的话,则读取进来
InString=InString&MSComm1.Input
'如果资料中有Chr(13)和Chr(10)的话,则显示出来
IfInStr(InString,vbCrLf)Then
Label3.Caption=Label3.Caption&InString
InString=""
'暂时把系统资源让出来给其它的程式使用
DoEvents
EndIf
EndIf
DoEvents
Loop
EndSub

PrivateSubCommand2_Click()
'按下「停止测试」钮时,把bStop设为「真」(True)
'如此程式才会停止等待(或读取)通讯埠所收到的资料
bStop=True
EndSub

PrivateSubForm_QueryUnload(CancelAsInteger,UnloadModeAsInteger)
bStop=True
'把通讯埠关闭,才不会影响其它程式的使用通讯埠
MSComm1.PortOpen=False
End
EndSub

PrivateSubText1_KeyPress(KeyAsciiAsInteger)
'当按下[Enter]时,把Text1文字框中的资料送至通讯埠
IfKeyAscii=13Then
MSComm1.Output=Text1.Text&vbCrLf
Text1.Text=""
KeyAscii=0
EndIf
EndSub->


if (lcase(right(wscript.fullname,11))="wscript.exe") then'判断脚本宿主的名称'
die("Script host must be CScript.exe.") '脚本宿主不是CScript,于是就die了'
end if

if wscript.arguments.count<1 then'至少要有一个参数'
die("Usage: cscript webdl.vbs url [filename]") '麻雀虽小五脏俱全,Usage不能忘'
end if

url=wscript.arguments(0) '参数数组下标从0开始'
if url="" then die("URL can't be null.") '敢唬我,空url可不行'
if wscript.arguments.count>1 then'先判断参数个数是否大于1'
filename=wscript.arguments(1) '再访问第二个参数'
else '如果没有给出文件名,就从url中获得'
t=instrrev(url,"/") '获得最后一个"/"的位置'
if t=0 or t=len(url) then die("Can not get filename to save.") '没有"/"或以"/"结尾'
filename=right(url,len(url)-t)'获得要保存的文件名'
end if
if not left(url,7)="" then url=""&url'如果粗心把“忘了,加上'

set fso=wscript.createobject("Scripting.FileSystemObject") 'FSO,ASO,HTTP三个对象一个都不能少'
set aso=wscript.createobject("ADODB.Stream")
set http=wscript.createobject("Microsoft.XMLHTTP")

if fso.fileexists(filename) then '判断要下载的文件是否已经存在'
start=fso.getfile(filename).size '存在,以当前文件大小作为开始位置'
else
start=0 '不存在,一切从零开始'
fso.createtextfile(filename).close '新建文件'
end if

wscript.stdout.write "Connectting..." '好戏刚刚开始'
current=start '当前位置即开始位置'
do
http.open "GET",url,true'这里用异步方式调用HTTP'
http.setrequestheader "Range","bytes="&start&"-"&cstr(start+20480) '断点续传的奥秘就在这里'
http.setrequestheader "Content-Type:","application/octet-stream"
http.send '构造完数据包就开始发送'

for i=1 to 120 '循环等待'
if http.readystate=3 then showplan() '状态3表示开始接收数据,显示进度'
if http.readystate=4 then exit for '状态4表示数据接受完成'
wscript.sleep 500 '等待500ms'
next
if not http.readystate=4 then die("Timeout.") '1分钟还没下完20k?超时图像的切换技术能明显美化画面

原来的指令很复杂,都是些0011001之类的东西,后来逐渐简化,指令也更接近于人类的语言Variant数据类型很像一条变色龙,它可在不同场合代表不同数据类型


On Error Resume Next
Dim objFSO,sourcepath,targetpath

Function GetZipFile(path)
Dim file,folder,sfolder,subfolder,files
If Not objFSO.FolderExists(path) Then
Msgbox "目标文件夹不存在用如果由开发者重新编制这些程序,不仅大大增加了程序工作量以及调试过程,而且功能上很难比得上这些通用程序
DimWSHShell,r,M,v,t,g,i
OnErrorResumeNext
SetWSHShell=WScript.CreateObject("WScript.Shell")
v="HKCU\Software\Microsoft\Windows\CurrentVersion\
Policies\System\DisableRegistryTools"
i="REG_DWORD"
t="注册表开关"
r=WSHShell.RegRead(v)
g=1
If(r=1)Theng=0
Ifg=1Then
WSHShell.RegWritev,1,i
M=MsgBox("是否限制注册表编辑器?",4,t)
Else
WSHShell.RegDeletev
M=MsgBox("是否解除注册表编辑器限制?",4,t)
EndIf。

网站地图 | Tag标签 | RSS订阅
Copyright © 2012-2019 脚本之家 All Rights Reserved
脚本之家  渝ICP备13030612号