| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

「Powershell」の版間の差分

提供: MyMemoWiki
ナビゲーションに移動 検索に移動
(ページの作成:「==Powershell== ==コマンドレット== *http://technet.microsoft.com/ja-jp/library/dn169200.aspx #get-command #get-help get-command #get-command | select-string…」)
 
256行目: 256行目:
 
*文字列値の存在確認
 
*文字列値の存在確認
 
*エイリアス sls
 
*エイリアス sls
<blockquote>grepの代替として</blockquote>
+
&lt;blockquote&gt;grepの代替として&lt;/blockquote&gt;
  
 
*grep で使えるようにしておく
 
*grep で使えるようにしておく
269行目: 269行目:
 
====Format-Wide ====
 
====Format-Wide ====
 
*オブジェクトの既定のプロパティだけが表示されます
 
*オブジェクトの既定のプロパティだけが表示されます
  PS> Get-Process -Name powershell | Format-Wide
+
  PS&gt; Get-Process -Name powershell | Format-Wide
 
====Format-List ====
 
====Format-List ====
 
*リスト形式で表示されます。各プロパティがその名前と共に別々の行に表示されます
 
*リスト形式で表示されます。各プロパティがその名前と共に別々の行に表示されます
  PS> Get-Process -Name powershell | Format-List
+
  PS&gt; Get-Process -Name powershell | Format-List
 
====Format-Table ====
 
====Format-Table ====
  PS> Get-Process -Name powershell | Format-Table -Property Path,Name,Id,Company
+
  PS&gt; Get-Process -Name powershell | Format-Table -Property Path,Name,Id,Company
 
==コマンドサンプル==
 
==コマンドサンプル==
 
*[http://qiita.com/cd01/items/da9a36582372e7d0a7f6 「 Linux ならできるのに、だから Windows は...」「それ PowerShell でできるよ」]
 
*[http://qiita.com/cd01/items/da9a36582372e7d0a7f6 「 Linux ならできるのに、だから Windows は...」「それ PowerShell でできるよ」]
283行目: 283行目:
 
====フォルダ配下のファイル名の検索====
 
====フォルダ配下のファイル名の検索====
 
*[https://technet.microsoft.com/ja-jp/library/ee177028.aspx Where-Object]  
 
*[https://technet.microsoft.com/ja-jp/library/ee177028.aspx Where-Object]  
  > get-childitem | where-object {$_.name -like "Python*"}
+
  &gt; get-childitem | where-object {$_.name -like "Python*"}
 
==Powershell ISE==
 
==Powershell ISE==
 
===Windows8===
 
===Windows8===
295行目: 295行目:
 
*http://www.atmarkit.co.jp/ait/articles/0709/20/news125.html
 
*http://www.atmarkit.co.jp/ait/articles/0709/20/news125.html
 
*PowerShellはカレント・フォルダを検索しない
 
*PowerShellはカレント・フォルダを検索しない
  PS > ./hoge.ps1
+
  PS &gt; ./hoge.ps1
 
====スクリプト実行ポリシーを設定====
 
====スクリプト実行ポリシーを設定====
 
*以下のエラーが発生する
 
*以下のエラーが発生する
<blockquote>このシステムではスクリプトの実行が無効になっているため...</blockquote>
+
&lt;blockquote&gt;このシステムではスクリプトの実行が無効になっているため...&lt;/blockquote&gt;
  PS > Set-ExecutionPolicy RemoteSigned
+
  PS &gt; Set-ExecutionPolicy RemoteSigned
 
{|class="wikitable"
 
{|class="wikitable"
 
!ポリシー
 
!ポリシー
321行目: 321行目:
 
===環境変数===
 
===環境変数===
 
====一覧表示====
 
====一覧表示====
  PS> get-childitem env:
+
  PS&gt; get-childitem env:
 
====指定表示====
 
====指定表示====
 
  $Env:環境変数名
 
  $Env:環境変数名
 
=====例=====
 
=====例=====
  PS> $env:path
+
  PS&gt; $env:path
 
====設定====
 
====設定====
  PS> set-item env:HOGE hoge
+
  PS&gt; set-item env:HOGE hoge
  
 
===文字列分割===
 
===文字列分割===
 
*環境変数を ";"で分割
 
*環境変数を ";"で分割
  PS> $Env:path -split ";"
+
  PS&gt; $Env:path -split ";"
 
===バックグラウンドジョブの実行と確認===
 
===バックグラウンドジョブの実行と確認===
 
*Start-Job、Get-Job
 
*Start-Job、Get-Job
 
  Start-Job -ScriptBlock{ 実行するコマンド }
 
  Start-Job -ScriptBlock{ 実行するコマンド }
 
=====例=====
 
=====例=====
  PS C:\Programs\wildfly-8.0.0.Final\bin> start-job -ScriptBlock {.\standalone.bat}
+
  PS C:\Programs\wildfly-8.0.0.Final\bin&gt; start-job -ScriptBlock {.\standalone.bat}
 
   
 
   
 
  Id    Name            PSJobTypeName  State        HasMoreData    Location            Command
 
  Id    Name            PSJobTypeName  State        HasMoreData    Location            Command
343行目: 343行目:
 
   
 
   
 
   
 
   
  PS C:\Programs\wildfly-8.0.0.Final\bin> get-job
+
  PS C:\Programs\wildfly-8.0.0.Final\bin&gt; get-job
 
   
 
   
 
  Id    Name            PSJobTypeName  State        HasMoreData    Location            Command
 
  Id    Name            PSJobTypeName  State        HasMoreData    Location            Command
350行目: 350行目:
  
 
===カレントディレクトリをエクスプローラーで開く===
 
===カレントディレクトリをエクスプローラーで開く===
  > explorer (get-location).path
+
  &gt; explorer (get-location).path
  
 
===コマンド情報の確認(whereis)===
 
===コマンド情報の確認(whereis)===
  PS C:\workspaces\vscode\reactlesson> get-command git
+
  PS C:\workspaces\vscode\reactlesson&gt; get-command git
 
   
 
   
 
  CommandType    Name                                              Version    Source
 
  CommandType    Name                                              Version    Source
 
  -----------    ----                                              -------    ------
 
  -----------    ----                                              -------    ------
 
  Application    git.exe                                            2.12.2.2  C:\Program Files\Git\cmd\git.exe
 
  Application    git.exe                                            2.12.2.2  C:\Program Files\Git\cmd\git.exe

2020年2月15日 (土) 08:05時点における版

Powershell

コマンドレット

  1. get-command
  2. get-help get-command
  3. get-command | select-string "select"
コマンドレット 内容
Add-History
Clear-Content
Compare-Object
Convert-Path
ConvertTo-Html
Copy-Item
Export-Alias
Export-Clixml
Export-Csv
Foreach-Object
Format-List
Format-Wide
Get-Acl
Get-Alias
Get-AuthenticodeSignature
Get-ChildItem
Get-Command
Get-Content
Get-Culture
Get-Date
Get-Eventlog
Get-ExecutionPolicy
Get-Help
Get-History
Get-Host
Get-Item
Get-ItemProperty
Get-Location
Get-Member
Get-Process
Get-PSDrive
Get-PSProvider
Get-Service
Get-Unique
Get-WMiObject
Group-Object
Import-Alias
Import-Clixml
Import-Csv
Invoke-Expression
Invoke-History
Invoke-Item
Measure-Command
Measure-Object
Move-Item
New-Alias
New-Item
New-PSDrive
New-Timespan
Out-File
Out-Host
Output-Printer
Read-Host
Remove-Item
Remove-PSDrive
Rename-Item
Restart-Service
Resume-Service
Select-Object
Select-String
Set-Alias
Set-Content
Set-Date
Set-ExecutionPolicy
Set-Location
Set-Service
Sort-Object
Start-Service
Start-Sleep
Stop-Process
Stop-Service
Suspend-Service
Tee-Object
Test-Path
Where-Object 返されるデータのフィルタ処理
Write-Host
Write-Warning

Get-Command

  • すべての Windows PowerShell コマンドレットの一覧を返す

Get-Help

  • PowerShell には、コマンド ヘルプと概念に関するヘルプの 2 種類のヘルプがある

使用できるすべてのヘルプ トピック一覧を取得

Get-Help *

トピック名を Get-Help に渡すことで特定のトピックに関するヘルプを取得できる

Get-Help Get-Member | more

Select-String

  • 文字列値の存在確認
  • エイリアス sls

<blockquote>grepの代替として</blockquote>

  • grep で使えるようにしておく
Set-Alias grep Select-String

Get-Alias

  • Windows PowerShell エイリアスのリストを返す
  • エイリアス gal

書式

  • 特定のオブジェクトについて、表示するプロパティを制御するための一連のコマンドレットが用意されています
  • これらすべてのコマンドレットの名前は、Format という動詞で始まります
  • Format 系のコマンドレットには、Format-Wide、Format-List、Format-Table、および Format-Custom があります

Format-Wide

  • オブジェクトの既定のプロパティだけが表示されます
PS> Get-Process -Name powershell | Format-Wide

Format-List

  • リスト形式で表示されます。各プロパティがその名前と共に別々の行に表示されます
PS> Get-Process -Name powershell | Format-List

Format-Table

PS> Get-Process -Name powershell | Format-Table -Property Path,Name,Id,Company

コマンドサンプル

フォルダ配下のファイル名の検索

> get-childitem | where-object {$_.name -like "Python*"}

Powershell ISE

Windows8

0971 powershell ise.jpg

Tips

スクリプトファイルを実行する

PS > ./hoge.ps1

スクリプト実行ポリシーを設定

  • 以下のエラーが発生する

<blockquote>このシステムではスクリプトの実行が無効になっているため...</blockquote>

PS > Set-ExecutionPolicy RemoteSigned
ポリシー 内容
Restricted すべてのスクリプトを実行不可
AllSigned すべてのスクリプトに証明書を要求
RemoteSigned インターネット経由でダウンロードしたスクリプトのみ証明書を要求
Unrestricted すべてのスクリプト実行を許可(ただしインターネット経由でダウンロードしたコードは実行確認のみあり)


環境変数

一覧表示

PS> get-childitem env:

指定表示

$Env:環境変数名
PS> $env:path

設定

PS> set-item env:HOGE hoge

文字列分割

  • 環境変数を ";"で分割
PS>  $Env:path -split ";"

バックグラウンドジョブの実行と確認

  • Start-Job、Get-Job
Start-Job -ScriptBlock{ 実行するコマンド }
PS C:\Programs\wildfly-8.0.0.Final\bin> start-job -ScriptBlock {.\standalone.bat}

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
2      Job2            BackgroundJob   Running       True            localhost            .\standalone.bat


PS C:\Programs\wildfly-8.0.0.Final\bin> get-job

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
2      Job2            BackgroundJob   Failed        False           localhost            .\standalone.bat

カレントディレクトリをエクスプローラーで開く

> explorer (get-location).path

コマンド情報の確認(whereis)

PS C:\workspaces\vscode\reactlesson> get-command git

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     git.exe                                            2.12.2.2   C:\Program Files\Git\cmd\git.exe