コンテンツへスキップ

$e = New-Object -ComObject Excel.Application
$e.Visible = $true
$book  = $e.Workbooks.Add()
$sheet = $book.Worksheets.Item(1)
$sheet.Cells.Item(7,3) = "実線"

# ------< 実線 >----------
$sheet.cells.item(7.3).borders.LineStyle = 1 #実線
#$sheet.cells.item(7,3).borders.Weight = 2    # 線の太さ

# 列幅を自動調整する
$range = $sheet.usedRange
$range.EntireColumn.AutoFit() | out-null

# セルの結合
$c1 = $sheet.Cells(2,3)
$c2 = $sheet.Cells(2,4)
$sheet.range($c1,$c2).mergeCells = 1
$sheet.cells.item(2,3).borders.Weight = 2    # 線の太さ
$sheet.cells.item(2,4).borders.Weight = 2    # 線の太さ

# Excelを閉じる
$e.Quit()
# プロセスを解放する
$e = $null
[GC]::Collect()

 

他のブックのシートをコピーする。

$file1 = 'E:\my-document\エクセル\template.xlsx' # source's fullpath
$file2 = 'E:\my-document\エクセル\Test05.xlsx' # destination's fullpath
$xl = new-object -c excel.application
$xl.displayAlerts = $false # don't prompt the user
$xl.visible = $true
$workBook1 = $xl.workbooks.open($file1, $null, $true) # open source, readonly
#$workBook2 = $xl.workbooks.open($file2) # open target

# 新しいbookを作成
$workBook2 = $xl.Workbooks.Add() # index1に追加される

# コピーするシートをセット
$sheetToCopy = $workBook1.sheets.item('テンプレートシート') # source sheet to copy

#シート名アレイ
$sheetLists = ("サーバ1","サーバ2","サーバ3")

$i = 1
foreach($sheetName in $sheetLists) {
    $sh1_wb2 = $workBook2.sheets.item($i)
    $sheetToCopy.copy($sh1_wb2)
    $workBook2.WorkSheets.item("テンプレートシート").name = [string]$sheetName
    $i++
}

# ブック作成時にデフォルトで作成されるシートを削除
$workBook2.WorkSheets.item("Sheet1").delete()
$workBook2.WorkSheets.item("Sheet2").delete()
$workBook2.WorkSheets.item("Sheet3").delete()

$workBook1.close($false) # 保存なし
$workBook2.SaveAs($file2)
#$workBook2.close($true) # 保存あり
$xl.quit()
$xl = $null
[gc]::Collect()

 

testinfra サンプル

$ more test_service2.py
import pytest

@pytest.mark.parametrize("name,version", [
    ("httpd","2.2.5"),
    ("python", "2.9"),
])

def test_packages(host, name, version):
    pkg = host.package(name)
    assert pkg.is_installed
    assert pkg.version.startsith(version)

@pytest.mark.parametrize("name", [
    ("httpd"),
    ("chronyd"),
])
def test_service(host, name):
    service = host.service(name)
    assert service.is_running

実行結果

$ py.test -v ./test_service2.py
/usr/local/lib/python3.6/site-packages/testinfra-6.0.0-py3.6.egg/_testinfra_renamed.py:10: DeprecationWarning: testinfra package has been renamed to pytest-testinfra. Please `pip install pytest-testinfra` and `pip uninstall testinfra` and update your package requirements to avoid this message
============================================================================= test session starts =============================================================================
platform linux -- Python 3.6.8, pytest-6.2.4, py-1.10.0, pluggy-1.0.0.dev0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /home/user1
plugins: testinfra-6.0.0, testinfra-6.3.0
collected 4 items

test_service2.py::test_packages[local-httpd-2.2.5] PASSED                                                                                                               [ 25%]
test_service2.py::test_packages[local-python-2.9] PASSED                                                                                                                [ 50%]
test_service2.py::test_service[local-httpd] PASSED                                                                                                                      [ 75%]
test_service2.py::test_service[local-chronyd] PASSED                                                                                                                    [100%]

============================================================================== 4 passed in 0.68s ==============================================================================

 

$oldSystemPath = [System.Environment]::GetEnvironmentVariable("path","Machine")
$newSystemPath = $oldSystemPath + ";" + <追加したいパス>
[System.Environment]::SetenvironmentVariable("Path",$newSystemPath,"Machine")

パスの確認

[System.Environment]::GetEnvironmentVariable("path","Machine") -split ";"

# 1Mバイトのファイルを作成
@(1..10) | Foreach{
    $random_bin = new-object byte[] (1024*1024); (new-object Random).NextBytes($random_bin); [IO.File]::WriteAllBytes("c:\tmp\test$_.dat", $random_bin)
}
# 最終更新日が任意のファイルを作成
for($i=0;$i -le 90;$i++){
    $file = $i.tostring() + ".log"
    $filepath = (join-path D:\log $file)
    new-item $filepath |out-null
    $j = "-" + $i
    Set-ItemProperty $filepath -Name LastWriteTime -Value (get-date).addDays($j) 
}

 

$a=@(3,2,1,5,4,8)
$b=@(4,2,3,1,5,6)
Compare-Object ($a|sort-object) ($b|sort-object) -IncludeEqual | foreach-object {
    $direction = $_.SideIndicator
    $value = $_.InputObject
    if( $direction -eq "<="){
        write-host("aにだけあり[{0}]" -f $value)
    } elseif ( $direction -eq "=>"){
        write-host("bにだけあり[{0}]" -f $value)
    } else {
        write-host("aとbにあり[{0}]" -f $value)
    }
}

 

server.yml

hostname: server1

network: 
- eno1: 
  device: eno1  
  ipaddress: 192.168.1.1
  netmask: 255.255.255.0
  gateway: 192.168.1.254
- eno2:
  device: eno2 
  ipaddress: 192.168.1.2
  netmask: 255.255.255.0
  gateway: 192.168.1.254

 

読み込み

>>> with open('E:\\my-document\\Programing\\testinfra\\servers\\server1.yaml','r') as yml:
...     config = yaml.safe_load(yml)
... 
>>> config
{'hostname': 'server1', 'network': [{'eno1': None, 'device': 'eno1', 'ipaddress': '192.168.1.1', 'netmask': '255.255.255.0', 'gateway': '192.168.1.254'}, {'eno2': None, 'device': 'eno2', 'ipaddress': '192.168.1.2', 'netmask': '255.255.255.0', 'gateway': '192.168.1.254'}]}
>>> config['hostname']
'server1'
>>> config['network']
[{'eno1': None, 'device': 'eno1', 'ipaddress': '192.168.1.1', 'netmask': '255.255.255.0', 'gateway': '192.168.1.254'}, {'eno2': None, 'device': 'eno2', 'ipaddress': '192.168.1.2', 'netmask': '255.255.255.0', 'gateway': '192.168.1.254'}]
>>> config['network'][0]
{'eno1': None, 'device': 'eno1', 'ipaddress': '192.168.1.1', 'netmask': '255.255.255.0', 'gateway': '192.168.1.254'}
>>> config['network'][0]['ipaddress']
'192.168.1.1'
>>> config['network'][1]['ipaddress'] 
'192.168.1.2'
>>>

 

# download itextsharp.dll.
# Invoke-WebRequest -Uri 'https://github.com/itext/itextsharp/releases/download/5.5.10/itextsharp-all-5.5.10.zip'

param(
    [parameter(mandatory=$false)][string]$ITextLibraryPath = "E:\tmp\itextsharp.dll",
    [parameter(mandatory=$false)][string]$InputFilePath    = "E:\tmp\input.pdf",
    [parameter(mandatory=$false)][string]$OutputFilePath   = "E:\tmp\output.txt"
)

[System.Reflection.Assembly]::LoadFrom($ITextLibraryPath) | Out-Null
$reader = New-Object iTextSharp.text.pdf.PdfReader -ArgumentList $InputFilePath
$pages = $reader.numberofpages

for($i=1;$i -le $pages;$i++){
    write-output("=============== page=[{0}] ===============" -f $i) >>$OutputFilePath
    [iTextSharp.text.pdf.parser.PdfTextExtractor]::GetTextFromPage($reader,$i) >> $OutputFilePath
}