ラベル システム情報 の投稿を表示しています。 すべての投稿を表示
ラベル システム情報 の投稿を表示しています。 すべての投稿を表示

2015年10月30日金曜日

◆OSバージョンを調べる

Finding Operating System Version - Power Tips - PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources

[Environment]クラスのstaticプロパティで取ってこれるよってだけの話。

 

メンバーの値を一覧表示するにはどうするんだろう・・・。

とりあえずベタにやるとこんな感じ?

>[environment] | gm -static -type property | ?{$_.name -ne "stacktrace"} |
    %{$_.name + " = " + [Environment]::($_.name)}

stacktraceは結果が長くなるのでとりあえず除外している。

image

2015年9月17日木曜日

◆svchostでホストされているサービスを表示する

Analyzing svchost Processes - Power Tips - PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources

 

サービスとプロセスをぶつけてsvchostでホストされているサービスを一覧表示する。(Ver3)

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018

$service = @{
  Name 
= 'Service'
  Expression =
 
    {
     
if ($_.Name -eq 'svchost'
)
      {
      
$processID = $_.
ID
       (
$serviceList.$processID).Name -join ', '
      }
    }
}


$serviceList = gwmi -Class Win32_Service |
  group -Property ProcessID -AsString -AsHashTable

ps |
  Select -Property Name, ID, CPU, $service |
  Out-GridView

image

2014年11月7日金曜日

◆コンピュータの説明を設定する

コンピュータの説明とは以下の部分。

image

 

>$op = gwmi Win32_OperatingSystem
>$op.Description = "仕事用コンピュータ"
>$op.Put()

 

Set-WmiInstanceを使ってもできる。

が、ヘルプを見ると以下のような感じで出来そうなのだが、旨くいかない。

>Set-WmiInstance -Class Win32_OperatingSystem -arguments @{Description="会社のコンピュータ"}

 

Set-WmiInstance : サポートされていないパラメーターです
発生場所 C:\Users\kumagai_mitsugu\AppData\Local\Temp\b67c3bf9-5b3d-4e70-bd99-d0ace79a285f.ps1:1 文字:16
+ Set-WmiInstance <<<<  -Class Win32_OperatingSystem -arguments @{Description="会社のコンピュータ"}
    + CategoryInfo          : InvalidOperation: (:) [Set-WmiInstance]、ManagementException
    + FullyQualifiedErrorId : SetWMIManagementException,Microsoft.PowerShell.Commands.SetWmiInstance

仕方が無いので、とりあえずこんな感じで。

>gwmi Win32_OperatingSystem | Set-WmiInstance -arguments @{Description="会社のコンピュータ"}

2014年10月1日水曜日

◆閾値以下の空き容量のHDドライブがあったら警告する

>if(Get-PSDrive | ?{$_.used} | %{$_.Free / ($_.used + $_.free)} | ?{$_ -lt 0.6}){"HD容量不足発生";Read-Host}

image

ここでは閾値として0.6(60%)を指定している。

後はこれをスタートアップで実行しても良いし、サーバーなどを巡回しても良いしって感じだろうか。

---

質問があったので補足

各ドライブの空き容量を表示するにはとりあえずこんな感じで

>Get-PSDrive | ?{$_.used} | select name , @{name="残容量(%)";expression={[int](($_.Free / ($_.used + $_.free))*100)}}

image

2014年3月11日火曜日

◆WindowsUpdate情報、McAfee情報

McAfeeのDatバージョンとWindowsUpdateの最終更新日を毎月報告せよ、と会社からのありがたーいお達し。

いまどき、「力技でやるんかい」って突っ込みはうちの会社には通用しない。

これまでは某H社の馬鹿高いツールを使っていたのだが、よっぽど評判が悪いのか待てど暮らせどWindows8には対応してこない。

ActiveDirectoryでも入れてちょっとしたツールでも作って情報収集すれば一発なのだが、何があっても(目先の)節約至上主義の会社なので力技をこよなく好む。

とりあえず自分だけは楽をしようとスクリプトを書いてみた。

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030

param(
  $mailFrom,
  $mailTo
)
$path = 
"hklm:SOFTWARE\Network Associates\ePolicy Orchestrator\Application Plugins\VIRUSCAN8800"

$prop = Get-ItemProperty -Path $path
#-----
$Session = New-Object -ComObject Microsoft.Update.Session 
$Searcher = $Session.CreateUpdateSearcher() 
$current = $Searcher.QueryHistory(1,1) | select -ExpandProperty date

#-----
$mess1 = "McAfee Datバージョンは 【{0}】 です。" -f $prop.DATVersion
$mess2 = "WindowsUpdate最終適用日は 【{0}】 です。" -f $current

#-----
if($mailFrom -and $mailTo){
  Send-MailMessage    -To $mailTo
                                    `
                     
-From $mailFrom
                                `
                     
-Subject "報告レポート"
                          `
                     
-SmtpServer "hoge.ccc.co.jp"
                  `
                     
-Body "`r`n$mess1`r`n$mess2"
                   `
                     
-Encoding  ([System.Text.Encoding]::
Default)
}

else{
  Add-Type -AssemblyName System.Windows.Forms
  [WIndows.Forms.MessageBox]::Show("$mess1`r`n$mess2") | Out-Null
}

McAfeeのDatバージョンについてはレジストリに持っている値を使ってみました。(確実かどうかは判りません)

WindowsUpdateについては、きっとやり方が色々あるのでしょうが、今回は以下のサイトを参考にしました。
Hey, Scripting Guy! コンピューターに追加されたすべての更新プログラムの一覧を取得する方法はありますか

12行目の「QueryHistory」メソッドで履歴の開始位置と件数を指定する様なので最新の1件を取得してみました。

そのまま実行すると結果をメッセージボックス表示、パラメータを指定するとメール送信としてみました。

------

WindowsUpdateの日付については「Get-HotFix」なんてコマンドレットがあるので、そちらのほうがきっと簡単ですよね・・・。

2013年2月4日月曜日

◆リモートマシンのドライブにデフラグが必要か調べる

PowerShell: ◆各ドライブにデフラグが必要か調べるのリモート版。

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018

function DefragAnalysis($computer)
{
 
gwmi win32_logicaldisk -Filter DriveType=3 | %
{
   
$drive = $_.
DeviceID
   
$disk =
 
     
gwmi win32_volume -Filter "DriveLetter='$drive'" -Comp $computer
    $def = 1 | select ComputerName,Drive,DefragRecommended
    $def.ComputerName = $computer
    $def.Drive = $drive
    $def.DefragRecommended =
 
      (
Invoke-WmiMethod -Path $($disk.
__PATH)  `
          
-Name DefragAnalysis).
DefragRecommended
   
$def
  } 
}


$computers = "sv01","sv02","sv03"
$computers | %{DefragAnalysis $_} | ft -AutoSize

「Win32_volume」オブジェクトの「DefragAnalysis」メソッドを「Invoke-WmiMethod」で呼ぶ。

ただし、リモートに対してオブジェクトを渡せないので「__PATH」プロパティに持っているシリアライズされた情報を渡すって感じでしょうか。

image

2012年12月21日金曜日

◆IISのログを取得する2(結合、複合化、解凍)

PowerShell: ◆IISのログを取得する1(取得、圧縮、暗号化、分割)で作成したファイルをもとに戻すスクリプト。

ファイルのパターンは以下のように別れる。

  1. 圧縮ファイル(ログ.zip)
  2. 暗号化ファイル(ログ.dat)
  3. 非暗号化分割ファイル(ZIP.001~ZIP.nnn)
  4. 暗号化分割ファイル(DAT.001~DAT.nnn)

パラメータを指定せずにスクリプトを実行するとファイル選択ダイアログが表示されるので、対象のファイルを選択する。
(分割ファイルの場合はすべての分割ファイルを選択する。)

ファイル選択ダイアログはPowershellをSTAモードで実行する必要があるため「OutLogメイン.ps1」経由で「結合.ps1」を呼んでいる。
(V3からはSTAがデフォルトになったようだが。)

パラメータ指定で実行する時は、「InPath」に入力ファイル(分割ファイルの時はフォルダ指定)、「OutPath」に出力フォルダを指定する。

出力フォルダに「ログyyyyMMdd_HHmmss」の名前でフォルダを作り、その中にログを復号・解凍する。

image

<OutLogメイン.ps1>

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021

param(
 
[string]$InPath,
  [string]$OutPath="desktop"
)
#staで起動するために「OutLogメイン経由で結合.ps1を起動する。Ver3では不要」

$currntPath = Split-Path $myInvocation.MyCommand.path
Start-Transcript (Join-Path $currntPath "OutLog.log")

<# 分配演算子は使えない(バグ?)
$param = @{}
$param.OutPath = $OutPath
if($InPath){$param.InPath = $InPath}
#>

$scriptPath = Join-Path $currntPath "結合.ps1"
if($InPath
){
   
Powershell -sta $scriptPath $InPath $OutPath
}else
{
   
Powershell -sta $scriptPath -OutPath $OutPath
}
Stop-Transcript

<結合.ps1>

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174

param(
 
$InPath,
  $OutPath="desktop"
)

function MakeDir($path
){
 
$f = mkdir $path
  Write-Host "mkdir $path" -ForegroundColor Cyan
}

$currntPath = Split-Path $myInvocation.MyCommand.path
$initFolder = [Environment]::GetFolderPath("desktop")

#入力パス取得&パターン判定
##ZIP:zipファイル、DAT:暗号化(DAT)ファイル、
##ZIPn:圧縮分割フォルダ、DATn:暗号化分割フォルダ

$inPtn = "";[String[]]$inPathFiles=""
if($InPath
){
 
if(Test-Path $InPath -PathType Leaf){  #ファイル指定--
  if((Split-Path $InPath -Leaf) -eq "ログ.zip"
){
   
$inPtn = "ZIP"
    $inPathFiles[0] = $InPath
  }else
{
   
if((Split-Path $InPath -Leaf) -eq "ログ.dat"
){
     
$inPtn = "DAT"
    $inPathFiles[0] = $InPath
    }else
{
     
Write-Error "◆入力指定誤り◆";return
    }
  }
  }
elseif(Test-Path $InPath -PathType Container){  #フォルダ指定--
    if(dir $InPath -Filter zip.*
){
   
$inPtn = "ZIPn"
    $inPathFiles = dir $InPath -Filter zip.* | select -ExpandProperty fullname
  }elseif(dir $InPath -Filter dat.*
){
   
$inPtn = "DATn"
    $inPathFiles = dir $InPath -Filter dat.* | select -ExpandProperty fullname
  }else
{
   
Write-Error "◆入力指定誤り◆";return
  }
  }
else
{
   
$InPath -eq ""
    $InPath
    if($InPath){"True"
}
   
Write-Error "◆入力指定誤り◆";return
  }
}
else
{
 
Add-Type -AssemblyName System.Windows.Forms
 
 
$dl = New-Object System.Windows.Forms.OpenFileDialog
 
 
$dl.Title = "ファイルの選択"
 
 
$dl.InitialDirectory = $initFolder
 
 
$dl.Multiselect = $true
  if($dl.ShowDialog() -eq "OK"
){ 
     
$inPathFiles = $dl.
FileNames
   
switch (Split-Path $inPathFiles[0] -Leaf
) {
     
"ログ.zip" {$inPtn = "ZIP";break
}
     
"ログ.dat" {$inPtn = "DAT";break
}
     
"dat.001" {$inPtn = "DATn";break
}
     
"zip.001" {$inPtn = "ZIPn";break
}
     
default
 {
       
Write-Error "◆入力指定誤り◆";return
      }
    }
  }
else{return
}
}


#出力先フォルダ作成
if((Test-Path $OutPath) -eq $false
){
 
$spcialFolder = [environment+specialfolder] |
 
   
Get-Member -Static -type property | select -expand name
  if($spcialFolder -contains $OutPath
){
   
$OutPath = [Environment]::GetFolderPath($OutPath
)
  }
else
{
   
Write-Error "◆出力パス誤り◆";return
  }
}

$outPathRoot =
 
 
Join-Path $OutPath ("ログ" + (Get-Date).ToString("yyyyMMdd_HHmmss"))
MakeDir $outPathRoot 


#◆◆◆ パターンに応じて「結合」「複合化」「解凍」実行をコントロール 
function Executer
{
 
 
switch ($inPtn
) {
   
"ZIP"
 {
     
$zipTargetFile = $inPathFiles[0]
      break
    }
   
"DAT"
 {
     
$decryptTargetFile = $inPathFiles[0]
      . F_Decrypt
      $zipTargetFile = $DecryptedFileName
      break
    }
   
"ZIPn"
 {
     
$joinTargetFiles = $inPathFiles
      . F_Join
      $zipTargetFile = $joinedFileName
      break
    }
   
"DATn"
 {
     
$joinTargetFiles = $inPathFiles
      . F_Join
      $decryptTargetFile = $joinedFileName
      . F_Decrypt
      $zipTargetFile = $DecryptedFileName
      break
    }
   
default
 {
     
break
    }
  }
 
. F_UnZip
  dir $outPathRoot | ?{$_.PSIsContainer -eq $false} | del
}

#◆結合
function F_Join
{
 
#$inpPath = "F:\Desktop\test"
  #$outPath = "F:\Desktop\Dec.txt"
  $joinedFileName = Join-Path $outPathRoot "結合ログ.tmp"
  $dest = New-Object System.IO.FileStream
(
   
$joinedFileName,[IO.FileMode]::Create,[IO.FileAccess]::
Write)
 
Get-Item $joinTargetFiles | sort name | %
{
     
$data = [System.IO.File]::ReadAllBytes($_.
FullName)
     
$dest.Write($data,0,$data.
Length)
  }
 
$dest.
Dispose()
 
Write-Host "結合しました" -ForegroundColor Cyan
}

#◆複合化
function F_Decrypt
{
 
$KEYFilePath = Join-Path $currntPath "KEY.dat"
  $IVFilePath = Join-Path $currntPath "IV.dat"
  if((Test-Path $KEYFilePath) -and (Test-Path $IVFilePath
)){
   
$desKey = [System.IO.File]::ReadAllBytes($KEYFilePath
)
   
$desIV = [System.IO.File]::ReadAllBytes($IVFilePath
)
  }
else
{
   
$desKey = [byte[]](1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1
)
   
$desIV = [byte[]](0,0,0,0,0,0,0,0
)
  }
 
$DecryptedFileName = Join-Path $outPathRoot "ログ.zip"
  $src = [System.IO.File]::ReadAllBytes($decryptTargetFile
)

 
$desProvider =
 
   
New-Object System.Security.Cryptography.TripleDESCryptoServiceProvider
  $decryptor = $desProvider.CreateDecryptor($desKey,$desIV
)
 
$ms = New-Object System.IO.MemoryStream
  $cs = New-Object System.Security.Cryptography.CryptoStream
(
   
$ms, $decryptor, [System.Security.Cryptography.CryptoStreamMode]::
Write)
 
$cs.Write($src,0,$src.
Length)
 
$cs.
Close()
 
[System.IO.File]::WriteAllBytes($DecryptedFileName,$ms.
ToArray())
 
Write-Host "複合しました" -ForegroundColor Cyan
}

#◆解凍
function F_UnZip
{
 
$dllPath = Join-Path $currntPath "ICSharpCode.SharpZipLib.dll"
  [Void][Reflection.Assembly]::LoadFile($dllPath
)
 
$zipFile =
 
     
New-Object ICSharpCode.SharpZipLib.Zip.FastZip
  $zipFile.RestoreAttributesOnExtract  = $true
  $zipFile.RestoreDateTimeOnExtract = $true
  $zipFile.CreateEmptyDirectories = $true
  #$zipFile.ExtractZip($DecryptedFileName,$outPathRoot,"")
  $zipFile.ExtractZip($zipTargetFile,$outPathRoot,""
)
 
Write-Host "解凍しました" -ForegroundColor Cyan
}

. Executer  #「結合」「複合化」「解凍」呼び出し
Write-Host "◆終了しました◆" -ForegroundColor Magenta