LoginSignup
2
1

Outlookを起動して、添付付きメールを送信する。宛先はバッチに埋め込まず、バッチファイル名に埋め込む。

Last updated at Posted at 2023-07-11

海外利用を含むPCから情報を収集したい

というニーズから作成したバッチです。

本人が知らないうちに情報を抜き取るという、インベントリ管理ソフト方式は気持ち悪いので、「どんな情報を誰に送るのかは本人が分かる」方式にしてます。

特徴は、以下の3点。
1.アウトルック限定だが、メーラを起動し、情報を添付ファイルにした送信メールを作成するので、ユーザは送信ボタンを押すだけで、情報収集ができるし、誰に何が送られるのか、送信前に自分で確認できる。

2.メールの送信先はバッチに埋め込んでいなくて、バッチのファイル名から取得しているので、バッチを修正できない人でも、メールの宛先変更ができる。

3.アウトルックのバージョンが上がっても、しばらくはこのまま使えるようになっている。
です。

なお、アウトルックがインストールされていない場合は、情報収集を行い、ファイル作成だけ行うので、例えばサーバをリプレースする際に、新旧サーバで実行すると、環境の違いをファイルとして取得できます。

また、元々のニーズより、海外でも利用できるよう、文字コードをutf-8にして動かししています。
コマンドによって、日本語がshift-jisになってしまうケースもあり、文字化けするものもあります。
結果ファイルの日本語が化けているものを見る場合は、結果ファイルをshift-jisで開いてください。
逆にそうすることで、文字化けするコマンドもあるのですが、マイクロソフトさん、統一して!

メールアドレスをバッチファイル名に埋め込む。

このバッチの特徴的なつくりの一つ目です。

以下の方法で、バッチファイル名の()内を、メールアドレスとして、取り出しています。
同じ方法で、色々な情報をバッチファイル名に埋め込むことができますので、バッチは変更してほしくないけど、簡単にカスタマイズできるようにしたいときに応用できます。
もちろん、バッチにパラメータで渡す方法もありますが、それだと「一つのバッチだけで、ダブルクリックで起動」という単純な方法が出来ず、ショートカットにパラメータを入れて、ダブルクリックも、普通の人にはちょっと難しい、入力を促して入れるのは、ユーザにやさしくないため、こんな方法にしました。

仕組みは単純で、自分のファイル名の()で囲まれた部分だけを取り出し、余分なスペースがあれば取り除くだけです。

ここは、@DamashiGami(コーポれーしょん 騙し髪)さんのアドバイスで、ブラッシュアップしました。

当初は、

rem set your mail address below
set "mailto=%~n0"
rem trim e-mail address
set "mailto=%mailto:*(=%"
set "mailto=%mailto:)=%"
set "mailto=%mailto: =%"

でしたが、以下にしました。
こっちの方が、シンプルで良いですね。

for /f "tokens=2 delims=() " %%m in ("%~n0") do set mailto=%%m

アウトルックをコマンド起動する。

なぜかわかりませんが、outlook.exeにpathが通っていないので、、コマンド起動の際、フルパス起動が必要です。
しかも、このoutlook.exeってやつがどこにインストールされているかは、バージョンや、アプリの32bit/64bitなどで異なる。

検索するとやたら時間がかかるので、以下の通り、あたりを付けて検索してます。
バージョン30から10まで、新しい方から検索してるという、めっちゃ力業の検索(?)です。(^^ゞ
(なお、よくわからないけど、そもそも管理者モードでしか直接起動できないのもあるらしいけど、それはあきらめてます。)

ここも、@DamashiGami(コーポれーしょん 騙し髪)さんのアドバイスで、ブラッシュアップしました。

当初は、

rem find OUTLOOK.exe (I'll try several versions.)
for /l %%i in (30,-1,10) do ( 
  set outlook="C:\Program Files (x86)\Microsoft Office\root\Office%%i\OUTLOOK.EXE"
  if exist !outlook! exit /b
  set outlook="C:\Program Files (x86)\Microsoft Office\Office%%i\OUTLOOK.EXE"
  if exist !outlook! exit /b
  set outlook="C:\Program Files\Microsoft Office\root\Office%%i\OUTLOOK.EXE"
  if exist !outlook! exit /b
  set outlook="C:\Program Files\Microsoft Office\Office%%i\OUTLOOK.EXE"
  if exist !outlook! exit /b
  )
:no_outlook
rem echo OMG!
echo I can't find "OUTLOOK.exe" 
echo.
set send_mail=NO
exit /b

でしたが、以下にしました。力業でなく、スマートな方法です。

set Outlook=

for /f "delims=" %%O in ('dir "%ProgramFiles%\Microsoft Office\OUTLOOK.exe" /b /s 2^>nul') do (
  set "Outlook=%%O"
  exit /b
  )
for /f "delims=" %%O in ('dir "%ProgramFiles(x86)%\Microsoft Office\OUTLOOK.exe" /b /s 2^>nul') do ( 
  set "Outlook=%%O"
  exit /b
  )

:no_outlook
rem echo OMG!
echo I can't find "OUTLOOK.exe" 
echo.
set send_mail=NO
exit /b

最終的な完成系は以下の通りです。

サーバの情報を集める際に、サーバ名が同じでIPアドレスが違うケースがあったので、情報のファイル名にコンピュータ名だけでなく、IPアドレスも併用するようにしました。
IPv6は実際に利用してないのですが、アドレスに「:」が使われています。
「:」はファイル名に使えないため、置き替えてます。ついでにファイル名に使えない文字をいくつか置き替えました。

なお、AWSユーザなど、環境変数にクレデンシャル情報を入れているケースもあるかと思います。
これらは収集すべきではないので、それらキーワードを含む環境変数はあえて、収集から除外してます。
除外キーワードは、"_ACCESS_KEY VCAP_SERVICES credential secret password _API_KEY"です。

remにある通り、システムの概略(HW、windowsアップデートなど)、ネットワークはどうなっているか、どんなサービスが入っていて、何が動いているか、パワーシェルは動くか、administratorは無効になっているか、何がインストールされているかなど、集めてます。

gather_pc_info ( メールアドレス ).bat
@echo off

rem If you obtained this file with the extension .txt, please change the extension to .bat.

rem Gather information
rem 1.system
rem 2.ipconfig
rem 3.service
rem 4.environment variables
rem 5.powershell policy
rem 6.user
rem 7.registry

title  gather_pc_info
chcp 65001>NUL
setlocal enabledelayedexpansion

rem dummy
set ipaddr=   NOT_found_IP : NOT_found_IP

rem use 1st IP address for file name.
for /f "usebackq delims=" %%a in (`ipconfig^|findstr "IPv4"`) do (
  set ipaddr=%%a
  goto :found_ip
)
rem if no IPv4 found , use IPv6
for /f "usebackq delims=" %%a in (`ipconfig^|findstr "IPv6"`) do (
  set ipaddr=%%a
  goto :found_ip
)

:found_ip
call :trim_ip

echo Thank you for your cooperation.
echo This batch program gathers information about this PC and creates an email.
echo After processing, send the created email.
echo.

call :initial

rem make information_file header
echo %body%>%info_file%
echo %date% %time%  pwd=%cd% >>%info_file%

rem system information
echo ====================================================================>>%info_file%
echo 1.Gathering system information.
echo 1.Gathering system information.>>%info_file%
systeminfo >>%info_file%

rem ipconfig information
echo ====================================================================>>%info_file%
echo 2.Gathering ipconfig information.
echo 2.Gathering ipconfig information.>>%info_file%
ipconfig /all >>%info_file%

rem service information
echo ====================================================================>>%info_file%
echo 3.Gathering service information.
echo 3.Gathering service information.>>%info_file%
sc query state=all >>%info_file%

rem environment variables
echo ====================================================================>>%info_file%
echo 4.Gathering environment variables exclude credential.
echo 4.Gathering environment variables exclude [%exclude_variables%]. >>%info_file%
set  | findstr /v /i %exclude_variables% >>%info_file%

rem powershell
echo ====================================================================>>%info_file%
echo 5.Gathering powershell Policy.
echo 5.Gathering powershell Policy >>%info_file%
powershell (Get-ExecutionPolicy -list) >>%info_file%

rem local user information
echo ====================================================================>>%info_file%
echo 6.Gathering Local user information.
echo 6.Gathering Local user information. >>%info_file%
net user >>%info_file%
net user administrator >>%info_file%


rem registry information
echo ====================================================================>>%info_file%
echo 7.Gathering registry information.
echo 7.Gathering registry information.>>%info_file%
echo [HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall] >>%info_file%
reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /s >>%info_file%
echo ====================================================================>>%info_file%
echo [HKLM\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall] >>%info_file%
reg query HKLM\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /s >>%info_file%

echo ====================================================================>>%info_file%
echo finish.
echo finish. >>%info_file%
echo ====================================================================>>%info_file%

if "%send_mail%"=="YES" (
  "%outlook%" /c ipm.note /a %info_file% /m "%mailto%?subject=%subject%&body=%body%"
) else (
  echo.
  echo Please send file blelow to %mailto%.
  echo %info_file%
  echo Finish.
  echo.
  pause
)
endlocal
exit /b

:initial
rem set your mail address below
for /f "tokens=2 delims=() " %%m in ("%~n0") do set mailto=%%m

set subject=PC_Information
set body=check %computername% by %username% at %DATE% %TIME%
set info_file="%cd%\pc_info_%computername%(%ipaddr%).txt"
set send_mail=YES

set exclude_variables="_ACCESS_KEY VCAP_SERVICES credential secret password _API_KEY"

set Outlook=

for /f "delims=" %%O in ('dir "%ProgramFiles%\Microsoft Office\OUTLOOK.exe" /b /s 2^>nul') do (
  set "Outlook=%%O"
  exit /b
  )
  
for /f "delims=" %%O in ('dir "%ProgramFiles(x86)%\Microsoft Office\OUTLOOK.exe" /b /s 2^>nul') do ( 
  set "Outlook=%%O"
  exit /b
  )

:no_outlook
rem echo OMG!
echo I can't find "OUTLOOK.exe" 
echo.
set send_mail=NO
exit /b


:trim_ip
set trim_head=%ipaddr:~0,1%
if not "%trim_head%"==":" (
  set ipaddr=%ipaddr:~1%
  goto :trim_ip
)
set ipaddr=%ipaddr:~2%
set ipaddr=%ipaddr::=.%
set ipaddr=%ipaddr:\=_%
set ipaddr=%ipaddr:/=_%
set ipaddr=%ipaddr:?=_%
exit /b
2
1
2

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
2
1