ドラクエ10ブログの予定 -5ページ目

ドラクエ10ブログの予定

ドラクエ10ブログの予定


●返り値の型について
PS C:\Users\customer\Desktop\test> dir

    ディレクトリ: C:\Users\customer\Desktop\test


Mode                LastWriteTime         Length Name                                                                                                                                                                      
----                -------------         ------ ----                                                                                                                                                                      
-a----       2008/04/08      2:09            783 FileConvert.ps1                                                                                                                                                           
-a----       2017/01/14     21:07              6 SJIS.txt                                                                                                                                                                  
-a----       2017/01/14     21:07              8 UTF.txt    


# ヒット数が2個以上なので、Object[]が戻り値になる
PS C:\Users\customer\Desktop\test> (Get-ChildItem . -Exclude "*.ps1" -name).GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                                                        
-------- -------- ----                                     --------                                                                                                                                                        
True     True     Object[]                                 System.Array                                                                                                                                                    


# ヒット数が1個なので、配列が戻り値となっていない
PS C:\Users\customer\Desktop\test> (Get-ChildItem . -Exclude "*.txt" -name).GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                                                        
-------- -------- ----                                     --------                                                                                                                                                        
True     True     String                                   System.Object                                                                                                                                                   


# 強制的に配列で返したい時は、@を文(式?)の前につける
PS C:\Users\customer\Desktop\test> @(Get-ChildItem . -Exclude "*.txt" -name).GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                                                        
-------- -------- ----                                     --------                                                                                                                                                        
True     True     Object[]                                 System.Array  

                                                                                                                                                               


●文字エンコーディングを統一したい
$file_count = (Get-ChildItem . -Exclude "*.ps1" -name).count
$a = Get-ChildItem . -Exclude "*.ps1" -name
for ($i = 0; $i -lt $file_count; $i++){
    Get-Content $a[$i]
}
あ        # 「SJIS.txt」はShift-JISで保存しているので文字化けせず表示される

縺・    # 「UTF.txt」UTF-8で保存しているので文字化け

 

 

 

 

 

 

●子スコープで変数の値を変更しても親スコープには影響を与えない

# スクリプトブロックに入る = 子スコープに入ると思っておけば今のところOK

$a = "AAA"
$a
& {$a = "BBB"; $a} # BBBが表示され値が書き換わったにみえるが。。。
$a # AAAが表示される!

 

PS C:\Windows\system32> $a = "AAA"
$a
& {$a = "BBB"; $a}
$a

=>
AAA
BBB
AAA

 

●同じスコープレベルでの変更

PS C:\Windows\system32> $a = "AAA"
$a
& {$a = "BBB"; $a}
$a
$a = "CCC"
$a

=>
AAA
BBB
AAA
CCC # 当然、上書きされている

 

参考:http://mtgpowershell.blogspot.jp/2010/06/blog-post_21.html

 

●Invoke-Commandの戻り値について理解が怪しいのでメモ

# call.ps1

$Ret = Invoke-Command -ScriptBlock {  C:\Users\foo\Downloads\log\test.ps1 }
$Ret.Length
$LASTEXITCODE

=>

PS C:\Windows\system32> C:\Users\foo\Downloads\log\call.ps1
AAA  # 画面表示されるが、$Ret変数に格納されれているわけではない
BBB # 同上
2 # 配列の要素数
99

 

# 配列の要素

PS C:\Windows\system32> $Ret[0]

2016年12月18日 19:43:13

 

PS C:\Windows\system32> $Ret[1]
True

 

# called.ps1

Write-Host "AAA"
Write-Host "BBB"
get-date
$?
exit 99