none
[分享]通過系統自帶命令獲取指定網絡接口流量數據的程序(包含批處理, VBS, PowerShell, C# 四種實現)加一個查看 3G 信號的批處理腳本 RRS feed

  • 常规讨论

  • 這四種程序都是通過調用 Windows 7/Server 2008 R2 下系統自帶命令

    netsh interface ipv4 show subinterfaces "IPv4 接口名"
    netsh interface ipv6 show subinterfaces "IPv6 接口名"

    獲取指定網絡接口的流量數據(能夠顯示的最大值受 netsh 的限制), 然後通過 for 和 find 命令過濾出發送和接受流量數據, 最後將其轉化為較為易讀到 KB 或 MB 數值.
    因為為了方便書寫, 所以下面這四種實現中對 netsh 子命令的調用都是使用簡寫.

    1. 批處理實現
    複製將下面的代碼, 然後根據代碼內的說明信息, 依據實際情況填入 IPv4, IPv6 的接口名, 最後保存為文件 NetworkTraffic.bat 並執行, 即可在命令提示符中獲取指定網絡接口的流量數據.

     

    @echo off
    setlocal enabledelayedexpansion
    REM 在 = 后不用添加半角双引号直接填入 IPv4 接口名
    set interface4=
    REM 在 = 后不用添加半角双引号直接填入 IPv6 接口名
    set interface6=
    set /a recvVal4=0
    set /a sendVal4=0
    set /a recvVal6=0
    set /a sendVal6=0
    set /a recvValB=0
    set /a sendValB=0
    
    if not defined interface4 (
    	set interface4=N/A
    	goto V6
    )
    
    for /f "tokens=3,4" %%a in ('netsh int ipv4 show subint "!interface4!" ^| find /i "!interface4!"') do (
    	set /a recvVal4=%%a
    	set /a recvValB=%%a
    	set /a sendVal4=%%b
    	set /a sendValB=%%b
    )
    if !recvVal4! lss 1048576 (
    	set /a recvVal4=!recvVal4!/1024
    	set unitR4=KB 
    ) else (
    	set /a recvVal4=!recvVal4!/1024/1024	
    	set unitR4=MB
    )
    if !sendVal4! lss 1048576 (
    	set /a sendVal4=!sendVal4!/1024
    	set unitS4=KB	
    ) else (
    	set /a sendVal4=!sendVal4!/1024/1024
    	set unitS4=MB	
    )
    
    :V6
    
    if not defined interface6 (
    	set interface6=N/A
    	goto Calc
    )
    
    for /f "tokens=3,4" %%a in ('netsh int ipv6 show subint "!interface6!" ^| find /i "!interface6!"') do (
    	set /a recvVal6=%%a
    	set /a recvValB=!recvValB!+%%a
    	set /a sendVal6=%%b
    	set /a sendValB=!sendValB!+%%b
    )
    if !recvVal6! lss 1048576 (
    	set /a recvVal6=!recvVal6!/1024
    	set unitR6=KB 
    ) else (
    	set /a recvVal6=!recvVal6!/1024/1024	
    	set unitR6=MB
    )
    if !sendVal6! lss 1048576 (
    	set /a sendVal6=!sendVal6!/1024
    	set unitS6=KB	
    ) else (
    	set /a sendVal6=!sendVal6!/1024/1024
    	set unitS6=MB	
    )
    
    :Calc
    
    if !recvVal6! equ 0 (set unitR6=KB)
    if !sendVal6! equ 0 (set unitS6=KB)
    
    if !recvValB! lss 1048576 (
    	set /a recvValB=!recvValB!/1024
    	set unitRB=KB 
    ) else (
    	set /a recvValB=!recvValB!/1024/1024	
    	set unitRB=MB
    )
    if !sendValB! lss 1048576 (
    	set /a sendValB=!sendValB!/1024
    	set unitSB=KB	
    ) else (
    	set /a sendValB=!sendValB!/1024/1024
    	set unitSB=MB	
    )
    
    echo NetworkTraffic.bat 获取当前指定网络接口的流量数据。
    echo Copyright (C) 2012 repl
    echo.
    echo Interface: !interface4! Type: IPv4 Recv: !recvVal4! !unitR4! Send: !sendVal4! !unitS4!
    echo Interface: !interface6! Type: IPv6 Recv: !recvVal6! !unitR6! Send: !sendVal6! !unitS6!
    echo Interface: BOTH Type: BOTH Recv: !recvValB! !unitRB! Send: !sendValB! !unitSB!
    
    endlocal

     

    注:
    a. 如果希望通過直接雙擊腳本顯示命令, 那麼請在代碼末尾添加 pause.
    b. 因為 set /a 命令只能支持 32 位精確度, 所以當流量超過 2147483647 字節後, 將不能輸出正確結果.

    2. VBS 實現
    複製將下面的代碼, 然後根據代碼內的說明信息, 依據實際情況填入 IPv4, IPv6 的接口名, 最後保存為文件 NetworkTraffic.vbs 並執行, 即可在消息框或命令提示符中獲取指定網絡接口的流量數據.

     

    Option Explicit
    Dim interface4, interface6 
    Dim cmdLine, cmd, rc, tmp
    Dim result4, result6, arr
    Dim recvVal4, unitR4, sendVal4, unitS4
    Dim recvVal6, unitR6, sendVal6, unitS6 
    Dim tmpRB, tmpSB, recvValB, unitRB, sendValB, unitSB
    
    const IFTYPE4 = "IPv4"
    const IFTYPE6 = "IPv6"
    
    '在半角双引号内填入使用 IPv4 的接口名
    interface4 = ""
    '在半角双引号内填入使用 IPv6 的接口名
    interface6 = ""
    
    result4 = ""
    result6 = ""
    
    recvVal4 = 0
    unitR4 = " KB"
    sendVal4 = 0
    unitS4 = " KB"
    
    recvVal6 = 0
    unitR6 = " KB"
    sendVal6 = 0
    unitS6 = " KB"
    
    tmpRB = 0
    tmpSB = 0
    recvValB = 0
    unitRB = " KB"
    sendValB = 0
    unitSB = " KB"
    
    Set arr = Nothing
    Set cmd = CreateObject("WScript.Shell")
    
    if (interface4 <> "") Then
    	cmdLine = "cmd /c " & chr(34) & _
     		"for /f " & _ 
    		chr(34) & "tokens=3,4" & chr(34) & _
    		" %a in ('netsh int " & IFTYPE4 & " show subint " & chr(34) & interface4 & chr(34) & _
    		" ^| find /i " & chr(34) & interface4 & chr(34) & "') do @echo %a %b" & chr(34)
    	Set rc = cmd.Exec(cmdLine)
    	If (Not rc.StdOut.AtEndOfStream) Then
    		result4 = rc.StdOut.ReadAll()
    	End If
    End If
    If (result4 <> "" ) Then 
    	arr = Split(result4, " ")
    
    	tmpRB = CDbl(arr(0))
    	If (CDbl(arr(0)) < 1048576) Then
    		recvVal4 =  CDbl(arr(0)) / 1024
    		unitR4 = " KB"
    	Else
    		recvVal4 =  CDbl(arr(0)) / 1024 / 1024
    		unitR4 = " MB"
    	End If
    
    	tmpSB = CDbl(arr(1))
    	If (CDbl(arr(1)) < 1048576) Then
    		sendVal4 = CDbl(arr(1)) / 1024
    		unitS4 = " KB"
    	Else
    		sendVal4 = CDbl(arr(1)) / 1024 / 1024
    		unitS4 = " MB"
    	End If
    	Set arr = Nothing
    End If
    
    if (interface6 <> "") Then
    	cmdLine = "cmd /c " & chr(34) & _
     		"for /f " & _ 
    		chr(34) & "tokens=3,4" & chr(34) & _
    		" %a in ('netsh int " & IFTYPE6 & " show subint " & chr(34) & interface6 & chr(34) & _
    		" ^| find /i " & chr(34) & interface6 & chr(34) & "') do @echo %a %b" & chr(34)
    	Set rc = cmd.Exec(cmdLine)
    	If (Not rc.StdOut.AtEndOfStream) Then
    		result6 = rc.StdOut.ReadAll()
    	End If
    	Set rc = Nothing
    End If
    If (result6 <> "" ) Then 
    	arr = Split(result6, " ")
    
    	tmpRB = tmpRB + CDbl(arr(0))
    	If (CDbl(arr(0)) < 1048576) Then
    		recvVal6 = CDbl(arr(0)) / 1024
    		unitR6 = " KB"
    	Else
    		recvVal6 = CDbl(arr(0)) / 1024 / 1024
    		unitR6 = " MB"
    	End If
    
    	tmpSB = tmpSB + CDbl(arr(1))
    	If (CDbl(arr(1)) < 1048576) Then
    		sendVal6 = CDbl(arr(1)) / 1024
    		unitS6 = " KB"
    	Else
    		sendVal6 = CDbl(arr(1)) / 1024 / 1024
    		unitS6 = " MB"
    	End If
    	Set arr = Nothing
    End If
    
    
    If (interface4 = "") Then
    	interface4 = "N/A"
    End If
    If (interface6 = "") Then
    	interface6 = "N/A"
    End If
    
    If (tmpRB < 1048576) Then
    	recvValB = tmpRB / 1024
    	unitRB = " KB"
    Else
    	recvValB = tmpRB / 1024 / 1024
    	unitRB = " MB"
    End If
    
    If (tmpSB < 1048576) Then
    	sendValB = tmpSB / 1024
    	unitSB = " KB"
    Else
    	sendValB = tmpSB / 1024 / 1024
    	unitSB = " MB"
    End If
    
    WSH.Echo "NetworkTraffic.vbs 获取当前指定网络接口的流量数据。" & _
    	chr(10) & chr(13) & _	
    	"Copyright (C) 2012 repl" & _
    	chr(10) & chr(13) & _	 
    	chr(10) & chr(13) & _
    	"Interface: " & interface4 & _
    	" Type: " & IFTYPE4 & _
    	" Recv: " & FormatNumber(recvVal4, 2, true) & unitR4 & _
    	" Send: " & FormatNumber(sendVal4, 2, true) & unitS4 & _
    	chr(10) & chr(13) & _	
    	"Interface: " & interface6 & _
    	" Type: " & IFTYPE6 & _
    	" Recv: " & FormatNumber(recvVal6, 2, true) & unitR6 & _
    	" Send: " & FormatNumber(sendVal6, 2, true) & unitS6 & _
    	chr(10) & chr(13) &  _	
    	"Interface: " & "BOTH" & _
    	" Type: " & "BOTH" & _
    	" Recv: " & FormatNumber(recvValB, 2, true) & unitRB & _
    	" Send: " & FormatNumber(sendValB, 2, true) & unitSB
    

     


    注:
    a. 如果直接執行 VBS 腳本, 結果將會以彈出消息框的方式提供, 當消息框彈出後, 可以通過按 Ctrl+C 複製結果到剪貼板.
    b. 對於該腳本還可以在命令提示符中輸出結果, 執行命令

    cscript /nologo .\NetworkTraffic.vbs

    為了方便執行, 可以為其添加一個批處理程序來使得其在命令提示符中實現

     

    @echo off
    cscript /nologo %~dp0NetworkTraffic.vbs
    

     

    此腳本假設通過 vbs 文件中一個目錄中.

    c. 因為 VBS 不支持 Decimal 類型, 所以輸出結果可能會以科學計數法表示.

    3. PowerShell 實現
    複製將下面的代碼, 然後根據代碼內的說明信息, 依據實際情況填入 IPv4, IPv6 的接口名, 最後保存為文件 NetworkTraffic.ps1 並執行(字符 ps 後的字符是數字一), 即可在命令提示符中獲取指定網絡接口的流量數據.

     

    set-strictmode -version 2.0
    
    set-variable -name IPV4 -value 'IPv4' -scope script -option constant
    set-variable -name IPV6 -value 'IPv6' -scope script -option constant
    
    # 半角单引号中填入 IPv4 的接口名
    [string]$interface4 = ''
    # 半角单引号中填入 IPv6 的接口名
    [string]$interface6 = ''
    
    [decimal]$recvVal4 = 0
    [string]$unitR4 = 'KB'
    [decimal]$sendVal4 = 0
    [string]$unitS4 = 'KB'
    
    [decimal]$recvVal6 = 0
    [string]$unitR6 = 'KB'
    [decimal]$sendVal6 = 0
    [string]$unitS6 = 'KB'
    
    [decimal]$recvValB = 0
    [string]$unitRB = 'KB'
    [decimal]$sendValB = 0
    [string]$unitSB = 'KB'
    
    [string]$returnValue = ''
    [string[]]$arr = $null
    
    if ($interface4 -ne $null -and $interface4 -ne '')
    {	
    	$returnValue = cmd /c "for /f `"tokens=3,4`" %a in ('netsh int $IPV4 show subint `"$interface4`" ^| find /i `"$interface4`"') do @echo %a %b"	
    	if ($returnValue -ne $null -and $returnValue -ne '')
    	{
    		$arr = $returnValue.Split(' ')
    		$returnValue = ''
    		
    		$recvValB = [decimal]$arr[0]	
    		if ([decimal]$arr[0] -lt 1MB)
    		{
    			$recvVal4 = [System.Math]::Round([decimal]$arr[0] / 1KB, 2)
    			$unitR4 = 'KB'
    		}
    		else
    		{
    			$recvVal4 = [System.Math]::Round([decimal]$arr[0] / 1MB, 2)
    			$unitR4 = 'MB'
    		}
    		
    		$sendValB = [decimal]$arr[1]
    		if ([decimal]$arr[1] -lt 1MB)
    		{
    			$sendVal4 = [System.Math]::Round([decimal]$arr[1] / 1KB, 2)
    			$unitS4 = 'KB'
    		}
    		else
    		{
    			$sendVal4 = [System.Math]::Round([decimal]$arr[1] / 1MB, 2)
    			$unitS4 = 'MB'
    		}
    
    		$arr = $null
    	}
    }
    
    if ($interface6 -ne $null -and $interface6 -ne '')
    {	
    	$returnValue = cmd /c "for /f `"tokens=3,4`" %a in ('netsh int $IPV6 show subint `"$interface6`" ^| find /i `"$interface6`"') do @echo %a %b"	
    	if ($returnValue -ne $null -and $returnValue -ne '')
    	{
    		$arr = $returnValue.Split(' ')
    		$returnValue = ''
    		
    		$recvValB += [decimal]$arr[0]	
    		if ([decimal]$arr[0] -lt 1MB)
    		{
    			$recvVal6 = [decimal]$arr[0] / 1KB
    			$unitR6 = 'KB'
    		}
    		else
    		{
    			$recvVal6 = [decimal]$arr[0] / 1MB
    			$unitR6 = 'MB'
    		}
    		
    		$sendValB += [decimal]$arr[1]
    		if ([decimal]$arr[1] -lt 1MB)
    		{
    			$sendVal6 = [decimal]$arr[1] / 1KB
    			$unitS6 = 'KB'
    		}
    		else
    		{
    			$sendVal6 = [decimal]$arr[1] / 1MB
    			$unitS6 = 'MB'
    		}
    
    		$arr = $null
    	}
    }
    
    # 也可用 [string]::IsNullOrEmpty($interface4) 替代
    if ($interface4 -eq $null -or $interface4 -eq '') {$interface4 = 'N/A'}
    if ($interface6 -eq $null -or $interface6 -eq '') {$interface6 = 'N/A'}
    
    if ($recvValB -lt 1MB)
    {
    	$recvValB = [System.Math]::Round($recvValB / 1KB, 2)
    	$unitRB = 'KB'
    }
    else
    {
    	$recvValB = [System.Math]::Round($recvValB / 1MB, 2)
    	$unitRB = 'MB'
    }
    
    if ($sendValB -lt 1MB)
    {
    	$sendValB = [System.Math]::Round($recvValB / 1KB, 2)
    	$unitSB = 'KB'
    }
    else
    {
    	$sendValB = [System.Math]::Round($recvValB / 1MB, 2)
    	$unitSB = 'MB'
    }
    
    [System.Text.StringBuilder]$sb = new-object System.Text.StringBuilder
    [void]$sb.AppendLine('NetworkTraffic.ps1 获取当前指定网络接口的流量数据。')
    [void]$sb.AppendLine('Copyright (C) 2012 repl')
    [void]$sb.AppendLine()
    [void]$sb.AppendLine("Interface: $interface4 Type: $IPV4 Recv: $recvVal4 $unitR4 Send: $sendVal4 $unitS4")
    [void]$sb.AppendLine("Interface: $interface6 Type: $IPV6 Recv: $recvVal6 $unitR6 Send: $sendVal6 $unitS6")
    [void]$sb.AppendLine("Interface: BOTH Type: BOTH Recv: $recvValB $unitRB Send: $sendValB $unitSB")
    
    $sb.ToString()
    
    
    

     


    注:
    a. PowerShell 腳本需要在 PowerShell 中執行, 因為默認情況下, PowerShell 禁止執行腳本, 所以如果要執行的話, 需要以管理員身份運行 powershell 並執行命令

    Set-ExecutionPolicy RemoteSigned

    設置完成後, 就可以通過

    & 文件所在路徑\NetworkTraffic.ps1

    來執行腳本.

    4. C# 實現
    複製將下面的代碼, 並保存為文件 NetworkTraffic.cs

     

    /*
     * 由SharpDevelop创建。
     * 用户: repl
     * 日期: 2012/1/30
     * 时间: 20:54
     * 
     * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
     */
    using System;
    using System.Diagnostics;
    using System.Text;
    
    namespace repl.Tools.NetworkTraffic
    {
    	class Program
    	{
    		public static void Main(string[] args)
    		{
    			if (args.Length <= 0) {return;}
    			
    			const string IPV4 = "IPv4";
    			const string IPV6 = "IPv6";
    			
    			string interface4 = "";
    			string interface6 = "";
    			
    			decimal rc = 0;
    			
    			decimal recvVal4 = 0;
    			string unitR4 = " KB";
    			decimal sendVal4 = 0;
    			string unitS4 = " KB";
    			
    			decimal recvVal6 = 0;
    			string unitR6 = " KB";
    			decimal sendVal6 = 0;
    			string unitS6 = " KB"; 
    			
    			decimal recvValB = 0;
    			string unitRB = " KB";
    			decimal sendValB = 0;
    			string unitSB = " KB";
    			
    			string returnValue = "";
    			string[]arr = null;
    			
    			interface4 = args[0];
    			if (args.Length >= 2) {interface6 = args[1];}
    			
    			if (interface4 != null && interface4 != "")
    			{
    				ProcessStartInfo startInfo = new ProcessStartInfo();
    				startInfo.UseShellExecute = false;
    				startInfo.RedirectStandardOutput = true;				
    				startInfo.FileName = "cmd";				
    				startInfo.Arguments = " /c \"for /f \"tokens=3,4\" %a in ('netsh int " + IPV4 +
    										" show subint " + "\"" + interface4 + "\" ^| find /i " +
    										"\"" + interface4 + "\"') do @echo %a %b\""; 
    				Process proc = new Process();				
    				proc.StartInfo = startInfo;
    				proc.Start();				
    				returnValue = proc.StandardOutput.ReadToEnd();												
    				
    				if (returnValue != null && returnValue != "")
    				{
    					arr = returnValue.Split(' ');
    					returnValue = null;
    					
    					rc = decimal.Parse(arr[0]);
    					recvValB = rc;
    					if (rc < (1024 * 1024))
    					{
    						recvVal4 = Math.Round(rc / 1024, 2);
    						unitR4 = " KB";
    					}
    					else
    					{
    						recvVal4 = Math.Round(rc / 1024 / 1024, 2);
    						unitR4 = " MB";
    					}
    					rc = 0;
    					
    					rc = decimal.Parse(arr[1]);
    					sendValB = rc;
    					if (rc < (1024 * 1024))
    					{
    						sendVal4 = Math.Round(rc / 1024, 2);
    						unitS4 = " KB";
    					}
    					else
    					{
    						sendVal4 = Math.Round(rc / 1024 / 1024, 2);
    						unitS4 = " MB";
    					}
    					rc = 0;
    				}
    			}
    			
    			if (interface6 != null && interface6 != "")
    			{
    				ProcessStartInfo startInfo = new ProcessStartInfo();
    				startInfo.UseShellExecute = false;
    				startInfo.RedirectStandardOutput = true;
    				startInfo.FileName = "cmd";				
    				startInfo.Arguments = " /c \"for /f \"tokens=3,4\" %a in ('netsh int " + IPV6 +
    										" show subint " + "\"" + interface6 + "\" ^| find /i " +
    										"\"" + interface6 + "\"') do @echo %a %b\""; 
    				Process proc = new Process();				
    				proc.StartInfo = startInfo;
    				proc.Start();				
    				returnValue = proc.StandardOutput.ReadToEnd();												
    				
    				if (returnValue != null && returnValue != "")
    				{
    					arr = returnValue.Split(' ');
    					returnValue = null;
    					
    					rc = decimal.Parse(arr[0]);
    					recvValB += rc;
    					if (rc < (1024 * 1024))
    					{
    						recvVal6 = Math.Round(rc / 1024, 2);
    						unitR6 = " KB";
    					}
    					else
    					{
    						recvVal6 = Math.Round(rc / 1024 / 1024, 2);
    						unitR6 = " MB";
    					}
    					rc = 0;
    					
    					rc = decimal.Parse(arr[1]);
    					sendValB += rc;
    					if (rc < (1024 * 1024))
    					{
    						sendVal6 = Math.Round(rc / 1024, 2);
    						unitS6 = " KB";
    					}
    					else
    					{
    						sendVal6 = Math.Round(rc / 1024 / 1024, 2);
    						unitS6 = " MB";
    					}
    				}
    			}
    
    			if (interface4 == null || interface4 == "") {interface4 = "N/A";}
    			if (interface6 == null || interface6 == "") {interface6 = "N/A";}
    			
    			if (recvValB < (1024 * 1024))
    			{
    				recvValB = Math.Round(recvValB / 1024, 2);
    				unitRB = " KB";
    			}
    			else
    			{
    				recvValB = Math.Round(recvValB / 1024 / 1024, 2);
    				unitRB = " MB";
    			}
    			
    			if (sendValB < (1024 * 1024))
    			{
    				sendValB = Math.Round(sendValB / 1024, 2);
    				unitSB = " KB";
    			}
    			else
    			{
    				sendValB = Math.Round(sendValB / 1024 / 1024, 2);
    				unitSB = " MB";
    			}
    			
    			StringBuilder sb = new StringBuilder();
    			sb.AppendLine("NetworkTraffic.exe 获取当前指定网络接口的流量数据。");
    			sb.AppendLine("Copyright (C) 2012 repl");
    			sb.AppendLine();
    			sb.AppendLine("用法: NetworkTraffic.exe\t\"IPv4 接口名\" \"IPv6 接口名\"");
    			sb.AppendLine("\t\t\t\t如果无某一个接口名, 则用 \"\" 代替");
    			sb.AppendLine();
    			sb.AppendLine("Interface: " + interface4 + " Type: " + IPV4 + " Recv: " + recvVal4 + unitR4 + " Send: " + sendVal4 + unitS4);
    			sb.AppendLine("Interface: " + interface6 + " Type: " + IPV6 + " Recv: " + recvVal6 + unitR6 + " Send: " + sendVal6 + unitS6);
    			sb.AppendLine("Interface: " + "BOTH" + " Type: " + "BOTH" + " Recv: " + recvValB + unitRB + " Send: " + sendValB + unitSB);
    			
    			Console.Write(sb.ToString());
    		}
    	}
    }
    
    然後執行執行命令

    %systemroot%\Microsoft.NET\Framework\v2.0.50727\csc /target:exe /debug- /optimize+ 文件所在路徑\NetworkTraffic.cs

    生成 NetworkTraffic.exe, 即可在命令提示符中獲取指定網絡接口的流量數據.

    注:
    a. 輸入 IPv4, IPv6 接口名的方法, 可以參見代碼, 或者直接 NetworkTraffic.exe 獲得

    四種實現的優缺點:

    批處理的代碼較為簡單, 不過加之 set 的限制會導致結果存在一定程度的誤差甚至是返回錯誤數據.
    vbs 實現可能存在因數值過大會將結果以科學計數法表示, 不宜閱讀, 但同時提供了兩種結果顯示方式.
    這兩種方法因為需求的組件都已內置在系統中, 所以可以直接在 Windows 7/Server 2008 R2 Server Core 中運行.
    PowerShell 實現, 因為其支持 Decimal 類型, 其輸出數值的精確度只受 netsh 的限制, 不過使用該腳本需要系統中啟用了 PowerShell, 不太適合默認沒有啟用 PowerShell 的系統, 比如 Windows Server 2008 R2 Server Core.
    C# 實現, 其內部實現與 PowerShell 沒有什麼差異, 不過在執行前需要先編譯成可執行文件, 但好在 .NET 環境和 csc 編譯器一般都內置在系統中, 所以通常來說不是太大問題.

    最後可以將批處理, VBS, 和 exe 文件複製到 \Windows 目錄以方便執行, 對於 PowerShell, 則可以將腳本添加到 $profile 文件中作為函數以方便執行, 同時可以通過 set-alias 為其設置別名以減少輸入的字符數.

     

     

     

     

     


    Folding@Home

    • 已编辑 repl 2012年1月31日 3:02
    2012年1月30日 16:25

全部回复

  • 這裡再提供一個查看 3G 網絡信號的批處理腳本, 這個功能由兩個腳本組成.
    第一個腳本 mbnsig.bat
    @echo off
    REM mbnsig.bat 用于查看 3G 网络信号
    REM Copyright (C) 2011 repl
    REM 这里假设接口名为 Mobile Broadband Connection
    REM 请以实际情况替代
    netsh mbn show signal interface="Mobile Broadband Connection" | find /i "信号"
    if %errorlevel% neq 0 (goto func2)
    goto exit
    
    :func2
    REM 因为不明原因可能出现当前有效的接口名后存在数字编号
    REM 但在重启后, 再次连接后又会变回 Mobile Broadband Connection
    REM 所以这里可能会查询两次
    netsh mbn show signal interface="Mobile Broadband Connection 2" | find /i "信号"
    if %errorlevel% neq 0 (echo signal error)
    
    :exit
    
    第二個腳本 mbnsigl.bat
     
     
    @echo off
    REM mbnsigl.bat 用于每隔 3 秒调用 mbnsig 来查看 3G 网络信号
    REM 直接关闭命令提示符窗口, 或按 Ctrl+C 结束脚本的运行
    REM Copyright (C) 2011 repl
    :loop
    	call mbnsig
    	timeout /t 3 > nul
    goto loop
    

    Folding@Home
    2012年1月31日 2:21