忍者ブログ

ぷーちんの点

ぷーちんの備忘録です。 今やっている事や知識とかを書いていきます。

[PR]

×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

WindowsServerログ収集③

★PowerShellでイベントログの取得★

【PowerShellの作成】
・収集イベント:システムとセキュリティの2つ
・警告・エラーのログを取得する
・取得したログを[CSV]ファイルに出力する
・1日から翌月1日の1ヶ月分を取得(例:7/1-8/1のログ)

------------------------------------------------------------------------------

#当月の1日を取得
$this_month = Get-Date -Day 1 -Hour 0 -Minute 0 -Second 0
#先月の1日を取得
$last_month = Get-date ($this_month).AddMonths(-1)
#ホスト名の取得
$host_name = hostname
#ファイル名
$file_name = "c:\auto_log\log\"+$host_name+"_"+(get-date -f yyyyMM) + "_"
#セキュリティのログ・ファイル作成
get-eventlog -logname security -entrytype Error,FailureAudit,warning -after $last_month -before $this_month |select entrytype,TimeGenerated,Source,EventID,MachineName,Message |export-csv $file_name"security.csv" -encoding utf8
#システムのログ・ファイル作成
get-eventlog -Logname system -entrytype Error,FailureAudit,warning -after $last_month -before $this_month |select entrytype,TimeGenerated,Source,EventID,MachineName,Message |export-csv $file_name"system.csv" -encoding utf8
#ファイルの有無確認し、なければ-1でエラーを返す
IF( (Test-Path -path $file_name"security.csv" ) -and (Test-Path -path $file_name"system.csv") ){
exit 0
}Else{
exit -1
}

------------------------------------------------------------------------------
---PSの基本---
・PowerShellのScriptの拡張子[.ps1]
・コメント:[#]
・変数:[$]

---変数---
[this_month]
  →当月の日付を格納(時刻は0:00:00にする)
[last_month]
  前月の日付を格納する
[host_name]
  →コンピュータ名を格納して、どのホストかをわかるようにする
[file_name]
  →絶対パスでディレクトリとファイル名を格納する

---内容---
・セキュリティのログをCSVで取得する
・システムのログをCSVで取得する
・セキュリティログとシステムログが[log]フォルダにあるかを確認
  exit=0:正常終了。それ以外は異常終了。

---操作方法---
○スクリプト作成○
1.上記ソースをメモ帳に貼り付け[eventlog.ps1]という名前で保存をする。
  保存先:c:\auto_log\
2.PSの権限を変更する
  [アクセサリ]→[WindowsPowerShell]→[WindowsPowerShell]
  右クリックをして[管理者で実行]を選択する。
  [Set-ExecutionPolicy remotesigned]コマンドを打つ。
  確認のため、[Get-ExecutionPolicy]コマンドでremotesignedと結果が出るか確認する。
  remotesignedなら問題なし。
3.保存したスクリプトを実行し、エラー等でないか確認する。
 (権限がないと弾かれる場合は、右クリックで[管理者として実行]する必要がある)

---PSの権限---
RestrictedPowerShellのソフトだけで実行する場合。スクリプト実行不可。(デフォ設定)
AllSigned信頼できる発行元が署名したスクリプトのみを実行できます。
RemoteSignedDLしたスクリプトは信頼できる発行元が署名した場合にのみ実行できます。
Unrestricted制限なし。すべての Windows PowerShell スクリプトを実行できます。
記述したScriptをPSのソフト上で1つずつ実行する事が出来る。
デフォの権限[Restricted]では、その操作のみを許可している。
そのため、一度権限をScript実行許可のある[RemoteSigned]に変更する必要があるわけです。
今回はローカル作業のみなので[remotesigned]にしております。

ぷーちんが、PSのスキなところは
PSのソフト上で実行するときに[Tab補完]が使える事。
例えば
[get-exec]まで入力して[Tab]キーを押すと[Get-ExecutionPolicy]と出てきます。
[get-]だけでも、取得できるコマンドが出てきますし、パラメータも出てきてくれます。
忘れても出てくるので、楽です(・ω<)
なので、ド素人のぷーちんでも出来るわけです。

---Get-Eventlogの説明---
get-eventlog -logname security -entrytype Error,FailureAudit,warning -after $last_month -before $this_month |select entrytype,TimeGenerated,Source,EventID,MachineName,Message |export-csv $file_name"security.csv" -encoding utf8

[get-eventlog]:イベントログを取得
[-logname]:何のログかを記載(今回はセキュリティとシステムですね)
[-entrytype]:ログのフィルターです(今回はエラーと重大と渓谷です)
[-after]:いつからのログか(変数で値を入れてます)
[-before]:いつまでのログか(変数で値を入れてます)
[|select]:取得ログの項目を選択してます。ここの記載順番でCSVに書き込まれます。
[export-csv]:csvファイルに出力します。その後にくる値は絶対パスでのファイル名になります。念の為、エンコードしてます。

PSはパイプ[|]で左の値を右へ渡しています。
[get-○○○]や[set-○○○]や[export-]等動詞をパイプで繋がずに1文で記述することは出来ません。
値を右へ渡すためにパイプを使って動詞を書いていきます。
今回ので言えば
get-eventlogで取得したデータをselectでソートし、export-csvが値を受け取りCSVへ出力する。
左から右へ左から右へと値を渡す形になってます(*´艸`*)

+++++++++++++++++++++++++++++++++++++++++++
ぷーちんです。
今日はPSの記述でした。
PSが一番説明長いかなーと思ってます。
PS初めての人でも、多分なんとなくわかるかと・・・。
PSでコマンド打てばすぐ掴めると思うンですけどね(*´艸`*)
きっとSEとかNW管理者が利用するのでしょうね。

PSといえば、office365でもPSが利用出来ます。
ただ、PSのソフト自体が違うようでDLが必要みたいですね。
PS色々あってよくわかりませんw

わからなければ、
[get-help]とか、[get-command]で調べるのが早そうですね。
[get-help get-executionpolicy]でexecutionpolicyのヘルプが出てきます。
ワイルドカード[*]も使えます。
PSぷーちんでも使える感が素敵ですww
もっとスキル向上に努めます・・・!

では、次回はメール送信のVBSのでお会いしましょう(╹◡╹)

拍手[0回]

PR

コメント

お名前
タイトル
文字色
メールアドレス
URL
コメント
パスワード Vodafone絵文字 i-mode絵文字 Ezweb絵文字

montre homme rolex daytona faux

cartierbraceletlove Говорит Илья очень много, и при этом повторяет одну и ту же мысль по три раза разными словами. Как путин, слов много, а толку нет)
montre homme rolex daytona faux http://www.movement-watch.cn/

copy cartier bangle price

cartierbraceletlove I just attempted to negotiate the account creation process on the IRS website. It was the buggiest web form I have ever encountered. First I was unable to scroll on the page when fields went outside the bottom window margin. i worked around that by using the tab button. Second the rules for creating a password are self-contradictory indicating that you can and cannot use the ‘@’ symbol. Half the time clicking ‘Continue’ generated an error and I had to start over. The designers of this web form should be ashamed.
copy cartier bangle price http://www.fashionlovebangle.cn/replica-cartier-love-bracelet-plated-18k-yellow-gold-4-diamonds-p-258.html

sautoir alhambra van cleef réplique

cartierbraceletlove Parece um super esportivo desenhado pela equipe do gta
sautoir alhambra van cleef réplique http://www.holidayvcagift.cn/fr/fake-van-cleef-necklace-c1/

replique montres cle de cartier

Hey! Quick question that’s totally off topic. Do you know how to make your site mobile friendly? My blog looks weird when browsing from my iphone4. I’m trying to find a template or plugin that might be able to fix this issue. If you have any recommendations, please share. With thanks!
replique montres cle de cartier http://www.aaawatch.cn/fr/cl%C3%A9-watch-series-c91/

hermes picotin 24 bags price

I just called Friendship Animal hospital and they said that they are “not making comments available to the media” about this matter. I told them that I am a resident in the Logan Circle area and a client and dog owner just looking for safety information, but she said that was their policy on this matter. Read into that whatever you will.
hermes picotin 24 bags price http://www.accessoires-mode.in/hermes-picotin-30-c18_20/

プロフィール

HN:
ぷーちん
性別:
非公開
職業:
インフラエンジニアになりたい
自己紹介:



P R





ムームードメイン
ムームードメイン
ムームードメイン
ムームードメイン
 


Download Vivaldi Web Browser Today!


ブログ内検索