コンテンツへスキップ

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)

 

# 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)

 

"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
===================================