捐血一袋救人一命

2010年10月25日 星期一

按照部門來安裝印表機

以往公司透過 Group Policy 安裝印表機,都是在Windows 啟動時,執行一支 printer.exe 的小程式來連接印表機,但是它會把 Active Directory 裡的印表機全數安裝上去。

對於所有的公司同仁都覺得有點困擾,因為使用者每次列印都要從一大堆印表機選擇其中某一台。

同仁希望能減少印表機的清單數量。

所以我寫了以下的 Script,透過讀取使用者OU來決定安裝哪些印表機

這樣只要設定一條Group Policy 就可以套用全公司,也不用設定好幾條 Group Policy去分別套用 OU

On Error Resume Next
' 讀取使用者名稱(CN)、所屬組織單位(OU)、網域(DC)
Set objSysInfo = CreateObject("ADSystemInfo")
strName = objSysInfo.UserName
' PS. strUserName = objSysInfo.UserName
' 格式:CN=User Name, OU=XXX,OU=XXX,DC=XXX,DC=XXX,DC=XXX
' 例:CN=UserName,OU=資訊室,OU=管理部,OU=管理處,OU=Company,DC=Company,DC=com,DC=tw

' 將字串拆解成陣列,依「逗號」拆開
arrUserName = Split(strName, ",")

' 陣列註標從 0 開始
' 陣列 0 是使用者名稱(CN=xxx)
' 陣列 1 是最底層 (OU=xxxx)
' 將陣列 1 的資料,依「等號」拆開
arrOU = Split(arrUserName(1), "=")

' 陣列 0 = "OU"
' 陣列 1 是使用者的組織單位名稱
strUserOU = arrOU(1)

Set objNetwork = CreateObject("WScript.Network")
Select Case strUserOU
    Case "ITDept"
        ' 建立印表機連線
        objNetwork.AddWindowsPrinterConnection "\\DFS1\HP3600"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\HP3600"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\Konica事務機"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\Konica事務機"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\FAXPrinter"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\FAXPrinter"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\PrtQ1_at_SaleDept"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\PrtQ1_at_SaleDept"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\PrtQ2_at_SaleDept"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\PrtQ2_at_SaleDept"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\SHARP影印機"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\SHARP影印機"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\SHIPPING"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\SHIPPING"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\P1006"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\P1006"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\R1800"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\R1800"
        ' 設定預設印表機
        objNetwork.SetDefaultPrinter "\\DFS1\HP3600"
    Case "SalesDept"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\HP3600"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\HP3600"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\Konica事務機"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\Konica事務機"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\FAXPrinter"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\FAXPrinter"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\PrtQ1_at_SaleDept"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\PrtQ1_at_SaleDept"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\PrtQ2_at_SaleDept"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\PrtQ2_at_SaleDept"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\SHARP影印機"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\SHARP影印機"
        objNetwork.SetDefaultPrinter "\\DFS1\Konica事務機"
    Case "ProDept"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\HP3600"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\HP3600"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\Konica事務機"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\Konica事務機"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\FAXPrinter"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\FAXPrinter"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\PrtQ1_at_SaleDept"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\PrtQ1_at_SaleDept"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\PrtQ2_at_SaleDept"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\PrtQ2_at_SaleDept"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\SHARP影印機"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\SHARP影印機"
        objNetwork.SetDefaultPrinter "\\DFS1\Konica事務機"
    Case "ShipDept"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\HP3600"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\HP3600"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\Konica事務機"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\Konica事務機"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\FAXPrinter"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\FAXPrinter"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\PrtQ1_at_SaleDept"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\PrtQ1_at_SaleDept"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\PrtQ2_at_SaleDept"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\PrtQ2_at_SaleDept"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\SHARP影印機"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\SHARP影印機"
        objNetwork.SetDefaultPrinter "\\DFS1\Konica事務機"
    Case "MarketDept"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\HP3600"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\HP3600"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\Konica事務機"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\Konica事務機"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\FAXPrinter"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\FAXPrinter"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\P1006"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\P1006"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\R1800"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\R1800"
        objNetwork.SetDefaultPrinter "\\DFS1\P1006"
    Case "ManDept"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\HP3600"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\HP3600"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\Konica事務機"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\Konica事務機"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\FAXPrinter"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\FAXPrinter"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\SHARP影印機"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\SHARP影印機"
        objNetwork.SetDefaultPrinter "\\DFS1\Konica事務機"
    Case "GMDept"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\HP3600"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\HP3600"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\Konica事務機"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\Konica事務機"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\FAXPrinter"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\FAXPrinter"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\SHARP影印機"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\SHARP影印機"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\P1006"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\P1006"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\R1800"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\R1800"
        objNetwork.SetDefaultPrinter "\\DFS1\SHARP影印機"
    Case "BOD"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\HP3600"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\HP3600"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\Konica事務機"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\Konica事務機"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\FAXPrinter"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\FAXPrinter"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\SHARP影印機"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\SHARP影印機"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\SHIPPING"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\SHIPPING"
        objNetwork.SetDefaultPrinter "\\DFS1\Konica事務機"
    Case "Finance"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\HP3600"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\HP3600"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\Konica事務機"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\Konica事務機"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\FAXPrinter"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\FAXPrinter"
        objNetwork.AddWindowsPrinterConnection "\\DFS1\SHARP影印機"
        objNetwork.AddWindowsPrinterConnection "\\DFS2\SHARP影印機"
        objNetwork.AddWindowsPrinterConnection "\\FANNY-PC\EPSONSty"
        objNetwork.AddWindowsPrinterConnection "\\JOYCE-PC\EpsonLQ1170C"
        objNetwork.SetDefaultPrinter "\\DFS1\Konica事務機"
    Case Else
        ' 移除印表機連線
        objNetwork.RemovePrinterConnection "\\DFS1\HP3600彩雷"
        objNetwork.RemovePrinterConnection "\\DFS1\Konica Minolta事務機"
        objNetwork.RemovePrinterConnection "\\DFS1\FX DocuPrint 203A"
        objNetwork.RemovePrinterConnection "\\DFS1\Q1"
        objNetwork.RemovePrinterConnection "\\DFS1\Q2"
        objNetwork.RemovePrinterConnection "\\DFS1\SHARP影印機"
        objNetwork.RemovePrinterConnection "\\DFS1\SHIPPING"
        objNetwork.RemovePrinterConnection "\\DFS2\FX DocuPrint 203A"
        objNetwork.RemovePrinterConnection "\\DFS2\Q1"
        objNetwork.RemovePrinterConnection "\\DFS2\Q2"
        objNetwork.RemovePrinterConnection "\\DFS2\SHARP影印機"
        objNetwork.RemovePrinterConnection "\\DFS2\SHIPPING"
        objNetwork.RemovePrinterConnection "\\DFS2\HP3600彩雷"
        objNetwork.RemovePrinterConnection "\\DFS2\Konica Minolta事務機"
End Select

反應:

0 意見: