プロパティについて

  • 12/01/2017
  • 5分で読める
    • S s
    • q
  • Short description

    PowerShell でオブジェクト プロパティを使用する方法について説明します。

    Long description

    PowerShell は、データストア内のアイテムまたはコンピュータの状態を表すために、オブジェクトと呼ばれる構造化された情報のコレクションを使用します。 通常、Microsoft .NET Frameworkの一部であるオブジェクトを使用しますが、PowerShellでカスタムオブジェクトを作成することもできます。

    アイテムとそのオブジェクトの間の関連性は非常に密接です。 オブジェクトを変更すると、通常、それが表すアイテムも変更されます。 たとえば、PowerShell でファイルを取得するとき、実際のファイルを取得するわけではありません。 その代わり、ファイルを表すFileInfoオブジェクトを取得します。 FileInfoオブジェクトを変更すると、ファイルも変更されます。

    ほとんどのオブジェクトは、プロパティを持っています。 プロパティはオブジェクトに関連付けられたデータです。 オブジェクトの異なるタイプは、異なるプロパティを持ちます。 たとえば、ファイルを表すFileInfoオブジェクトは、ファイルが読み取り専用属性の場合は$True、そうでない場合は$Falseを含むIsReadOnlyプロパティを持っています。ファイルシステムのディレクトリを表すDirectoryInfoオブジェクトは、親ディレクトリへのパスを含むParentプロパティを持ちます。 たとえば、FileInfo オブジェクトのプロパティを取得するには、ファイルを表す FileInfo オブジェクトを取得する Get-ChildItem コマンドレットを使用します。 次に、パイプライン演算子 (|) を使用して、FileInfo オブジェクトを Get-Member に送信します。 次のコマンドは、PowerShell.exe ファイルを取得し、それを Get-Member に送信します。自動変数 $Pshome には、PowerShell のインストール ディレクトリのパスが含まれています。 PowerShellで作業する場合、オブジェクトのすべてのメンバーにアクセスすることができます。

    オブジェクトのプロパティのみを取得し、メソッドを取得しない場合は、次の例に示すように、Get-MemberコマンドレットのMemberTypeパラメーターに値「property」を指定して使用します。

Get-ChildItem $pshome\PowerShell.exe | Get-Member -MemberType property
TypeName: System.IO.FileInfoName MemberType Definition---- ---------- ----------Attributes Property System.IO.FileAttributes Attributes {get;set;}CreationTime Property System.DateTime CreationTime {get;set;}CreationTimeUtc Property System.DateTime CreationTimeUtc {get;set;}Directory Property System.IO.DirectoryInfo Directory {get;}DirectoryName Property System.String DirectoryName {get;}Exists Property System.Boolean Exists {get;}Extension Property System.String Extension {get;}FullName Property System.String FullName {get;}IsReadOnly Property System.Boolean IsReadOnly {get;set;}LastAccessTime Property System.DateTime LastAccessTime {get;set;}LastAccessTimeUtc Property System.DateTime LastAccessTimeUtc {get;set;}LastWriteTime Property System.DateTime LastWriteTime {get;set;}LastWriteTimeUtc Property System.DateTime LastWriteTimeUtc {get;set;}Length Property System.Int64 Length {get;}Name Property System.String Name {get;}

プロパティを見つけたら、PowerShell コマンドで使用することができます。

プロパティ値

特定の型のすべてのオブジェクトは同じプロパティを持っていますが、それらのプロパティの値は特定のオブジェクトを説明します。 例えば、すべてのFileInfoオブジェクトはCreationTimeプロパティを持っていますが、そのプロパティの値はファイルごとに異なります。

オブジェクトのプロパティの値を取得する最も一般的な方法は、ドット・メソッドを使用することです。 オブジェクトを含む変数や、オブジェクトを取得するコマンドなど、オブジェクトへの参照を入力します。 たとえば、次のコマンドは、PowerShell.exe ファイルの CreationTim プロパティの値を表示します。 Get-ChildItem コマンドは、PowerShell.exe ファイルを表すFileInfo オブジェクトを返します。 コマンドは括弧で囲まれており、プロパティにアクセスする前にコマンドが実行されるようになっています。

(Get-ChildItem $pshome\PowerShell.exe).creationtime
Tuesday, March 18, 2008 12:07:52 AM

次の例のように、オブジェクトを変数に保存してから、dotメソッドを使用してそのプロパティを取得することもできます。 Select-Object および Format-List にはそれぞれ、Property パラメーターがあります。 Propertyパラメータを使用すると、1つまたは複数のプロパティとその値を指定することができます。

例えば、次のコマンドは PowerShell.exe ファイルのすべてのプロパティの値を表示します。

Get-ChildItem $pshome\PowerShell.exe | Format-List -Property *
PSPath : Microsoft.PowerShell.Core\FileSystem::C:\Windows\System3 2\WindowsPowerShell\v1.0\PowerShell.exePSParentPath : Microsoft.PowerShell.Core\FileSystem::C:\Windows\System3 2\WindowsPowerShell\v1.0PSChildName : PowerShell.exePSDrive : CPSProvider : Microsoft.PowerShell.Core\FileSystemPSIsContainer : FalseMode : -a----VersionInfo : File: C:\Windows\System32\WindowsPowerShell\ v1.0\PowerShell.exe InternalName: POWERSHELL OriginalFilename: PowerShell.EXE.MUI FileVersion: 10.0.16299.15 (WinBuild.160101.0800) FileDescription: Windows PowerShell Product: Microsoft Windows Operating System ProductVersion: 10.0.16299.15 Debug: False Patched: False PreRelease: False PrivateBuild: False SpecialBuild: False Language: English (United States)BaseName : PowerShellTarget : {C:\Windows\WinSxS\amd64_microsoft-windows-powershell-ex e_31bf3856ad364e35_10.0.16299.15_none_8c022aa6735716ae\p owershell.exe}LinkType : HardLinkName : PowerShell.exeLength : 449024DirectoryName : C:\Windows\System32\WindowsPowerShell\v1.0Directory : C:\Windows\System32\WindowsPowerShell\v1.0IsReadOnly : FalseExists : TrueFullName : C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exExtension : .exeCreationTime : 9/29/2017 6:43:19 AMCreationTimeUtc : 9/29/2017 1:43:19 PMLastAccessTime : 9/29/2017 6:43:19 AMLastAccessTimeUtc : 9/29/2017 1:43:19 PMLastWriteTime : 9/29/2017 6:43:19 AMLastWriteTimeUtc : 9/29/2017 1:43:19 PMAttributes : Archive

Static properties

PowerShell で .NET クラスの静的プロパティを使用することができます。

クラスの静的プロパティを取得するには、Get-Member コマンドレットの Static パラメータを使用します。

Get-Date | Get-Member -MemberType Property -Static
TypeName: System.DateTimeName MemberType Definition---- ---------- ----------MaxValue Property static datetime MaxValue {get;}MinValue Property static datetime MinValue {get;}Now Property datetime Now {get;}Today Property datetime Today {get;}UtcNow Property datetime UtcNow {get;}

静的プロパティの値を取得するには、次の構文を使用します。

::<Property>

たとえば、次のコマンドは、System.DateTimeクラスのUtcNow staticプロパティの値を取得します。

::UtcNow

Properties of scalar objects and collections

Properties of one (“scalar”) object of a particular type are often different from the properties of a collection of objects of the same type.For example, every service has as DisplayName property, but a collection of services does not have a DisplayName property.For example.

The following command gets the DisplayName property of the’Audiosrv’ service.

(Get-Service Audiosrv).DisplayName
Windows Audio

PowerShell 3.0 以降では、スカラーオブジェクトとコレクションの異なるプロパティから生じるスクリプトエラーを防止しようと努めています。

(Get-Service).DisplayName
Application ExperienceApplication Layer Gateway ServiceWindows All-User Install AgentApplication IdentityApplication Information...

コレクションを送信し、単一 (「スカラー」) オブジェクトにのみ存在するプロパティを要求すると、PowerShell はコレクション内のすべてのオブジェクトについてそのプロパティの値を返します。

(Get-Service).Count
176

PowerShell 3.0 以降、ゼロまたは 1 オブジェクトの Count または Length プロパティを要求すると、PowerShell は正しい値を返します。

(Get-Service Audiosrv).Count
1

個々のオブジェクトとコレクションにプロパティがある場合、コレクションのプロパティだけが返されます。

コメントを残す

メールアドレスが公開されることはありません。