rainit2006 / My_Windows

0 stars 0 forks source link

PowerShell/Bat #19

Open rainit2006 opened 6 years ago

rainit2006 commented 6 years ago

在PowerShell ISE里执行时,报错:

ファイル C:\Users\tetsuo\hello.ps1 を読み込めません。ファイル C:\Users\tetsuo\hello.ps1 はデジタル署名されていません。 このスクリプトはシステムで実行されません。

解决方法: 需要以管理员权限启动,执行“Set-ExecutionPolicy RemoteSigned”。 如果不是管理员权限的话,则报错:

Set-ExecutionPolicy : レジストリ キー 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell ' へのアクセスが拒否されました。

rainit2006 commented 6 years ago

メッセージボックスで変数2つの内容を表示

Add-Type -Assembly System.Windows.Forms
[System.Windows.Forms.MessageBox]::Show("行数は $ROW です。`n列数は $COL です。", "結果")
rainit2006 commented 6 years ago

Excel操作

} While($first -ne $null -and $first.Row -ne $find.Row) $count


- Merge cells

$MergeCells = $objWorksheet.Range("B1:C1")

$MergeCells.Select() $MergeCells.MergeCells = $true

   ...find処理..
 $target_row = $find.Row $target_col =$find.Column $target_row.toString() + "," + $target_col.toString()

 $ma = $find.MergeArea $ma.Value()  //输出结合cell的值 $ma.Address() //输出结合cell的地址,比如:$B$31:$B$33 $ma.Rows.count //结合cell里包含的row的数量 $ma.Rows.item(2).Row //结合cell里第2行元素的对应的Row

for($i=1; $i -lt $row_count+1; $i++){        
    #$ma.Rows.item($i).font.color = 255
    $row =$ma.Rows.item($i).Row
    $sheet_sr.Rows.item($row).font.color = 255
}

if($sheet.Cells.item($target_row,$target_col).MergeCells)
{
   $target_row.toString() + "," + $target_col.toString()+" is a mergeCell"
}


- Insert
下面变量似乎没有用
$xlShiftDown = -4121
xlShiftToRight = -4161
rainit2006 commented 6 years ago

Batファイル処理

查看command的exit code Windows OS下是用%errorlevel%

执行命令
echo %errorlevel%

batファイルの引数 %1, %2。。。

if "%1" == "" goto ERROR
if "%2" == "" goto ERROR

ファイル出力 echo Hello >> memo.txt

@echo off : 表示OFF

変数の設定方法 set a = 0

rainit2006 commented 6 years ago

一括でファイルコード変換

  # スクリプトファイルのパラメータを宣言(先頭から「変換対象のフォルダ」
    # 「変換後のファイルの保存先」「変換前の文字コード」「変換後の文字コード」)
param(
  [String]$in = "C:\Source",
  [String]$out = "C:\Source-output",
  [String]$from = "SJIS",
  [String]$to = "utf-8"
)

    # 引数$from、$toから、文字コードを表すEncodingオブジェクトを生成
$enc_f = [Text.Encoding]::GetEncoding($from)
$enc_t = [Text.Encoding]::GetEncoding($to)
    # 与えられたパス(c:\tmp\convert)から合致するファイルリストを再帰的に取得
  Get-ChildItem $in -recurse |
    # 取得したファイルを順番に処理
    ForEach-Object {
    # 取得したオブジェクトがファイルの場合のみ処理(フォルダの場合はスキップ)
      if($_.GetType().Name -eq "FileInfo"){
    # 変換元ファイルをStreamReaderオブジェクトで読み込み
        $reader = New-Object IO.StreamReader($_.FullName, $enc_f)
    # 保存先のパス、保存先の親フォルダのパスを生成
        $o_path = $_.FullName.ToLower().Replace($in.ToLower(), $out)
        $o_folder = Split-Path $o_path -parent
    # 保存先のフォルダが存在しない場合にフォルダを自動生成
        if(!(Test-Path $o_folder)){
          [Void][IO.Directory]::CreateDirectory($o_folder)
        }
    # 保存先ファイルをStreamWriterオブジェクトでオープン
        $writer = New-Object IO.StreamWriter($o_path, $false, $enc_t)
        $o_path
    # 変換元ファイルを順に読み込み、保存先ファイルに書き込み
        while(!$reader.EndOfStream){$writer.WriteLine($reader.ReadLine())}
    # ファイルをすべてクローズ
        $reader.Close()
        $writer.Close()
      }
    }
rainit2006 commented 6 years ago

■Passing variables from 1 PowerShell script to another PowerShell script https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-6

●Create 1st .ps1 file 1 Create 2 variables 2 Assign each variable with a value 3 Use “powershell.exe” command with the “-file” parameter to execute the 2nd .ps1 file and include both variable names 4 Save the script

●Create 2nd .ps1 file 1 Use “$args” variable to accept the variable from 1st .ps1 file  Note: “$args” is an array therefore you will be required to use an integer value to access the passing value (Example: $args[0]) 2 Output the variable to confirm that the variable was pass to the 2nd script correctly 3 Save the script using the same location as the 1st .ps1 file (to make it easy for our example)