LoginSignup
0
0

仕事用PowerShellコマンド

Last updated at Posted at 2023-05-09

※※ 私用メモ

フォルダー自動作成

for($i=1; $i -le 20; $i++) {
$N = "{0:000}" -f $i
$N2 = "ZZ" + $N
mkdir $N2 
}

フォルダー自動削除

Remove-Item -Path ./* -Recurse -Include *Thumbs.db*
Get-ChildItem -Recurse -Name -Include *要件定義*

コード内テキスト参照(横並びチェックに利用)

$infolder="C:\_ZaimApp\ローカルリポジトリ\tkz_web_source\jz\trunk\jz_server\webapps\JZWeb\WEB-INF\src_gyomu"
$keyword="JZC090R"
$outputfile=New-Object System.IO.StreamWriter("C:\log.txt",$true,[Text.Encoding]::GetEncoding("UTF-8"))
# 検索対象ディレクトリー存在チェック
if (Test-Path $infolder) {
    # 検索対象フォルダ内のファイル一覧を取得
    $infilelist=Get-ChildItem -Path $infolder -Recurse
    # 1ファイルずつ処理
    foreach ($infilename in $infilelist) {
        # ファイル存在チェック
        if (Test-Path $infilename.FullName) {
           # ファイルの中身取得
           $line=Get-Content $infilename.FullName
           # 1行ずつ処理
           foreach ($getline in $line) {
               # キーワードがある行かどうかの判定
               if ($getline -match $keyword ) {
                   # 出力ファイルに行を書きだす
                   $outputfile.WriteLine("$($infilename.Name): $($getline)")
               }
            }
        }
    }
}
$outputfile.Close()

カレントディレクトリーの全てのアイテムの特定の名前を置換する。

Get-ChildItem | Rename-Item -NewName {
	$_ -Replace ".BK",""}

リモートデスクトップPORT
通常 3389
ActiveDirectory 56379

リモート接続元のIPを確認する。

function getRemoteIpAddr(){
    $result = &"netstat" -n -p tcp
    foreach ($line in $result) {
        if ( ${line} -match "  TCP +\d+\.\d+\.\d+\.\d+:3389 +(\d+\.\d+\.\d+\.\d+):.+ESTABLISHED") {
            return $Matches[1]
        }
    }
    return ""
}
$ret = getRemoteIpAddr

ファイルカウント集計

$infolder="C:\Users\zaimu010\Documents\006.マクロ作成\CSV\00_ALL"
$outputfile=New-Object System.IO.StreamWriter("C:\Users\zaimu010\Documents\006.マクロ作成\FTRAN_Cnt_ALL.csv",$true,[Text.Encoding]::GetEncoding("UTF-8"))
if (Test-Path $infolder) {
    $infilelist=Get-ChildItem -Path $infolder -Recurse
    Write-Host $infilelist
    foreach ($infilename in $infilelist) {
        if (Test-Path $infilename.FullName) {
           $fname=$infilename.Name
           $rfname=$fname
           Write-Host $rfname
           $rcount=(Get-Content $infilename.FullName).count
           Write-Host $rcount
           $num=$fname.Length - ($fname -replace "_","").Length
           Write-Host $num
           if ($num -ge 2 ) {
               Write-Host $rfname
               $strindex=($rfname.LastIndexOf("_"))
               Write-Host $strindex
               $rfname=($fname.Substring(0,$strindex)) 
           }
           Write-Host $rfname
           $writes="01, ,$fname,$rfname,$rcount"
           
           $outputfile.WriteLine("$writes")
        }
    }
}
$outputfile.Close()
0
0
0

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
0
0