0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

共有プリンタを誰でも、同じ状態にセットアップ可能にする、Powershellスクリプトを書いて手抜きする話

Last updated at Posted at 2020-01-20

はじめに

先日某所に共有プリンターを設置したが、
・各個人PCにわざわざ設定しに行くのは面倒(個人所有のノートPCからの使用を想定)
・ある程度、プリンター名等の設定状況を共通化したい(説明簡略化やサポート的に)
・そこでの指定機はWindowsなので基本Windows前提で。macosとかはどうにかなるやろ。てか、どうにかしろ。
・Windowsは(指定機が64bit故に必要か微妙だが一応)、32bitと64bitの考慮が必要。(ドライバー的に)
・ドライバー標準のインストーラーで上手くいかなかった。(プリンターIPの手動指定が必要だった)

の条件(?)があり、結果的にbatファイルかPowershellの2択になった。

なぜPowershellスクリプトで…?

選んだ理由はただ単純に、
・記述仕様が見慣れた感じ
・変数が自由に定義できて使える
・batよりは環境差異に対応可能(そう)
・batよりは退任時の引き継ぎもしやすそう(最重要)
・Windowsならば7以降、標準で使える!(重要)

制作した物

注)このセットアップツールはFujiXerox社製 DocuPrint C3350 1台を前提として記述しています。 

配布したzipファイルの構造

zipファイルのディレクトリー構造はこんな感じ。
セットアップツール.zip/
 ├ cswndフォルダー(64bit)/
 │ └ ART_EX/
 │   └ amd64/
 │     └ 001/
 │       └ FXLERJL.inf
 ├ cswnd32フォルダー(32bit)/
 │ └ ART_EX/
 │   └ win2000_XP/
 │     └ FXLKNJL.inf
 ├ 管理者権限kick用.bat
 └ セットアップツール.ps1

セットアップツールとキックbat。

こんな感じ。xxxやディレクトリー、パスの部分はご自身の環境に合わせて設定を。
※PowerShellから管理者権限を取れない為、batを使って管理者権限のPowershellでツールを呼び出す形に変更しました。

"管理者権限kick用.bat"
cd /d %~dp0
powershell -NoProfile -ExecutionPolicy Unrestricted .\セットアップツール.ps1
"セットアップツール.ps1"
## --- config ---
# プリンタのIPアドレス
$ipaddress = "xxx.xxx.xxx.xxx"

# 解凍したプリンタドライバパッケージ内のinfファイルに記述されている、プリンタの名称
$inf_printername = "FX DocuPrint C3350"

# PCの「デバイスとプリンター」に表示されるプリンタの名称
$printername = "xxxxxxx-PRN (DocuPrint C3350)"

# --- 本ファイル の 解凍場所 を判定する ---

if( $PSVersionTable.PSVersion.Major -ge 3 ){
    echo "Data from `$PSScriptRoot"
    $ScriptDir = $PSScriptRoot
}
# for PS v2
else{
    echo "Data from `$MyInvocation.MyCommand.Path"
    $ScriptDir = Split-Path $MyInvocation.MyCommand.Path -Parent
}

Write-Output $ScriptDir

## --- main ---
Set-Location C:\Windows\System32\Printing_Admin_Scripts\ja-JP\

# 通信ポートの作成
$port_name = "LPD_" + $ipaddress
cscript prnport.vbs -a -r $port_name -h $ipaddress -o lpr -q lp -n 515 -me -i 1 -y public

# --- OS の bit を判定する ---
$os = Get-WmiObject -Class Win32_OperatingSystem
Write-Host $os.OSarchitecture

if ( $os.OSarchitecture -eq "64 ビット" )
    {
    $driv64bit_path = "\cswnd\ART_EX\amd64\001\FXLERJL.inf"
    $doc64_path = $ScriptDir + $driv64bit_path
    
    # 64bit版ドライバのインストール
    cscript prndrvr.vbs -a -m $inf_printername -v 3 `
    -i "$doc64_path"
    }
else
    {
    $driv32bit_path = "\cswnd32\ART_EX\Win2000_XP\FXLKNJL.inf"
    $doc32_path = $ScriptDir + $driv32bit_path

    # 32bit版ドライバのインストール
    cscript prndrvr.vbs -a -m $inf_printername -v 3 `
    -i "$doc32_path"
    }

# プリンタの作成
cscript prnmngr.vbs -a -p $printername -m $inf_printername -r $port_name
## --- end ---

スクリプト解説的な

参考記事

PowerShellで、プリンタドライバをインストールする - Quiita

[スクリプトフォルダーの取得 - MURA の Home Page]
(https://www.vwnet.jp/Windows/PowerShell/pwd.htm)

参考記事からの変更点

1.Wikiサイトにドライバーとツール一式をzipファイル形式でアップして、
利用者自身で任意の場所に解凍し実行を想定していたので、人により解凍場所(のディレクトリー)が異なる為、解凍先のパスを取得
(この行為は一見無駄に見えますが、後ほど生きてきます)

"セットアップツール.ps1(8行目~25行目)"
# PCの「デバイスとプリンター」に表示されるプリンタの名称
$printername = "xxxxxxx-PRN (DocuPrint C3350)"

# --- 本ファイル の 解凍場所 を判定する ---

if( $PSVersionTable.PSVersion.Major -ge 3 ){
    echo "Data from `$PSScriptRoot"
    $ScriptDir = $PSScriptRoot
}
# for PS v2
else{
    echo "Data from `$MyInvocation.MyCommand.Path"
    $ScriptDir = Split-Path $MyInvocation.MyCommand.Path -Parent
}

Write-Output $ScriptDir

## --- main ---

2.bitごとの判定を行った後、1で求めたディレクトリーと、対応するドライバーINFファイルへの相対パスを結合し、絶対パスへ変換し代入。(下では一例として、64bitを挙げてますが32bitでもやってることは同じです。)

"セットアップツール.ps1(36行目~44行目)"
if ( $os.OSarchitecture -eq "64 ビット" )
    {
    $driv64bit_path = "\cswnd\ART_EX\amd64\001\FXLERJL.inf"
    $doc64_path = $ScriptDir + $driv64bit_path
    
    # 64bit版ドライバのインストール
    cscript prndrvr.vbs -a -m $inf_printername -v 3 `
    -i "$doc64_path"
    }

最後に

普段はHTMLやJavascriptを使うので、Powershellは初ですが比較的簡単で驚きました。
batファイルだともう少し面倒くさかっただろうなぁ…

0
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?