作者 主題: [分享] 找出哪些電腦沒關機, 該電腦是誰所用, 產生報表, 並下關機指令  (閱讀 5662 次)

0 會員 與 1 訪客 正在閱讀本文。

Dino9021

  • SA 苦力組
  • 憂鬱的高中生
  • ***
  • 文章數: 175
    • 檢視個人資料
花了一個晚上寫了這個小小程式
迫不及待與大家分享...
應該還有更好的寫法, 不過累了....

存成 .vbs 就可以執行了~
最好用網域 Administrator 或可以登入網域中每一台 PC 的帳號執行
才能查出該電腦的使用者是誰 / 有哪些

代碼: [選擇]
'
' FSO 元件的開啟及關閉方法:
'
' Windows 2000
' 開啟: RegSvr32 C:\\WINNT\\SYSTEM32\\scrrun.dll
' 關閉: RegSvr32 /u C:\\WINNT\\SYSTEM32\\scrrun.dll
'
' Windows XP / 2K3
' 開啟: regsvr32 scrrun.dll
' 關閉: regsvr32 /u scrrun.dll
'
' 指令執行完畢後需登出才會生效
'
Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1, ForWriting = 2
NowDateTime=DateAdd("h",0,now())
NowDateTime=Year(NowDateTime) & "_" & Right("0" & Month(NowDateTime), 2) & "_" & Right("0" & Day(NowDateTime), 2) & "_" & Right("0" & Hour(NowDateTime), 2) & "_" & Right("0" & Minute(NowDateTime), 2) & "_" & Right("0" & Second(NowDateTime), 2)

'--------------------------------------------------------------
' 設定值

' 建立檢查未關機電腦名稱之批次檔
StepOneFileName = "C:\StepOne.bat"

' 取得未關機電腦名稱
StepTwoFileName = "C:\StepTwo.txt"

' 解析未關機電腦名稱
StepThreeFileName = "C:\StepThree.bat"

' 取得未關機電腦之使用者名稱
StepFourFileName = "C:\StepFour.txt"

' 合併未關機電腦名稱與其使用者名稱 => 報表
StepFiveFileName = "C:\PC_Online_"&NowDateTime&".txt"

' 要下指令關機之電腦
PCtoShutDownFileName = "C:\ShutDownPC.txt"

' 關機指令
PCtoShutDownExecutionFileName = "C:\ShutDownPC.bat"

' C Class 網段
IPClass="192.168.4"

' 要掃描的開始 IP
IPRangeFirst=100

' 要掃描的結尾 IP
IPRangeLast=100
'--------------------------------------------------------------

' 刪除舊的 StepFile
If objFSO.FileExists(StepOneFileName) Then
objFSO.DeleteFile(StepOneFileName)
End If

If objFSO.FileExists(StepTwoFileName) Then
objFSO.DeleteFile(StepTwoFileName)
End If

If objFSO.FileExists(StepThreeFileName) Then
objFSO.DeleteFile(StepThreeFileName)
End If

If objFSO.FileExists(StepFourFileName) Then
objFSO.DeleteFile(StepFourFileName)
End If

If objFSO.FileExists(StepFiveFileName) Then
objFSO.DeleteFile(StepFiveFileName)
End If

If objFSO.FileExists(PCtoShutDownFileName) Then
objFSO.DeleteFile(PCtoShutDownFileName)
End If

' 預先開啟結案檔案
Set StepFiveFile = objFSO.OpenTextFile(StepFiveFileName, ForWriting, True)
Set PCtoShutDownFile = objFSO.OpenTextFile(PCtoShutDownFileName, ForWriting, True)

' 建立查詢 BAT 檔
Set StepOneFile = objFSO.OpenTextFile(StepOneFileName, ForWriting, True)
StepOneFile.WriteLine "nbtstat -A %1.%2 | Find ""UNIQUE"" /I | Find ""<00>"" /I >> %3"
StepOneFile.Close

' 開始檢查未關機電腦名稱
IPNow=IPRangeFirst
While IPNow <= IPRangeLast
objShell.Run(StepOneFileName&" "&IPClass&" "&IPNow&" "&StepTwoFileName),1,True
IPNow=IPNow+1
Wend
objFSO.DeleteFile(StepOneFileName)

' 開始解析未關機電腦名稱
If objFSO.FileExists(StepTwoFileName) Then

Set StepTwoFile = objFSO.OpenTextFile(StepTwoFileName, ForReading, False)

While StepTwoFile.AtEndOfStream <> True

' 從 StepTwoFileName 中解析出單純的電腦名稱
StepTwoFileNameTempArray = Split(StepTwoFile.ReadLine, "    ")

' 並建立查詢該電腦使用者之批次檔 (StepFourFileName) , 電腦名稱為 StepTwoFileNameTempArray(1)
Set StepThreeFile = objFSO.OpenTextFile(StepThreeFileName, ForWriting, True)
StepThreeFile.WriteLine "dir ""\\"&StepTwoFileNameTempArray(1)&"\C$\Documents and Settings"" | Find ""<DIR>          "" /I | Find ""Default User"" /V /I | Find ""All Users"" /V /I | Find "">          ."" /V /I | Find ""Administrator"" /V /I | Find ""磁碟區"" /V /I | Find ""目錄"" /V /I | Find ""位元"" /V /I >> "&StepFourFileName
StepThreeFile.Close

' 合併電腦與使用者資料第一步: 寫入電腦名稱
PCtoShutDownFile.WriteLine StepTwoFileNameTempArray(1)
StepFiveFile.WriteLine "電腦名稱: "&StepTwoFileNameTempArray(1)
StepFiveFile.WriteLine "--------------------"
' 合併電腦與使用者資料第一步: 寫入電腦名稱

' 執行 StepFourFileName 批次檔取得開電腦使用者名稱並存於 StepFourFileName 中
objShell.Run(StepThreeFileName),1,True

' 讀取 StepFourFileName 取得該電腦之使用者名稱
Set StepFourFile = objFSO.OpenTextFile(StepFourFileName, ForReading, False)

' 合併電腦與使用者資料第二步: 寫入使用者名稱
' 從 StepFourFileName 中解析出單純的使用者名稱
AddUser=0
While StepFourFile.AtEndOfStream <> True
StepFourFileNameTempArray = Split(StepFourFile.ReadLine, "<DIR>          ")
If ADDUser=0 Then
StepFiveFile.WriteLine "使用者: "&StepFourFileNameTempArray(1)
AddUser=1
Else
StepFiveFile.WriteLine "    "&StepFourFileNameTempArray(1)
End If
Wend
' 關閉 StepFourFileName 檔案
StepFourFile.Close
objFSO.DeleteFile(StepFourFileName)
StepFiveFile.WriteLine "========================================"
' 合併電腦與使用者資料第二步: 寫入使用者名稱
Wend
End If
StepTwoFile.Close
objFSO.DeleteFile(StepTwoFileName)
objFSO.DeleteFile(StepThreeFileName)
' 開始解析未關機電腦名稱

StepFiveFile.Close
PCtoShutDownFile.Close

Set PCtoShutDownExecutionFile = objFSO.OpenTextFile(PCtoShutDownExecutionFileName, ForWriting, True)
PCtoShutDownExecutionFile.WriteLine """C:\Program Files\PsTools\psshutdown"" -c -f -k -m ""600 秒後關機"" -n 10 -t 600 @"&PCtoShutDownFileName
PCtoShutDownExecutionFile.Close

objShell.Run(PCtoShutDownExecutionFileName),1,True
objFSO.DeleteFile(PCtoShutDownFileName)
objFSO.DeleteFile(PCtoShutDownExecutionFileName)
« 上次編輯: 2008-10-23 00:51 由 Dino9021 »
雖然我不是什麼厲害的前輩, 但除了私人問題外我不接受私人訊息問技術問題
好的/有用的資訊公開讓所有人都能知道是最好的
另外, 發問請詳述問題/Log/你曾經進行的嘗試以及結果
請先參考 http://phorum.study-area.org/index.php/topic,24128.0.html

Dino9021

  • SA 苦力組
  • 憂鬱的高中生
  • ***
  • 文章數: 175
    • 檢視個人資料
今天做了些修改, 參考看看
可以將未關機的電腦+使用者報表傳至 FTP
再用 php 讀取發出 E-mail

CheckPCOnline.vbs
代碼: [選擇]
'
' FSO 元件的開啟及關閉方法:
'
' Windows 2000
' 開啟: RegSvr32 C:\\WINNT\\SYSTEM32\\scrrun.dll
' 關閉: RegSvr32 /u C:\\WINNT\\SYSTEM32\\scrrun.dll
'
' Windows XP / 2K3
' 開啟: regsvr32 scrrun.dll
' 關閉: regsvr32 /u scrrun.dll
'
' 指令執行完畢後需登出才會生效
'
On Error Resume Next

Dim objShell
Dim objFSO

Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1, ForWriting = 2
NowDateTime=DateAdd("h",0,now())
NowDateTimeForFileName=Year(NowDateTime) & "_" & Right("0" & Month(NowDateTime), 2) & "_" & Right("0" & Day(NowDateTime), 2) & "_" & Right("0" & Hour(NowDateTime), 2) & "_" & Right("0" & Minute(NowDateTime), 2) & "_" & Right("0" & Second(NowDateTime), 2)
NowDateTimeRegular=Year(NowDateTime) & "." & Right("0" & Month(NowDateTime), 2) & "." & Right("0" & Day(NowDateTime), 2) & " " & Right("0" & Hour(NowDateTime), 2) & ":" & Right("0" & Minute(NowDateTime), 2) & ":" & Right("0" & Second(NowDateTime), 2)

'--------------------------------------------------------------
' 設定值

' 測試用=1 (不刪除暫存檔供檢查) , 實際運作=0
ScriptForTest=1

' 作業系統語言, 執行 nbtstat 後顯示的是中文還是英文? 英文=0 , 中文=1
SystemOSLanguage=0

' 建立檢查未關機電腦名稱之批次檔
StepOneFileName = "StepOne.bat"

' 取得未關機電腦名稱
StepTwoFileName = "StepTwo.txt"

' 解析未關機電腦名稱
StepThreeFileName = "StepThree.bat"

' 取得未關機電腦之使用者名稱
StepFourFileName = "StepFour.txt"

' 合併未關機電腦名稱與其使用者名稱
StepFiveFileName = "PC_Online_"&NowDateTimeForFileName&".txt"

' 供發布的檔案名稱
AnnounceFileName= "PC_Online.txt"

' 要下指令關機之電腦
PCtoShutDownFileName = "ShutDownPC.txt"

' 關機指令
PCtoShutDownExecutionFileName = "ShutDownPC.bat"

' PsShutdown.exe 所在路徑 (後面不加 "\" 符號)
PsShutdownFileDirectory="C:\Program Files\PsTools"

' FTP 上傳批次暫存檔
FTPUploadBatchFileName="FTPUploadBatch.bat"

' FTP 上傳腳本暫存檔
FTPUploadScriptFileName="FTPUploadScript.txt"

' 要搜尋的網段 C_Class
IPClass="192.168.0"

' 要搜尋的起始 IP (含)
IPRangeFirst=100

' 要搜尋的結束 IP (含)
IPRangeLast=149

' 上傳之 FTP 位址 / IP , 相關資訊
FTPServer="ftp.test.com"
FTPUser="CheckPCOnline"
FTPPWD="password"

'--------------------------------------------------------------

' 刪除舊的 StepFile
If objFSO.FileExists(StepOneFileName) Then
objFSO.DeleteFile(StepOneFileName)
End If

If objFSO.FileExists(StepTwoFileName) Then
objFSO.DeleteFile(StepTwoFileName)
End If

If objFSO.FileExists(StepThreeFileName) Then
objFSO.DeleteFile(StepThreeFileName)
End If

If objFSO.FileExists(StepFourFileName) Then
objFSO.DeleteFile(StepFourFileName)
End If

If objFSO.FileExists(StepFiveFileName) Then
objFSO.DeleteFile(StepFiveFileName)
End If

If objFSO.FileExists(PCtoShutDownFileName) Then
objFSO.DeleteFile(PCtoShutDownFileName)
End If

' 預先開啟結案檔案
PCOnlineCount=0
Set StepFiveFile = objFSO.OpenTextFile(StepFiveFileName, ForWriting, True)
Set PCtoShutDownFile = objFSO.OpenTextFile(PCtoShutDownFileName, ForWriting, True)
StepFiveFile.WriteLine ""
StepFiveFile.WriteLine "未關機電腦報表"
StepFiveFile.WriteLine ""
StepFiveFile.WriteLine "檢查開始時間: "&NowDateTimeRegular
StepFiveFile.WriteLine "========================================"
StepFiveFile.WriteLine ""

' 建立檢查未關機電腦名稱查詢 BAT 檔
Set StepOneFile = objFSO.OpenTextFile(StepOneFileName, ForWriting, True)
IPNow=IPRangeFirst
While IPNow <= IPRangeLast
If SystemOSLanguage=0 Then
StepOneFile.WriteLine "nbtstat -A "&IPClass&"."&IPNow&" | Find ""UNIQUE"" /I | Find ""<00>"" /I >> "&StepTwoFileName
ElseIf SystemOSLanguage=1 Then
StepOneFile.WriteLine "nbtstat -A "&IPClass&"."&IPNow&" | Find ""唯一"" /I | Find ""<00>"" /I >> "&StepTwoFileName
End If
IPNow=IPNow+1
Wend
StepOneFile.Close
objShell.Run(StepOneFileName),1,True

' 開始解析未關機電腦名稱
If objFSO.FileExists(StepTwoFileName) Then

Set StepTwoFile = objFSO.OpenTextFile(StepTwoFileName, ForReading, False)

While StepTwoFile.AtEndOfStream <> True

' 從 StepTwoFileName 中解析出單純的電腦名稱
StepTwoFileNameTemp = Replace(StepTwoFile.ReadLine," ","")
StepTwoFileNameTempArray = Split(StepTwoFileNameTemp, "<00>")
ComputerName=StepTwoFileNameTempArray(0)

' 建立查詢該電腦使用者之批次檔 (StepFourFileName) , 電腦名稱為 StepTwoFileNameTempArray(0) 中
Set StepThreeFile = objFSO.OpenTextFile(StepThreeFileName, ForWriting, True)

' Windows XP 檔案結構
StepThreeFile.WriteLine "dir ""\\"&ComputerName&"\C$\Documents and Settings"" | Find ""<DIR>          "" /I | Find ""Default User"" /V /I | Find ""All Users"" /V /I | Find "">          ."" /V /I | Find ""Administrator"" /V /I | Find ""磁碟區"" /V /I | Find ""目錄"" /V /I | Find ""位元"" /V /I >> "&StepFourFileName
' Windows Vista 檔案結構
StepThreeFile.WriteLine "dir ""\\"&ComputerName&"\C$\Users"" | Find ""<DIR>          "" /I | Find ""Default User"" /V /I | Find ""Default"" /V /I | Find ""Public"" /V /I | Find ""All Users"" /V /I | Find "">          ."" /V /I | Find ""Administrator"" /V /I | Find ""磁碟區"" /V /I | Find ""目錄"" /V /I | Find ""位元"" /V /I >> "&StepFourFileName

StepThreeFile.Close

' 合併電腦與使用者資料第一步: 寫入電腦名稱
PCOnlineCount=PCOnlineCount+1
PCtoShutDownFile.WriteLine ComputerName
StepFiveFile.WriteLine "電腦名稱: "&ComputerName
StepFiveFile.WriteLine "--------------------"
' 合併電腦與使用者資料第一步: 寫入電腦名稱

' 執行 StepFourFileName 批次檔取得開電腦使用者名稱並存於 StepFourFileName 中
objShell.Run(StepThreeFileName),1,True

' 讀取 StepFourFileName 取得該電腦之使用者名稱
Set StepFourFile = objFSO.OpenTextFile(StepFourFileName, ForReading, False)

' 合併電腦與使用者資料第二步: 寫入使用者名稱
' 從 StepFourFileName 中解析出單純的使用者名稱
AddUser=0
While StepFourFile.AtEndOfStream <> True
StepFourFileNameTempArray = Split(StepFourFile.ReadLine, "<DIR>          ")
If ADDUser=0 Then
StepFiveFile.WriteLine "使用者: "&StepFourFileNameTempArray(1)
AddUser=1
Else
StepFiveFile.WriteLine "    "&StepFourFileNameTempArray(1)
End If
Wend
' 關閉 StepFourFileName 檔案
StepFourFile.Close
objFSO.DeleteFile(StepFourFileName)
StepFiveFile.WriteLine "----------------------------------------"
' 合併電腦與使用者資料第二步: 寫入使用者名稱
Wend
End If
StepTwoFile.Close
' 開始解析未關機電腦名稱

StepFiveFile.WriteLine ""
DoneDateTime=DateAdd("h",0,now())
DoneDateTimeRegular=Year(DoneDateTime) & "." & Right("0" & Month(DoneDateTime), 2) & "." & Right("0" & Day(DoneDateTime), 2) & " " & Right("0" & Hour(DoneDateTime), 2) & ":" & Right("0" & Minute(DoneDateTime), 2) & ":" & Right("0" & Second(DoneDateTime), 2)
StepFiveFile.WriteLine "========================================"
StepFiveFile.WriteLine "檢查結束時間: "&DoneDateTimeRegular
StepFiveFile.WriteLine "共有 "&PCOnlineCount&" 台電腦未關機"

StepFiveFile.Close
PCtoShutDownFile.Close
' 完成未關機列表

' 如果有電腦未關機則進行強制關機與公告
If PCOnlineCount<>0 Then

' 製作強制關機指令批次檔
Set PCtoShutDownExecutionFile = objFSO.OpenTextFile(PCtoShutDownExecutionFileName, ForWriting, True)
PCtoShutDownExecutionFile.WriteLine """"&PsShutdownFileDirectory&"\psshutdown"" -c -f -k -m ""為節省資源, 此電腦將在 10 分鐘後關機, 若要繼續使用請按 [Cancle]. For Saving Resources and [[--MONEY--]]. This computer will shutdown in 10 minutes. Press [Cancel] to preceed to use it. "" -n 10 -t 600 @"&PCtoShutDownFileName
PCtoShutDownExecutionFile.Close

' 執行強制關機指令批次檔
If ScriptForTest=0 Then
objShell.Run(PCtoShutDownExecutionFileName),1,True
End If

' 製作公佈檔案
objFSO.CopyFile StepFiveFileName , AnnounceFileName , TRUE

' 製作上傳 FTP 執行批次檔
Set FTPUploadBatchFile = objFSO.OpenTextFile(FTPUploadBatchFileName, ForWriting, True)
FTPUploadBatchFile.WriteLine "ftp -v -s:"&FTPUploadScriptFileName&" "&FTPServer
FTPUploadBatchFile.Close

' 製作上傳 FTP 執行批次檔之腳本檔
Set FTPUploadScriptFile = objFSO.OpenTextFile(FTPUploadScriptFileName, ForWriting, True)
FTPUploadScriptFile.WriteLine FTPUser
FTPUploadScriptFile.WriteLine FTPPWD
FTPUploadScriptFile.WriteLine "send "&AnnounceFileName
FTPUploadScriptFile.WriteLine "quit"
FTPUploadScriptFile.Close

' 上傳 FTP
If ScriptForTest=0 Then
objShell.Run(FTPUploadBatchFileName),1,True
End If

End If

' 實際運作時刪除暫存檔案
If ScriptForTest=0 Then
objFSO.DeleteFile(StepOneFileName)
objFSO.DeleteFile(StepTwoFileName)
objFSO.DeleteFile(StepThreeFileName)
objFSO.DeleteFile(PCtoShutDownFileName)
objFSO.DeleteFile(PCtoShutDownExecutionFileName)
objFSO.DeleteFile(FTPUploadBatchFileName)
objFSO.DeleteFile(FTPUploadScriptFileName)
objFSO.DeleteFile(AnnounceFileName)
End If


CheckPCOnline.php
代碼: [選擇]
<?

$AdminEmail="Administrator@test.com";

$SMTP_Server="192.168.0.1";

ini_set ( 'SMTP', $SMTP_Server );
ini_set ( 'smtp_port', '25' );
ini_set ( 'sendmail_from', $AdminEmail);

$File="D:\WebSite\PC_Online.txt";

$NowTime=getdate();

if (strlen($NowTime['mon'])==1) {
$NowTime['mon']="0".$NowTime['mon'];
};

if (strlen($NowTime['mday'])==1) {
$NowTime['mday']="0".$NowTime['mday'];
};

if (strlen($NowTime['hours'])==1) {
$NowTime['hours']="0".$NowTime['hours'];
};

if (strlen($NowTime['minutes'])==1) {
$NowTime['minutes']="0".$NowTime['minutes'];
};

if (strlen($NowTime['seconds'])==1) {
$NowTime['seconds']="0".$NowTime['seconds'];
};

$Now=$NowTime['year'].".".$NowTime['mon'].".".$NowTime['mday']." ".$NowTime['hours'].":".$NowTime['minutes'].":".$NowTime['seconds'];

if (file_exists($File)==1) {

$fd=fopen($File,'r');
$FileContent=fread($fd,filesize($File));
$FileContentArray=split("\r\n",$FileContent);

$MessageBody="";
for ($i=0;$i<count($FileContentArray);$i++) {
$MessageBody.=$FileContentArray[$i]."<br>\r\n";
};

fclose($fd);

$MessageSubject = "未關機電腦報表 ".$Now;

$MessageHeaders = "From: CheckPCOnline <".$AdminEmail.">\r\nReply-To: ".$AdminEmail."\r\nMIME-Version: 1.0\nContent-type: text/html; charset=big5\rContent-Transfer-Encoding: 8bit";

$IsMailSucceed=mail($AdminEmail, $MessageSubject, $MessageBody, $MessageHeaders);


if ($IsMailSucceed==1) {
echo "E-mail 寄送完成!";
} else {
echo "E-mail 寄送失敗";
};

unlink($File);
};
exit;
?>
« 上次編輯: 2008-10-23 15:32 由 Dino9021 »
雖然我不是什麼厲害的前輩, 但除了私人問題外我不接受私人訊息問技術問題
好的/有用的資訊公開讓所有人都能知道是最好的
另外, 發問請詳述問題/Log/你曾經進行的嘗試以及結果
請先參考 http://phorum.study-area.org/index.php/topic,24128.0.html