コンテンツへスキップ

compmgmt.msc:コンピュータの管理
devmgmt.msc :デバイスマネージャ
diskmgmt.msc:ディスクの管理
eventvwr.msc:イベントビューア
fsmgmt.msc :共有フォルダ
gpedit.msc :グループポリシー
lusrmgr.msc :ローカルユーザーとグループ
perfmon.msc :パフォーマンス
rsop.msc :ポリシーの結果セット
services.msc:サービス
secpol.msc :ローカルセキュリティ設定

$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$excel.DisplayAlerts = $true


# 既存のブックを開く場合
$book = $excel.Workbooks.Open($bookfile)
# 新規の場合
$book = $excel.workbooks.add()

# シート追加
$excel.Worksheets.Add()

# シートの名前を変更
$book.Sheets(1).Name = "その1"
$book.Sheets(2).Name = "その2"
$book.Sheets(3).Name = "その3"


# セルに入力
$sheet = $book.Sheets("その1")
# $sheet.Cells.Item(行,列)
$sheet.Cells.Item(1, 2) = "テスト"
$sheet.Cells.Item(1, 3) = "テスト2"
$sheet.Cells.Item(1, 4) = "テスト3"
$sheet.Cells.Item(2, 2) = "テスト3--------------------------"

# 罫線を引く
$sheet.Range("A1:C3").Borders.LineStyle = 1

# 列の幅を自動調整
$sheet.Columns.AutoFit()

# セルの色を変更する。
$sheet.Cells.Item( 2, 2).Interior.ColorIndex = 3

# 終了
$book.SaveAs("${HOME}\Desktop\test.xlsx")
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($sheet)

 

・メモリ

# Memory Stress test
$maxMemoryUsed = 75 # percentage
while($true){
    $p = Get-WmiObject Win32_OperatingSystem | %{(($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)/$_.TotalVisibleMemorySize) * 100}
    if($p -le $maxMemoryUsed){
        try{
            $a += "m" * 100MB
        } catch [Exception]{
            $b += "m" * 100MB
        }
    }
    write-host("memory used [{0}]" -f $p.toString())
    sleep 1
}

・CPU

while($true){ echo $i;$i++ }

複数powershellを起動(start-process powershell )し上のコマンドを実行する。

・ディスク 1Gバイトのダミーファイルを作成する。

fsutil file createnew testfile (1024*1024*1024)

・stressTest.ps1 作成中
# Memory Stress test
param(
    [parameter(mandatory=$false)][string]$type,
    [parameter(mandatory=$false)][int]$percent,
    [parameter(mandatory=$false)][string]$driveLetter,
    [parameter(mandatory=$false)][string]$dummyFileName = "dummyFile",
    [parameter(mandatory=$false)][switch]$help
)

function memoryTest($percent) {
    while($true){
        $p = Get-WmiObject Win32_OperatingSystem | ForEach-Object {(($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)/$_.TotalVisibleMemorySize) * 100}
        if($p -le $percent){
            try{
                $a += "m" * 100MB
            } catch [Exception]{
                $b += "m" * 100MB
            }
        }
        write-host("memory used [{0}]" -f $p.toString())
        sleep 1
    }
}

function cpuTest($percent){
    while($true){
        $newestJobId = ""
        $p = (Get-WmiObject Win32_PerfFormattedData_PerfOS_Processor | where-object name -eq "_Total").PercentProcessorTime
        if($p -le $percent){
            Start-Job -ScriptBlock {
                write-host($i)
                $i++
            } | Out-Null
            $job = "Start-job"
            sleep 1
        } else {
            $id =(get-job).id
            $newestJobId=$id[-1]
            stop-job $newestJobId
            $job = "Stop-job"
            
        }
        write-host("cpu used [{0}]%, [{1}] {2}" -f $p, $job, $newestJobId)
    }
}

function diskCapacity($percent,$driveLetter){
    $dummyFilePath = join-path ($driveLetter + ":\") $dummyFileName
    fsutil file createnew $dummyFilePath (1024*1024*1024)
}

function usage(){
    write-host("usage:")
    write-host("stressTest.ps1")
    write-host("    -type memory -percent %")
    write-host("    -type cpu -percent %")
    write-host("    -type diskCapacity -percent % -driveLetter (c|d|e...)")
}

if(($type -eq "memory") -And ($percent)){
    memoryTest $percent
} elseif(($type -eq "cpu") -And ($percent)){
    cpuTest $percent
} elseif(($type -eq "diskCapacity") -And ($driveLetter -ne "") -And ($percent)){
    diskCapacity $percent $driveLetter
} elseif($help){
    usage
} else {
    usage
}

 

アクティブになっている電源設定の確認

PS E:\> powercfg /list

既存の電源設定 (* アクティブ)
-----------------------------------
電源設定の GUID: 381b4222-f694-41f0-9685-ff5bb260df2e  (バランス)
電源設定の GUID: 92aa4406-3cbf-471d-bb08-1fdc071fb5c2  (UMA Mode) *
電源設定の GUID: c2a836a6-020a-41ff-85b2-ed779d969fcd  (カスタム プラン 1)

電源設定の確認

PS E:\> powercfg /q
電源設定の GUID: 92aa4406-3cbf-471d-bb08-1fdc071fb5c2  (UMA Mode)
  サブグループの GUID: 0012ee47-9041-4b5d-9b77-535fba8b1442  (ハード ディスク)
    GUID エイリアス: SUB_DISK
    電源設定の GUID: 6738e2c4-e8a5-4a42-b16a-e040e769756e  (次の時間が経過後ハード ディスクの電源を切る)
      GUID エイリアス: DISKIDLE
      利用可能な設定の最小値: 0x00000000
      利用可能な設定の最大値: 0xffffffff
      利用可能な設定の増分: 0x00000001
      利用可能な設定の単位: 秒
    現在の AC 電源設定のインデックス: 0x000004b0
    現在の DC 電源設定のインデックス: 0x0000012c

~以下省略~

アクティブをバランスに設定

PS E:\> powercfg -setactive SCHEME_BALANCED

アクティブをUMAに設定

powercfg -setactive ((powercfg /list | select-string UMA) -split " ")[2]

 

# logger.ps1
class Logger{
    [string]$file
    [bool]$isWriteHost
    [bool]$isOutputFile

    Logger(){
        $this.isWriteHost = $TRUE
        $this.isOutputFile = $TRUE
    }

    [void]setLogfile([string]$file){
        $this.file = $file
    }

    [string]buildMessage([string]$message){
        $dt = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        $message = ("{0} $message" -f $dt, $message)
        return $message
    }

    [void]outputFile([string]$message){
        if($this.isOutputFile){
            write-output $message | Add-content $this.file -Encoding Default
        }
    }

    [void]outputConsole([string]$message){
        if($this.isWriteHost){
            write-host($message)
        }
    }
}

class MyLogger:Logger{
    [int]$logLevel
    [System.Collections.Hashtable]$hash_level

    logger(){
        $this.logLebel = 1
    }

    [void]setLevel([string]$level){
        $this.hash_level = new-object System.Collections.Hashtable
        $this.hash_level."DEBUG" = 1
        $this.hash_level."INFO" = 2
        $this.hash_level."WARNING" = 3
        $this.hash_level."ERROR" = 4
        $this.hash_level."CRITICAL" = 5
        $this.logLevel = $this.hash_level.$level
    }

    [void]output([string]$message,[int]$num){
        $num = $num
        $message = $this.buildMessage($message)
        if($num -ge $this.logLevel){
            $this.outputFile($message)
            $this.outputConsole($message)
        }
    }

    [void]debug([string]$msg){
        $message = ("DEBUG: {0}" -f $msg)
        $this.output($message,1)
    }
    [void]info([string]$msg){
        $message = ("INFO: {0}" -f $msg)
        $this.output($message,2)
    }
    [void]warning([string]$msg){
        $message = ("WARNING: {0}" -f $msg)
        $this.output($message,3)
    }
    [void]error([string]$msg){
        $message = ("ERROR: {0}" -f $msg)
        $this.output($message,4)
    }
    [void]critical([string]$msg){
        $message = ("CRITICAL: {0}" -f $msg)
        $this.output($message,5)
    }
}

使い方

function outlog(){
    $logger.debug("test loglevel is debug.")
    $logger.info("test loglevel is info.")
    $logger.warning("test loglevel is warning.")
    $logger.error("test loglevel is error.")
    $logger.critical("test loglevel is critical.")
}

. E:\my-document\Programing\powershell\logger.ps1

del .\test.log

#-------------------------------------#
$logfile = "test.log"
$logger = new-object MyLogger
$logger.setLogfile($logfile)

write-host("LogLevel = 指定なし(DEBUG), isWriteHost = 指定なし(TRUE), isOutputFile = 指定なし()TRUE")
outlog
sleep(5)

write-host("")
write-host("LogLevel = WARNING, isWriteHost = TRUE, isOutputFile = TRUE")
$logger.isWritehost = $TRUE
$logger.isOutputFile = $TRUE
$logger.setLevel("WARNING")
outlog
sleep(5)

write-host("")
write-host("LogLevel = CRITICAL, isWriteHost = FALSE, isOutputFile = TRUE")
$logger.isWritehost = $FALSE
$logger.isOutputFile = $TRUE
$logger.setLevel("CRITICAL")
outlog
sleep(5)

write-host("")
write-host("LogLevel = DEBUG, isWriteHost = TRUE, isOutputFile = FALSE")
$logger.isWritehost = $TRUE
$logger.isOutputFile = $FALSE
$logger.setLevel("DEBUG")
outlog
sleep(5)

 

作成中

<# 
    パラメータ
    # network
    ifName,ifIndex,Ipaddress,MacAddress,Gateway
    # bonding
    @(ifIndex1,ifIndex2),Ipaddress,MacAddress,Gateway
    # routing
#>

$server = New-Object System.Collections.Hashtable

# ルーティング
$routing1 = ("aのルーティング","bのルーティング")
$routing2 = "cのルーティング"

$server.s001 = @{
    # key は ifIndex
    "network" = @{
        1 = @{
            "name"      = "インターフェース1";
            "ipaddress" = "192.168.1.100";
            "netmask"   = "255.255.255.0";
        };
        2 = @{
            "name"      = "インターフェース2";
            "ipaddress" = "192.168.2.100";
            "netmask"   = "255.255.255.0";
        };
        3 = @{
            "name"      = "インターフェース3";
            "ipaddress" = "";
            "netmask"   = "";
        };
        4 = @{
            "name"      = "インターフェース4";
            "ipaddress" = "";
            "netmask"   = "";
        };
    };

    "bonding" = @{ 
        1 = @{
            "interfaceList" = @(3,4);
            "ipaddress"     = "192.168.3.100";
            "netmask"       = "255.255.255.0";
        };
    };

    "routing" = @($routing1,$routing2);
}

$server.s002 = @{
    "network" = @{
        1 = @{
            "name"      = "インターフェース1";
            "ipaddress" = "192.168.1.101";
            "netmask"   = "255.255.255.0";
        };
        2 = @{
            "name"      = "インターフェース2";
            "ipaddress" = "192.168.2.101";
            "netmask"   = "255.255.255.0";
        };
        3 = @{
            "name"      = "インターフェース3";
            "ipaddress" = "";
            "netmask"   = "";
        };
        4 = @{
            "name"      = "インターフェース4";
            "ipaddress" = "";
            "netmask"   = "";
        };
    };

    "bonding" = @{ 
        1 = @{
            "interfaceList" = @(3,4);
            "ipaddress"     = "192.168.3.101";
            "netmask"       = "255.255.255.0";
        };
    };

    "routing" = $routing;
}
<#
#>

param(
    $servername
)

#
$s = $server.$servername

# Network Adapter設定
write-host("# ネットワークアダプター設定")
foreach($key in $s.network.keys) {
    $nic = $s.network.$key
    if($nic.ipaddress){
        write-host("ifIndex=[{0}],name=[{1}],ipaddress=[{2}],netmask=[{3}]" -f $key,$nic.name,$nic.ipaddress,$nic.netmask)
    }
}

# Bonding設定
write-host("# Bonding設定")
foreach($key in $s.bonding.keys){
    $bond = $s.bonding.$key
    if($bond.ipaddress){
        write-host("bondInterface=[{0}],ipaddress=[{1}],netmask=[{2}]" -f ($bond.interfaceList -join ","),$bond.ipaddress,$bond.netmask)
    }
}

# ルーティング設定
write-host("# Routhing設定")
if($routing){
        write-host("[{0}]" -f $routing)
}

 

"c:\Program Files (x86)\WinSCP\WinSCP.exe" /console /script="e:\tmp\test.upfile"

スクリプトファイル
===================================
option batch on
open <アカウント>:<パスワード>@<ホスト>
cd <ホストのディレクトリ>
option transfer binary
lcd <ローカルのディレクトリ>
put <アップロードするファイル名>  
get <ダウンロードするファイル名>
exit
===================================

ex)アップロード
===================================
option batch on
open matsumoto:password@192.168.46.151
cd /home/matsumoto
option transfer binary
lcd "e:\tmp"
put test.txt
exit
===================================

ex)ダウンロード
===================================
option batch on
open matsumoto:password@192.168.46.151
cd /home/matsumoto
option transfer binary
lcd "e:\tmp"
get test_download.txt
exit
===================================