PowerShell Uri クラスのプロパティと設定値を一括一覧取得する例

PowerShell
Uri クラス (System)
URI (Uniform Resource Identifier) のオブジェクト表現と URI の部分への簡単なアクセスを提供します。

 Uri クラス (System) | Microsoft Docs より、プロパティの一覧を取得するC#のコード例が載っている。

C#の場合

Uri uri = new Uri("https://user:password@www.contoso.com:80/Home/Index.htm?q1=v1&q2=v2#FragmentName");

Console.WriteLine($"AbsolutePath: {uri.AbsolutePath}");
Console.WriteLine($"AbsoluteUri: {uri.AbsoluteUri}");
Console.WriteLine($"DnsSafeHost: {uri.DnsSafeHost}");
Console.WriteLine($"Fragment: {uri.Fragment}");
Console.WriteLine($"Host: {uri.Host}");
Console.WriteLine($"HostNameType: {uri.HostNameType}");
Console.WriteLine($"IdnHost: {uri.IdnHost}");
Console.WriteLine($"IsAbsoluteUri: {uri.IsAbsoluteUri}");
Console.WriteLine($"IsDefaultPort: {uri.IsDefaultPort}");
Console.WriteLine($"IsFile: {uri.IsFile}");
Console.WriteLine($"IsLoopback: {uri.IsLoopback}");
Console.WriteLine($"IsUnc: {uri.IsUnc}");
Console.WriteLine($"LocalPath: {uri.LocalPath}");
Console.WriteLine($"OriginalString: {uri.OriginalString}");
Console.WriteLine($"PathAndQuery: {uri.PathAndQuery}");
Console.WriteLine($"Port: {uri.Port}");
Console.WriteLine($"Query: {uri.Query}");
Console.WriteLine($"Scheme: {uri.Scheme}");
Console.WriteLine($"Segments: {string.Join(", ", uri.Segments)}");
Console.WriteLine($"UserEscaped: {uri.UserEscaped}");
Console.WriteLine($"UserInfo: {uri.UserInfo}");

// AbsolutePath: /Home/Index.htm
// AbsoluteUri: https://user:password@www.contoso.com:80/Home/Index.htm?q1=v1&q2=v2#FragmentName
// DnsSafeHost: www.contoso.com
// Fragment: #FragmentName
// Host: www.contoso.com
// HostNameType: Dns
// IdnHost: www.contoso.com
// IsAbsoluteUri: True
// IsDefaultPort: False
// IsFile: False
// IsLoopback: False
// IsUnc: False
// LocalPath: /Home/Index.htm
// OriginalString: https://user:password@www.contoso.com:80/Home/Index.htm?q1=v1&q2=v2#FragmentName
// PathAndQuery: /Home/Index.htm?q1=v1&q2=v2
// Port: 80
// Query: ?q1=v1&q2=v2
// Scheme: https
// Segments: /, Home/, Index.htm
// UserEscaped: False
// UserInfo: user:password

 それを、PowerShell版に置き換えた場合のコード例

工夫したところは、プロパティ名をリスト出力し、コマンド文字列を動的生成した上で、Invoke-Expression を使って結果出力させているところ。

プロパティ名をリスト出力し、コマンド文字列を動的生成した上で、Invoke-Expression を使って結果出力させる手法は、これ以外のクラス(オブジェクト)でも利用できる。

Powershell版

実行結果

$URI Property Count: 22
1 $URI.AbsolutePath : /Home/Index.htm
2 $URI.AbsoluteUri : https://user:password@www.contoso.com:80/Home/Index.htm?q1=v1&q2=v2#FragmentName
3 $URI.Authority : www.contoso.com:80
4 $URI.DnsSafeHost : www.contoso.com
5 $URI.Fragment : #FragmentName
6 $URI.Host : www.contoso.com
7 $URI.HostNameType : Dns
8 $URI.IdnHost : www.contoso.com
9 $URI.IsAbsoluteUri : True
10 $URI.IsDefaultPort : False
11 $URI.IsFile : False
12 $URI.IsLoopback : False
13 $URI.IsUnc : False
14 $URI.LocalPath : /Home/Index.htm
15 $URI.OriginalString : https://user:password@www.contoso.com:80/Home/Index.htm?q1=v1&q2=v2#FragmentName
16 $URI.PathAndQuery : /Home/Index.htm?q1=v1&q2=v2
17 $URI.Port : 80
18 $URI.Query : ?q1=v1&q2=v2
19 $URI.Scheme : https
20 $URI.Segments : / Home/ Index.htm
21 $URI.UserEscaped : False
22 $URI.UserInfo : user:password
ライセンス:本記事のテキスト/コードは特記なき限り CC BY 4.0 です。引用の際は出典URL(本ページ)を明記してください。
利用ポリシー もご参照ください。

コメント

タイトルとURLをコピーしました