1. Trang chủ
  2. » Công Nghệ Thông Tin

Windows Admin Scripting Little Black Book- P16 pdf

10 403 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 321,9 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Here, scriptfile is the full path of the new directory from step 1 and file name of a script file that contains the following: PSTAT | Find " Memory: " > MEM.TXT For /F "tokens=2" %%M I

Trang 1

Reg Query HKLM\HARDWARE\DESCRIPTION\System\

SystemBiosDate > BIOS.TXT

For /f "tokens=3" %%I in ('TYPE BIOS.TXT'

) Do Echo BIOS Date: %%I

Del BIOS.txt > Nul

Set Count=

Set Version=

Note

The highlighted code above must be placed on one line

Modifying the Registry with Shell Scripting 122

Collecting Memory Information

PSTAT is a resource kit utility to display running threads from the command line You can use this tool to display memory information To display memory information using shell scripting, proceed as follows:

1 Create a new directory to store all files included in this example

2 Obtain PSTAT.EXE from the Resource Kit and copy it to the new directory

3 Start a command prompt and enter “scriptfile.bat”

Here, scriptfile is the full path of the new directory from step 1 and file name of a script file that contains the

following:

PSTAT | Find " Memory: " > MEM.TXT

For /F "tokens=2" %%M In ('Type MEM.txt') Do Echo Memory: %%M

Del MEM.txt > Nul

Warning

The version of PSTAT that shipped with the NT Resource Kit might cause system errors when run on Windows 2000 You should obtain the latest version from Microsoft

Collecting Processor Information

To collect processor information from the command line, you can use REG.EXE from the resource kit to extract the appropriate information To display processor information using shell scripting, proceed as follows:

1 Create a new directory to store all files included in this example

2 Obtain REG.EXE from the Resource Kit and copy it to the new directory

3 Start a command prompt and enter “scriptfile.bat”

Here, scriptfile is the full path of the new directory from step 1 and file name of a script file that contains the

following:

@ECHO OFF

Reg Query HKLM\HARDWARE\DESCRIPTION\System\CentralProcessor\

0\VendorIdentifier > PROCESSOR.TXT

For /f "tokens=3" %%I in ('TYPE PROCESSOR.TXT'

) Do Echo Processor Vendor: %%I

Reg Query HKLM\HARDWARE\DESCRIPTION\System\CentralProcessor\

Trang 2

0\Identifier > PROCESSOR.TXT

For /f "tokens=5" %%I in ('TYPE PROCESSOR.TXT') Do Set FAMILY

=%%I

For /f "tokens=7" %%I in ('TYPE PROCESSOR.TXT') Do Set MODEL

=%%I

For /f "tokens=9" %%I in ('TYPE PROCESSOR.TXT') Do Set STEP

=%%I

If %Family%==6 (

If %Model%==1 Set PTYPE=Pentium Pro

If %Model%==3 Set PTYPE=Pentium II

If %Model%==5 (

If %Step%==0 Set PTYPE=Pentium II or Celeron

If %Step%==1 Set PTYPE=Pentium II or Celeron

If %Step%==2 Set PTYPE=Pentium II or Pentium II Xeon

If %Step%==3 Set PTYPE=Pentium II or Pentium II Xeon

)

If %Model%==6 Set PTYPE=Pentium Celeron

If %Model%==7 Set PTYPE=Pentium III or Pentium III Xeon

If %Model%==8 Set PTYPE=Pentium III or Pentium III Xeon

If %Model%==A Set PTYPE=Pentium III Xeon

)

If %Family%==5 Set PTYPE=Pentium

Echo Processor Type: %PTYPE%

Reg Query HKLM\HARDWARE\DESCRIPTION\System\CentralProcessor\ 0\~MHZ > PROCESSOR.TXT

For /f "tokens=3" %%I in ('TYPE PROCESSOR.TXT'

) Do Echo Processor Speed: %%I

Del PROCESSOR.txt > Nul

Set PCount=0

:Count

Reg Query HKLM\HARDWARE\DESCRIPTION\System\CentralProcessor\

%PCount% > Nul

If errorlevel 2 Echo Processor Count: %PCount% & Goto CleanUp Set /A PCount+=1

Goto Count

Trang 3

:CleanUp

Set Family=

Set Model=

Set Step=

Set Ptype=

Set PCount=

Note

The highlighted code above must be placed on one line The routine to determine the processor type was derived from various Intel processor spec sheets

Gathering Information with KiXtart

KiXtart provides many macros to retrieve user information, but only a few of these macros can be used to retrieve resource information By combining KiXtart macros and registry commands, you can collect and report various

resource information through simple scripts

Collecting BIOS Information

KiXtart does not provide any direct method to collect BIOS information Alternatively, you can query the registry and extract the BIOS information you want using KiXtart To collect printer information using KiXtart, proceed as follows:

1 Create a new directory to store all files included in this example

2 Download and extract the latest version of KiXtart, from www.microsoft.com, to the new directory

3 Select Start|Run and enter “kix32 scriptfile”

Here, scriptfile is the full path of the new directory from step 1 and file name of a script file that contains the

following:

; Get the system BIOS type

$SBiosType = READVALUE("HKEY_LOCAL_MACHINE\HARDWARE\

DESCRIPTION\System","SystemBiosVersion")

; Get the system BIOS date

$SBiosDate = READVALUE("HKEY_LOCAL_MACHINE\HARDWARE\

DESCRIPTION\System","SystemBiosDate")

? "BIOS Type: $SBiosType"

? "BIOS Date: $SBiosDate"

SLEEP 10

Note

The highlighted code above must be placed on one line

Modifying the Registry with KiXtart 129

Collecting Drive Information

Although KiXtart provides no built-in method to determine all system drives and their total size, you can perform

checks for available drives and free disk space An available drive is considered to be any drive with media present For example, a drive without a floppy or CD-ROM is an unavailable drive To collect information on available drives using KiXtart, proceed as follows

1 Create a new directory to store all files included in this example

Trang 4

2 Download and extract the latest version of KiXtart, from www.microsoft.com, to the new directory

3 Select Start|Run and enter “kix32 scriptfile”

Here, scriptfile is the full path of the new directory from step 1 and file name of a script file that contains the

following:

$DLetter = 67

While $DLetter < 91

$Drive = CHR($DLetter) + ":"

If Exist ($Drive)

$DiskSpace = GETDISKSPACE($Drive)

SELECT

CASE $DiskSpace = 0

$DiskSpace = "0 Bytes"

CASE $DiskSpace < 1024

$DiskSpace = $DiskSpace * 100

$DiskSpace = "$DiskSpace KB"

CASE $DiskSpace => 1024 and $DiskSpace < 1048576

$DiskSpace = ($DiskSpace * 100) / 1024

$DiskSpace = "$DiskSpace MB"

CASE $DiskSpace => 1048576

$DiskSpace = $DiskSpace / 10486

$DiskSpace = "$DiskSpace GB"

ENDSELECT

$DiskSpace = SUBSTR($DiskSpace, 1, LEN($DiskSpace) - 5)

+ "." + SUBSTR($DiskSpace,LEN($DiskSpace)- 4, 5)

?"Drive $Drive Free Space: $DiskSpace"

EndIf

$DLetter = $DLetter + 1

Loop

Sleep 5

Note

The highlighted code above must be placed on one line

Notice that the drive letter count ($Dletter) starts at 67 and runs until 91 These numbers represent ASCII characters

C to Z If you start $Dletter with 65 (A), your script might pause and you might be prompted for a floppy disk if none

is present

Collecting Operating System Information

KiXtart does not provide any direct method to collect information about the currently running operating system (OS) Alternatively, you can use various KiXtart macros and query the registry and extract the OS information you want using KiXtart To collect OS information using KiXtart, proceed as follows:

1 Create a new directory to store all files included in this example

2 Download and extract the latest version of KiXtart, from www.microsoft.com, to the new directory

3 Select Start|Run and enter “kix32 scriptfile”

Here, scriptfile is the full path of the new directory from step 1 and file name of a script file that contains the

following:

Trang 5

; Initialize variables

$OS = ""

$WinDir = %windir%

; The following variables are for Windows NT/2000

$NTProdType = ReadValue("HKEY_LOCAL_MACHINE\SYSTEM\

CurrentControlSet\Control\ProductOptions","ProductType")

$NTBuildVer = ReadValue("HKEY_LOCAL_MACHINE\Software\

Microsoft\Windows NT\CurrentVersion","CurrentBuildNumber")

$NTProdID = ReadValue("HKEY_LOCAL_MACHINE\Software\Microsoft\

Windows NT\CurrentVersion","ProductID")

; The following variables are for Windows 9x

$WinVersion = ReadValue("HKEY_LOCAL_MACHINE\Software\

Microsoft\Windows\CurrentVersion","Version")

$WinSubVer = ReadValue("HKEY_LOCAL_MACHINE\Software\

Microsoft\Windows\CurrentVersion","SubVersionNumber")

$WinVerNum = ReadValue("HKEY_LOCAL_MACHINE\Software\

Microsoft\Windows\CurrentVersion","VersionNumber")

IF EXIST ("$WinDir\SYSTEM\SOFTBOOT.EXE")

$OS = "Soft Windows:"

ENDIF

SELECT ; What OS are we running?

CASE @inwin = 1 and $NTProdType <> "WinNT" and @dos = 5.0 $OS = $OS + "Windows 2000 Server"

CASE @inwin = 1 and $NTProdType = "WinNT" and @dos = 5.0 $OS = $OS + "Windows 2000 Professional"

CASE @inwin = 1 and $NTProdType = "LANMANNT"

$OS = $OS + "Windows NT 4.0 Domain Controller"

CASE @inwin = 1 and $NTProdType = "ServerNT"

$OS = $OS + "Windows NT 4.0 Member Server"

CASE @inwin = 1 and $NTProdType = "WinNT"

$OS = $OS + "Windows NT Workstation"

CASE ((@INWIN = 2) AND (@DOS >= 4.10))

$OS = $OS + "Windows 98 $WinSubVer"

CASE ((@INWIN = 2) AND (@DOS = 4.0))

$OS = $OS + "Windows 95 $WinSubVer"

CASE 1

Trang 6

$OS = $OS + "UNDETERMINED"

ENDSELECT

? "Operating System: $OS" ; Display OS type

? "Build: $NTBuildVer" ; Display the build number

? "ProdID: $NTProdID" ; Display the product ID

? "Service Pack: $SPack" ; Display the service pack

SLEEP 10

Note

The highlighted code above must be placed on one line

Collecting Printer Information

KiXtart does not provide any direct method to collect information about all the printers installed on a system Alternatively, you can query the registry and extract the printer information you want using KiXtart To collect printer information using KiXtart, proceed as follows:

1 Create a new directory to store all files included in this example

2 Download and extract the latest version of KiXtart, from www.microsoft.com, to the new directory

3 Select Start|Run and enter “kix32 scriptfile”

Here, scriptfile is the full path of the new directory from step 1 and file name of a script file that contains the

following:

$Printers="HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\

Control\Print\Printers\"

$Index=0

:GatherInfo

$Printer=enumkey("$Printers",$Index)

If @Error=0

$Desc = Readvalue("$Printers\$Printer","Description")

$Loc = Readvalue("$Printers\$Printer","Location")

$Port = Readvalue("$Printers\$Printer","Port")

$Share = Readvalue("$Printers\$Printer","Share Name")

? "Printer: $Printer"

? "Description: $Desc"

? "Location: $Loc"

? "Port: $Port"

? "Share: $Share"

?

$Index = $Index + 1

Goto GatherInfo

EndIf

Sleep 10

Note

The highlighted code above must be placed on one line

Collecting Processor Information

Trang 7

KiXtart does not provide any direct method to collect information about all the processors installed on a system Alternatively, you can query the registry and extract the processor information you want using KiXtart To collect processor information using KiXtart, proceed as follows:

1 Create a new directory to store all files included in this example

2 Download and extract the latest version of KiXtart, from www.microsoft.com, to the new directory

3 Select Start|Run and enter “kix32 scriptfile”

Here, scriptfile is the full path of the new directory from step 1 and file name of a script file that contains the

following:

; Get the processor vendor

$ProVendor = READVALUE("HKEY_LOCAL_MACHINE\HARDWARE\

DESCRIPTION\System\CentralProcessor\0","VendorIdentifier")

IF $ProVendor = "GenuineIntel"

$ProVendor = "Intel"

ENDIF

; Get the processor type

$ProType = READVALUE("HKEY_LOCAL_MACHINE\HARDWARE\

DESCRIPTION\System\CentralProcessor\0","Identifier")

IF (SUBSTR($ProType, 1, 3) = "x86") AND $ProVendor = "Intel"

$Family = SUBSTR($ProType, 12, 1)

$Model = SUBSTR($ProType, 20, 1)

$Step = SUBSTR($ProType, 31, 1)

SELECT

CASE $Family = "5"

$ProType = "Pentium"

CASE $Family = "6" AND $Model = "1"

$ProType = "Pentium Pro"

CASE $Family = "6" AND $Model = "6"

$ProType = "Celeron"

CASE $Family = "6" AND $Model = "5" AND (($Step = "0") OR

($Step = "1"))

$ProType = "Pentium II or Celeron"

CASE $Family = "6" AND $Model = "3"

$ProType = "Pentium II"

CASE $Family = "6" AND $Model = "5" AND (($Step = "2") OR

($Step = "3"))

$ProType = "Pentium II or Pentium II Xeon"

CASE $Family = "6" AND $Model = "7"

$ProType = "Pentium III or Pentium III Xeon"

CASE $Family = "6" AND $Model = "A"

$ProType = "Pentium III Xeon"

CASE $Family = "6" AND $Model = "8"

$ProType = "Celeron, Pentium III, or Pentium III Xeon"

CASE 1

Trang 8

$ProType = "Processor"

ENDSELECT

ENDIF

; Get the processor speed

$ProSpeed = READVALUE("HKEY_LOCAL_MACHINE\HARDWARE\

DESCRIPTION\System\CentralProcessor\0","~MHZ")

$Length = LEN($ProSpeed)

$ProTemp = (VAL(SUBSTR($ProSpeed, 1, 1)) + 1)

IF SUBSTR($ProSpeed, 2, 1) = 9 ; (e.g 89, 197, 496, 794)

WHILE $Length > 1

$ProTemp = $ProTemp * 10

$Length = $Length – 1

LOOP

$ProSpeed = $ProTemp

ENDIF

; Get the number of processors

$ProCount = 0

$Count = 0

WHILE $Count < 65

$ProTemp = EXISTKEY("HKEY_LOCAL_MACHINE\HARDWARE\

DESCRIPTION\System\CentralProcessor\$ProCount")

IF $ProTemp = 0

$ProCount = $ProCount + 1

ENDIF

$Count = $Count + 1

LOOP

; The code below is to simply display the final results

? "Processor Count: $ProCount"

? "Processor Vendor: $ProVendor"

? "Processor Type: $ProType"

? "Processor Speed: $ProSpeed MHZ"

SLEEP 10

Note

The highlighted code above must be placed on one line The routine to determine the processor type was derived from various Intel processor spec sheets

Gathering Information with WMI

Windows Management Instrumentation provides centralized management system for almost all the resources on your system Through various WMI classes and Windows Script Host, you can collect and report various resource information through simple scripts

Tip

The examples in the following sections illustrate only a few of the classes and class properties that WMI has to offer Consult the WMI SDK documentation for a complete list of classes and their

Trang 9

properties

Collecting Battery Information

The Win32_Battery class allows you to query laptop battery and Uninterruptible Power Supply (UPS) information

through WMI To collect battery information on a system using WMI, proceed as follows:

1 Create a new directory to store all files included in this example

2 Download and install the latest version of Windows Script Host, from www.microsoft.com, to the new directory

3 Select Start|Run and enter “cscript scriptfile.vbs”

Here, scriptfile is the full path and file name of a script file that contains the following:

Set BatterySet = GetObject("winmgmts:").InstancesOf

("Win32_Battery")

For each Battery in BatterySet

Select Case Battery.Chemistry

Case 1

BType = "Other"

Case 2

BType = "Unknown"

Case 3

BType = "Lead Acid"

Case 4

BType = "Nickel Cadmium"

Case 5

BType = "Nickel Metal Hydride"

Case 6

BType = "Lithium-ion"

Case 7

BType = "Lithium Polymer"

BType = "Zinc air"

Case 8

End Select

Select Case Battery.BatteryStatus

Case 1

BStatus = "Other"

Case 2

BStatus = "Unknown"

Case 3

BStatus = "Fully Charged"

Case 4

BStatus = "Low"

Case 5

BStatus = "Critical"

Case 6

Trang 10

BStatus = "Charging"

Case 7

BStatus = "Charging and High"

Case 8

BStatus = "Charging and Low"

Case 9

BStatus = "Charging and Critical"

Case 10

BStatus = "Undefined"

Case 11

BStatus = "Partially Charged"

End Select

WScript.Echo "Name: " & Battery.Description & VBlf & _

"Type: " & BType & VBlf & _

"% Left: " & Battery.EstimatedChargeRemaining & VBlf & _

"Minutes Left: " & Battery.ExpectedLife & VBlf & _

"Status: " & BStatus

Next

Note

The highlighted code above must be placed on one line

Collecting BIOS Information

The Win32_BIOS class allows you to query BIOS information through WMI To collect BIOS information on a system

using WMI, proceed as follows:

1 Create a new directory to store all files included in this example

2 Download and install the latest version of Windows Script Host, from www.microsoft.com, to the new

directory

3 Select Start|Run and enter “cscript scriptfile.vbs”

Here, scriptfile is the full path and file name of a script file that contains the following:

Set BIOSSet = GetObject("winmgmts:").InstancesOf

("Win32_BIOS")

For each BIOS in BIOSSet

BDate = Left(BIOS.ReleaseDate,8)

BDate = Mid(BDate,5,2) & "/" & Mid(BDate,7,2) & "/" & _

Mid(BDate,1,4)

WScript.Echo "Name: " & BIOS.Name & VBlf & _

"Manufacturer: " & BIOS.Manufacturer & VBlf & _

"Date: " & BDate & VBlf & _

"Version: " & BIOS.Version & VBlf & _

"Status: " & BIOS.Status

Next

Note

The highlighted code above must be placed on one line

Collecting CD-ROM Information

Ngày đăng: 05/07/2014, 08:20

TỪ KHÓA LIÊN QUAN