2017年6月18日 星期日

AutoIt 軟體自動化安裝

在企業的環境中常常有需要自動化佈署軟體的需求,AutIt可以讓佈署更加便利且自動化,以下是介紹軟體佈署的做法:
程式碼的運作可以定義要執行軟體的數量並且一個接一個依序執行,這裡以安裝Windows 的修正程式為例。語法中   Global $sExecPath[2]   本行定義增加增加執行多少程式,本例為2個,若有多個程式要執行只要增加數字就可以了,本例程式碼是陣例的應用。
以下程式碼定義要執行的程式名稱及參數,要注意的是要佈署的程式和AutoIt程式要放在一起,若放在不同的路徑要定義清楚否則就會失敗。因為要透過自動化佈署所以參數要定義為自動且靜默( Silent install )

以下是要安裝的程式碼:

$sExecPath[0] = @ScriptDir & "wusa.exe X86-all-windows6.1-kb3170455-x86.msu" &" /log /quiet /norestart"
$sExecPath[1] = @ScriptDir & "wusa.exe Windows6.1-KB3192403-x86.msu" &" /log /quiet /promptrestart"



範例:




#cs ----------------------------------------------------------------------------

AutoIt Version: 3.3.14.2
Author: 奔騰兔開發誌
Date : 2017/06/16
Script Function: Script Demo
Description :陣列的簡單運用於軟體佈署
Notes:http://pentiumto.blogspot.tw/


#ce ----------------------------------------------------------------------------


#include


Opt("TrayIconDebug", 1) ;0=no info, 1=debug line info
Opt("TrayIconHide", 1) ;0=show, 1=hide tray icon

Dim $sNowtime=@YEAR &"-"& @MON &"-"& @MDAY &" "& @HOUR &":"& @MIN

;指定要執行的程式數量,並提供執行路徑

Global $sExecPath[2]

$sExecPath[0] = @ScriptDir & "wusa.exe X86-all-windows6.1-kb3170455-x86.msu" &" /log /quiet /norestart"
$sExecPath[1] = @ScriptDir & "wusa.exe Windows6.1-KB3192403-x86.msu" &" /log /quiet /promptrestart"




ExecuteMe() ;啟動程式


Func ExecuteMe()


For $element In $sExecPath
Sleep(2000)
ConsoleWrite($sNowtime & " [Info] " & @ScriptLineNumber & " " &$element&@CRLF)


Local $iPID = RunWait(@ComSpec & " /c " & $element, @ScriptDir, @SW_MINIMIZE )

ProcessWaitClose($iPID)

ConsoleWrite($sNowtime & " [Info] " & @ScriptLineNumber & " " & $iPID&@CRLF)
Next


EndFunc







2017年6月16日 星期五

AutoIt 建立本機帳號並設定管理者群組中

在方便自動化作業,常常有需要在Windows 中建立帳號,並且設定好權限,以下用一段簡單的程式碼建立一個帳號,並將帳號加入到本機的管理者群組中

範例:


#cs ----------------------------------------------------------------------------

AutoIt Version: 3.3.14.2
Author: 奔騰兔開發誌
Date : 2017/06/16
Script Function: Script Demo
Description :建立本機帳號並設定管理者群組中
Notes:http://pentiumto.blogspot.tw/


#ce ----------------------------------------------------------------------------


Opt("TrayIconDebug", 1) ;0=no info, 1=debug line info
Opt("TrayIconHide", 0) ;0=show, 1=hide tray icon

Dim $sNowtime=@YEAR &"-"& @MON &"-"& @MDAY &" "& @HOUR &":"& @MIN

; ConsoleWrite($sNowtime & " [Info] " & @ScriptLineNumber & " 自訂訊息可以在開發過程除錯 "&@CRLF)



Dim $NewUser = "Admin" ;This is the User account to be created
Dim $Newpass = "Admin@password" ;This is were you enter the password you want for this account
Dim $Comment = "Windows Account Create Demo" ;This is a discription of the account that is added to the windows. Optional - leave blank for nothing to be entered
Dim $FName = "Admin" ;This is the Full name section of the account. Optional - leave blank for nothing to be entered



CreateUser()



Func CreateUser()

RunWait('net user '& $Newuser & ' ' & $Newpass & ' /add /fullname:"' & $FName & '" /comment:"' & $comment & '"', '', @SW_MAXIMIZE)

RunWait('net user '& $Newuser & ' /EXPIRES:never', '', @SW_HIDE)

RunWait('net localgroup users ' & $Newuser & ' /delete', '', @SW_HIDE)

RunWait('net localgroup Administrators ' & $Newuser & ' /add', '', @SW_HIDE)

RunWait('net accounts /maxpwage:unlimited', '', @SW_HIDE)


EndFunc





執行結果:

image
image

AutoIt Function 功能

為了讓你在開發AutoIt程式有效重覆運用程式碼,Function 的運用可以讓你快速開發AutoIt程式,以是介紹使用的方式:
一般的Function 直接將程式碼寫在 Func......EndFunc 之中 同時必須為Function 命名這樣程式才可以呼叫使用 如下,Function 的名稱為 FirstFunction() 在呼叫Function  時有一點要注意必須在Function 之前執行時要加上「()」括號

FirstFunction()     ;呼叫Function
Func FirstFunction()
…
…程式碼寫在這之間
…
EndFunc

範例:


#cs ----------------------------------------------------------------------------

AutoIt Version: 3.3.14.2
Author: 奔騰兔開發誌
Date : 2017/06/16
Script Function: Script Demo
Description :Function 簡單運用
Notes:http://pentiumto.blogspot.tw/


#ce ----------------------------------------------------------------------------


Opt("TrayIconDebug", 1) ;0=no info, 1=debug line info
Opt("TrayIconHide", 0) ;0=show, 1=hide tray icon

Dim $sNowtime=@YEAR &"-"& @MON &"-"& @MDAY &" "& @HOUR &":"& @MIN

FirstFunction() ;呼叫Function

Func FirstFunction()

ConsoleWrite($sNowtime & " [Info] " & @ScriptLineNumber & " 一般 Function 簡單運用 "&@CRLF)

EndFunc






執行結果:
image

2017年6月15日 星期四

AutoIt 開發範本

為了讓AutoIt 開發的程式後續的維護及讓未來接手維護的人能夠好維護,建議在開發的過程建立標準的範本,就是Templete ,在這裡分享我自已開發過程中使用的範本,當然在這機年開發AutoIt的過程中也參考了網路上許多先進的程式,給同樣使用AutoIt的同好參考囉。





#cs ----------------------------------------------------------------------------

AutoIt Version: 3.3.14.2
Author: XXXX XXXX
Date : 2017/06/15
Script Function: AutoIT Script Templete
Description :
Notes:


#ce ----------------------------------------------------------------------------


Opt("TrayIconDebug", 1) ;0=no info, 1=debug line info
Opt("TrayIconHide", 0) ;0=show, 1=hide tray icon

Dim $sNowtime=@YEAR &"-"& @MON &"-"& @MDAY &" "& @HOUR &":"& @MIN

ConsoleWrite($sNowtime & " [Info] " & @ScriptLineNumber & " 自訂訊息可以在開發過程除錯 "&@CRLF)




執行結果:


在開發工具下方的Debug 視窗可以透過ConsoleWrite() 寫出訊息並且可以得知是那一行,在開發的過程中非常好用。
image

2017年6月7日 星期三

ListView 資料存取設計

完成ListView 的資料Bind 後可依取得的資料進行後續的設計
image

在ListView的ItemDataBound 事件中取得ListView 的資料後進行資料處理
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        //使用ItemDataBound事件中找到ListView 的資料ID="UnitsInStockLabel"的值,並且
        //依傳回的值進行判斷,小於10修改ForeColor 為紅色,其它為綠色

        Label lb = (Label)e.Item.FindControl("UnitsInStockLabel");
        if (int.Parse(lb.Text) < 10)
        {
            lb.ForeColor = System.Drawing.Color.Red;
        }
        else
        {
            lb.ForeColor = System.Drawing.Color.Green;
        }
    }
結果:
根據不同的值顯示出紅色或綠色
image

2014年3月27日 星期四

C Sharp 大樂透程式



int[] lottos = new int[6];
int count = 0;     
while (true)
{
    int num = rnd.Next(1, 50);
    if (Array.IndexOf(lottos, num) > -1)  //比對陣列中的資料是否重覆出現
        continue;                                 //若重覆出現則再跑一次
    else
    {
        lottos[count] = num;              //計算開出幾個號碼
        count++;
        if (count == 6)
            break;
    }
}
listBox1.Items.Add("開獎順序 : " + string.Join(",", lottos));  //使用String.Join 重新組合陣列的資料
Array.Sort(lottos);
listBox1.Items.Add("大小排序 : " + string.Join(",", lottos));  //在listBox 中顯示開出的號碼

2013年9月4日 星期三

開啟ASPX 的Trace Report


開啟aspx網頁的Trace Report 可以檢視aspx 網頁的相關資訊,在每一個aspx 的網頁的page 指示詞中加入   trace=”true”  即可在瀏覽此網頁時顯示出Report
<%@ Page Language="C#" Trace="true" %>
顯示結果:
image