●バージョン表示
PS C:\Users\me> pwsh --version
PowerShell 7.1.0
PS C:\Users\me> $PSVersionTable
Name Value
---- -----
PSVersion 7.1.0
PSEdition Core
GitCommitId 7.1.0
OS Microsoft Windows 10.0.18363
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
●コンソール操作
Ctrl+→、Ctrl+←:単語単位の移動
Ctrl+End:カーソル位置より後の文字を削除
Ctrl+Home:カーソル位置より前の文字を削除
Ctrl+Space:入力候補一覧の表示
Shift+Tab:入力補完を戻る
●変数
PS C:\Users\me> $ば=$PSVersionTable
PS C:\Users\me> $ば
Name Value
---- -----
PSVersion 7.1.0
PSEdition Core
GitCommitId 7.1.0
OS Microsoft Windows 10.0.18363
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
●サンプル
1..10 | ForEach-Object{"" > item$_.txt}
Get-ChildItem -Path item*.txt | ForEach-Object { Rename-Item -Path $_ -NewName $_.name.replace("item","file") }
Get-ChildItem file*.txt | Rename-Item -NewName { $_.name.Replace("file","item")}
Get-ChildItem | Select-Object *
Get-ChildItem | Select-Object * -First 1
Get-ChildItem | Get-Member
Get-ChildItem -path item*|Select-Object name,DirectoryName,Length|Export-Csv -Path output.csv
PS D:\Temp> Import-Csv .\output.csv|Measure-Object -sum -Property length
Count : 11
Average :
Sum : 22
Maximum :
Minimum :
StandardDeviation :
Property : Length
PS D:\Temp> (Invoke-WebRequest -Uri https://google.com).statuscode
200
PS D:\Temp> Invoke-WebRequest -Uri https://google.com
StatusCode : 200
StatusDescription : OK
Content : <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="ja"><head><met
a content="世界中のあらゆる情報を検索するためのツールを提供しています。さまざまな検索機能を
活用して、お探しの情報を見つけてください。" name="description"><meta content="n…
RawContent : HTTP/1.1 200 OK
Date: Tue, 17 Nov 2020 15:42:46 GMT
Cache-Control: max-age=0, private
P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
Server: gws
X-XSS-Protection: 0
X-Frame-…
Headers : {[Date, System.String[]], [Cache-Control, System.String[]], [P3P, System.String[]], [Server
, System.String[]]…}
Images : {@{outerHTML=<img alt="Google" height="92" src="/images/branding/googlelogo/1x/googlelogo_w
hite_background_color_272x92dp.png" style="padding:28px 0 14px" width="272" id="hplogo">; t
agName=IMG; alt=Google; height=92; src=/images/branding/googlelogo/1x/googlelogo_white_back
ground_color_272x92dp.png; style=padding:28px 0 14px; width=272; id=hplogo}}
InputFields : {}
Links : {@{outerHTML=<a class="gbzt gbz0l gbp1" id=gb_1 href="https://www.google.co.jp/webhp?tab=ww
"><span class=gbtb2></span><span class=gbts>検索</span></a>; tagName=A; class=gbzt gbz0l gb
p1; id=gb_1; href=https://www.google.co.jp/webhp?tab=ww}, @{outerHTML=<a class=gbzt id=gb_2
href="https://www.google.co.jp/imghp?hl=ja&tab=wi"><span class=gbtb2></span><span class=gb
ts>画像</span></a>; tagName=A; class=gbzt; id=gb_2; href=https://www.google.co.jp/imghp?hl=
ja&tab=wi}, @{outerHTML=<a class=gbzt id=gb_8 href="https://maps.google.co.jp/maps?hl=ja&ta
b=wl"><span class=gbtb2></span><span class=gbts>マップ</span></a>; tagName=A; class=gbzt; i
d=gb_8; href=https://maps.google.co.jp/maps?hl=ja&tab=wl}, @{outerHTML=<a class=gbzt id=gb_
78 href="https://play.google.com/?hl=ja&tab=w8"><span class=gbtb2></span><span class=gbts>P
lay</span></a>; tagName=A; class=gbzt; id=gb_78; href=https://play.google.com/?hl=ja&tab=w8
}…}
RawContentLength : 48025
RelationLink : {}
●サンプル
$url = "https://sites.google.com/site/495a411/"
$validStatusCode = 404
$outfile = "D:/temp/StatusCheck.log"
while ($true) {
# ステータスコードを取得する
$statusCode = try {
(Invoke-WebRequest -uri $url {
} until (condition)).statusCode
}
catch {
$Error[0].Exception.GetBaseException().Response.StatusCode.Value__
}
# ステータスコードが200かどうか確認する
if ($statuscode -ne $validStatusCode) {
#StatusCheck.log存在確認
if (-not(test-path -path $outfile)) {
#StatusCheck.logファイルを作る
new-item -Path $outfile
}
$json = [ordered]@{
Date = (Get-Date).ToString("F")
Url = $url
StatusCode = $statusCode
} | ConvertTo-Json -Compress
# $json | out-file -literalpath $outfile -Append -encoding ([System.Text.encoding]::GetEncoding(932))
$json | out-file -literalpath $outfile -Append -encoding Default
}
# 60秒停止する
Start-Sleep -Seconds 60
}