タグ: Powershell

  • Windows10 のPowershellの最大メモリサイズの変更

    Windows 10(およびWindows Server 2012, 2012 R2, 2016, 2019も同じ)でのPowershellの実行時の最大メモリ数の変更手順。Powershellを実行したときに、メモリを使いすぎたので、制限する。

    変更手順

    1. Powershellを管理者モードで起動する
    2. Powershellのバージョン確認する
      $PSversionTable
      ※Powershellが3.0以降の場合は、追加手順あり。
    3. Powershellのメモリ割り当てサイズの確認
      Get-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB
    4. 設定変更
      Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 2048
    5. プラグインの確認(Ver3.0以降の場合は実行)
      ls WSMan:localhost\Plugin 
    6. プラグインのPowershellの割り当てサイズの確認(Ver3.0以降の場合は実行)
      Get-Item WSMan:localhost\Plugin\microsoft.powershell\Quotas\MaxConcurrentCommandsPerShell 
    7. プラグインのPowershellの割り当てサイズの変更(Ver3.0以降の場合は実行)
      Set-Item WSMan:localhost\Plugin\microsoft.powershell\Quotas\MaxConcurrentCommandsPerShell 2048
    8. 設定変更後WinRMの再起動
      Restart-Service WinRM
    9. 事後確認
      Get-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB
    10. 32bitのPowershellを管理者モードで起動する
    11. Powershellのメモリ割り当てサイズの確認
      Get-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 
      ※もし、32bitのPowershellのメモリが変わっていない場合は、同じように変更する。

    作業ログ。

    PS C:\WINDOWS\system32> Get-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB
    
    
       WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Shell
    
    Type            Name                           SourceOfValue   Value
    ----            ----                           -------------   -----
    System.String   MaxMemoryPerShellMB                            2048
    
    
    PS C:\WINDOWS\system32> Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 2048
    警告: 更新された構成は、プラグインあたりのクォータの値が 2048
    を超えるプラグインの操作に影響する可能性があります。登録されているすべてのプラグインの構成を確認し、影響を受けるプラグ
    インのプラグインあたりのクォータの値を変更してください。
    PS C:\WINDOWS\system32>
    PS C:\WINDOWS\system32>
    PS C:\WINDOWS\system32>
    PS C:\WINDOWS\system32>
    PS C:\WINDOWS\system32>
    PS C:\WINDOWS\system32> ls WSMan:localhost\Plugin
    
    
       WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Plugin
    
    Type            Keys                                Name
    ----            ----                                ----
    Container       {Name=Event Forwarding Plugin}      Event Forwarding Plugin
    Container       {Name=microsoft.powershell}         microsoft.powershell
    Container       {Name=microsoft.powershell.workf... microsoft.powershell.workflow
    Container       {Name=microsoft.powershell32}       microsoft.powershell32
    Container       {Name=WMI Provider}                 WMI Provider
    
    
    PS C:\WINDOWS\system32> Get-Item WSMan:localhost\Plugin\microsoft.powershell\Quotas\MaxConcurrentCommandsPerShell
    
    
       WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Plugin\microsoft.powershell\Quotas
    
    Type            Name                           SourceOfValue   Value
    ----            ----                           -------------   -----
    System.String   MaxConcurrentCommandsPerShell                  2147483647
    
    
    PS C:\WINDOWS\system32> Set-Item WSMan:localhost\Plugin\microsoft.powershell\Quotas\MaxConcurrentCommandsPerShell 2048
    警告: 構成の変更は、WinRM サービスを再起動しないと有効になりません。WinRM
    サービスを再起動するには次のコマンドを実行します: 'Restart-Service winrm'
    PS C:\WINDOWS\system32>
    PS C:\WINDOWS\system32>
    PS C:\WINDOWS\system32> Restart-Service winrm
    PS C:\WINDOWS\system32>
    

    参考
    https://tech.guitarrapc.com/entry/2013/08/02/000842

  • Powershell起動時に「~PowerShell をお試しください」が表示される

    PowerShellを起動させたところ、下記のメッセージが表示された。

    Windows PowerShell
    Copyright (C) Microsoft Corporation. All rights reserved.
    
    新しいクロスプラットフォームの PowerShell をお試しください https://aka.ms/pscore6
    
    PS C:\Users\zen>
    

    この表示されたURLにアクセスすると、Powershell6のページになる。

    つまり、Powershell6(正確には、Powershell Core 6)のインストールを促すメッセージ。既存環境のPowershellのバージョンを確認すると、Powershell 5.1なので、6をお勧めしてきているようだ。

    PS C:\Users\zen> $PSVersionTable
    
    Name Value
    ---- -----
    PSVersion 5.1.18362.145
    PSEdition Desktop
    PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
    BuildVersion 10.0.18362.145
    CLRVersion 4.0.30319.42000
    WSManStackVersion 3.0
    PSRemotingProtocolVersion 2.3
    SerializationVersion 1.1.0.1
    
    PS C:\Users\zen>
    

  • PowerCLIのバージョンを調べる

    PowerCLIは、WindowsのPowershellを使って、ESXiなどのvSphereを管理するツール。これのバージョン情報は、「Get-PowerCLIVersion」で表示できる。

    PowerCLI C:\> Get-PowerCLIVersion
    
    PowerCLI Version
    ----------------
       VMware PowerCLI 6.5 Release 1 build 4624819
    ---------------
    Component Versions
    ---------------
       VMware Cis Core PowerCLI Component 6.5 build 4624453
       VMware VimAutomation Core PowerCLI Component 6.5 build 4624450
       VMWare ImageBuilder PowerCLI Component 6.5 build 4561891
       VMWare AutoDeploy PowerCLI Component 6.5 build 4561891
       VMware Vds PowerCLI Component 6.5 build 4624695
       VMware Cloud PowerCLI Component 6.5 build 4624821
       VMware HA PowerCLI Component 6.0 build 4525225
       VMware HorizonView PowerCLI Component 7.0.2 build 4596620
       VMware Licensing PowerCLI Component 6.5 build 4624822
       VMware PCloud PowerCLI Component 6.5 build 4624825
       VMware Storage PowerCLI Component 6.5 build 4624820
       VMware vROps PowerCLI Component 6.5 build 4624824
       VMware vSphere Update Manager PowerCLI 6.5 build 4540462
    
  • PowershellでPowerCLIのモジュールを読み込む

    PowerCLIのコマンドレットを使ったPowershellのスクリプトを書こうとしたところ、古いPowerCLIのコマンドではエラーになった。 PowerCLI 6.5では、Import-Moduleで読み込むようになっていた。 (5系は環境がないので、不明)

    PowerCLI 6.5 以降のとき

    # PowerCLIのコマンドレットをインポート
    Import-Module VMware.VimAutomation.Core
    

    PowerCLI 4.1 のとき

    # PowerCLIのコマンドレットをインポート
    Add-PSSnapin VMware.VimAutomation.Core
    

  • PowerShell」の次期バージョンは「PowerShell 7」

    米Microsoftは4月5日(現地時間)、スクリプティング環境「PowerShell」のロードマップを発表した。次期バージョンは「PowerShell Core 6.3」ではなく、「PowerShell 7」になるという。プレビューリリースは今年5月、正式リリースは「.NET Core 3.0」の正式公開(年内を予定)後になるという。

    https://forest.watch.impress.co.jp/docs/news/1178786.html

    次のPowershellは、PowerShell 7なのね。Windows10だと5.1から7になると。(6は、PowerShell Coreで別途インストールが必要だから、すっ飛ばされると) コマンドレットのオプションが増えるだろうから、また古いOSで使えないものが増えるんだろうな。

    そういえば、手元にある「Windows Powershell ポケットリファレンス」は、3.0までの対応。PowerShell Coreのコマンドレットもないので、PowerShell7のときは改訂版がでないだろうか。

  • Powershellの同時実行数を無制限にする

    まずは、現在のPowershellの同時実行数を調べる

    Get-Item WSMan:\localhost\Shell\MaxShellsPerUser
    

    Get-Itemコマンドレットを使い、Powershellの実行数を調べる。
    WinRMを使用するので、管理者モードで実行する。
    もし、WinRMサービスが起動していない場合には、起動するかを問われるのでYesを答える。

    PS C:\Windows\system32> Get-Item WSMan:\localhost\Shell\MaxShellsPerUser
    
    
       WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Shell
    
    Name                      Value                                         Type
    ----                      -----                                         ----
    MaxShellsPerUser          5                                             System.String
    
    
    PS C:\Windows\system32>
    

    つぎにPowershellの同時実行数を無制限にする

    Set-Item WSMan:\localhost\Shell\MaxShellsPerUser 0
    

    Set-Itemコマンドレットを使用し、MaxShellsPerUserを0で指定する。
    0の場合は、無制限になる。
    10や20を指定すれば、同時実行数はその数になる。

  • Powershellで5秒間隔でPING疎通確認する

    Powershellで5行置きに、ICMPで疎通確認をし続けるスクリプト。 localhostのところを疎通確認したいホスト名に変える、結果をhoge.txtに書き込むので、任意のファイル名に変える。

    ランライナー。

    while ($true -eq $true) {get-date >> hoge.txt; test-Connection -computername "localhost" -count 1  -Quiet  >> hoge.txt; sleep 5 }
    
  • VMwareのPowerCLIのバージョンを調べる方法

    VMware PowerCLIのバージョンを調べるには、
    PowerCLIを起動させて、以下のコマンドを実行する。

    Get-PowerCLIVersion
    
  • Powershellをコマンドレットの途中で改行する

    Powershellでコマンドレットを使っていると、引数やオプションなどが増えて1行が長くなる。 そうするとコマンドが書きにくく、可読性も下がるので、途中で改行を行えると便利だ。

    Powershellのコマンドレット中の改行は「`」で行える。 「`」がモニタ上のゴミのように見えるのがいけていない。

    改行になると、「>> 」が行頭に付く。 コマンドレットの終わりは、空行でENTERすると実行される。

    例)

    PS C:\> Get-HotFix  -id kb3107998 , `
    >> KB3101746, `
    >> KB3102810
    >>
    
    Source        Description      HotFixID      InstalledBy          InstalledOn
    ------        -----------      --------      -----------          -----------
    PCname        Security Update  KB3101746     NT AUTHORITY\SYSTEM  2015/11/11 0:00:00
    PCname        Update           KB3102810     NT AUTHORITY\SYSTEM  2015/11/11 0:00:00
    PCname        Update           KB3107998     NT AUTHORITY\SYSTEM  2015/11/11 0:00:00
    
  • Powershellからメールを送信する方法

    Windows 7 や Windows Server 2008 R2などでは、初期状態ではTelnetクライアントが機能としてインストールされていないため、Telnetを使ったメール送信テストが使用できない。

    Windows 7 や Windows Server 2008 R2 などでは、標準でインストールされている Powershellにメール送信コマンドがあり、それを使うことで代用できる。 難点としては、エラー時のコードがわかりにくいこと、ポート番号を変えられないこと。

    Powershellでメールを送信するには、Powershellを立ち上げて、 [[Send-MailMessage]] コマンド(コマンドレット)を実行する

     
    Send-MailMessage 
     -To 宛先のメールアドレス 
     -From 送信元のメールアドレス 
     -SmtpServer SMTPサーバ(FQDNやIPアドレス)
     -Subject "件名をダブルクォーテーションの中に書く"
     -Body "本文をダブルクォーテーションの中に書く"
    

    認証の無しでよい場合は、以下のようになる

     Send-MailMessage -To hogehoge@xenos.jp -From hoge@xenos.jp -SmtpServer smtpserver.xenos.jp -Subject "TEST MAIL from Powershell" -Body "This is test mail."
    

    また認証とSSLでのアクセスが必要な場合は下記のオプションを付ける。

     -Credential ユーザ名
     -UseSsl
    

    Telnetを使ってメールを送るのに比べたら、Powershellを使ってメールを送る方が簡単かも。 これでSMTPサーバに接続用の穴をあけたとか、FWに穴をあけたとか、 そういうことの確認が簡単にできる。

    ■参考