|
ここでは、Windows PowerShell Variables のサンプルを紹介します。
- "Memory Mbyte " + [int]($Mem.TotalPhysicalMemory/$Mbyte)
次の事例を実行してみよう。
# http://www.computerperformance.co.uk/powershell/powershell_variables.htm
# PowerShell $ Variable Example
$Mem= WmiObject Win32_ComputerSystem
$Mbyte =1048576 # Another variable
"Memory Mbyte " + [int]($Mem.TotalPhysicalMemory/$Mbyte)
$Mem
3行目で、Win32_ComputerSystem の WMI オブジェクトを取得して、$Mem に格納します。
4行目で、1048576 を $Mbyte に格納します。
5行目で、$Mem.TotalPhysicalMemory / $Mbyte の演算結果を表示します。MB 単位で表示します。
7行目で、$Mem を表示します。
それでは、実行してみましょう。下記のような結果が得られます。
Memory Mbyte 3327
Domain : WORKGROUP
Manufacturer : System manufacturer
Model : P5K-E
Name : KAMxxxxx-PC7-14
PrimaryOwnerName : kamxxxxx
TotalPhysicalMemory : 3488731136
上記を実行すると
PowerShell 7.15 では、エラーです。何故か Object が無い??
Get-WmiObject: The term 'Get-WmiObject' is not recognized as a name
of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- [int]$a = 7 ; $a + 3
次の事例を実行してみよう。
# Declaring PowerShell Integer Variable
[int]$a = 7
$a + 3
$a
# PS>10
2行目で、7 を [int] にキャストして $a に格納します。
3行目で、$a と 3 の加算した結果を表示します。
4行目で、$a を表示します。
それでは、実行してみましょう。下記のような結果が得られます。
10
7
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- [int]$a =7 ; $a ="Twenty"
次の事例を実行してみよう。
# Declaring PowerShell Integer Variable
[int]$a =7
$a ="Twenty"
$a
# PS> Error "Cannot Convert value.
2行目で、7 を [int] にキャストして $a に格納します。
3行目で、"Twenty" を $a に格納します。
4行目で、$a を表示します。
それでは、実行してみましょう。下記のような結果が得られます。
値 "Twenty" を型 "System.Int32" に変換できません。エラー: "入力文字列の形式が正しくありません。"
発生場所 F:\usr\PowerShell\sample_06\079_PS_Variables_03.ps1:3 文字:1
+ $a ="Twenty"
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) []、RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromStringToInteger
7
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- $b = 7 ; $b = "Twenty"
次の事例を実行してみよう。
$b = 7
$b = "Twenty"
$b
# PS> Twenty
1行目で、7 を $b に格納します。
2行目で、"Twenty" を $b に格納します。
3行目で、$b を表示します。
それでは、実行してみましょう。下記のような結果が得られます。
Twenty
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- $DriveA, $DriveB, $DriveC, $DriveD = 250, 175, 330, 200
次の事例を実行してみよう。
# ----< Declaring Multiple Variables >----
$DriveA, $DriveB, $DriveC, $DriveD = 250, 175, 330, 200
Write-Host "`$DriveA = $DriveA"
Write-Host "`$DriveB = $DriveB"
Write-Host "`$DriveC = $DriveC"
Write-Host "`$DriveD = $DriveD"
2行目で、250, 175, 330, 200 を マルチ変数 $DriveA, $DriveB, $DriveC, $DriveD に格納します。
4〜7行で、各変数を表示しています。`$ は、エスケープして、$ を文字として表示します。
それでは、実行してみましょう。下記のような結果が得られます。
$DriveA = 250
$DriveB = 175
$DriveC = 330
$DriveD = 200
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Foreach ($CIM in $WMI) {$i++}
次の事例を実行してみよう。
# PowerShell Variables Including $_.
$i=0
$Type = "Win32"
$WMI = Get-WmiObject -List | Where-Object {$_.name -Match $Type}
Foreach ($CIM in $WMI) {$i++}
Write-Host 'There are '$i' types of '$Type
3行目で、"Win32" を $Type に格納します。
4行目で、WMI オブジェクトリストを取得して、.name が $Type であるものを $WMI に格納します。
5行目で、オブジェクトの個数をカウントします。
6行目で、個数を表示します。
それでは、実行してみましょう。下記のような結果が得られます。
There are 835 types of Win32
上記を実行すると
PowerShell 7.15 では、エラーです。何故か Object が無い??
Get-WmiObject: The term 'Get-WmiObject' is not recognized as a name
of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Set-Variable Thermometer 32 -option constant
次の事例を実行してみよう。
# Example: PowerShell Set-Variable constant
Set-Variable Thermometer 32 -option constant
$Thermometer
2行目で、32 を Thermometer にコンスタントとして、格納します。
4行目で、$Thermometer を表示します。
それでは、実行してみましょう。下記のような結果が得られます。
32
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Set-Variable AllOverPlace 99 -scope global
次の事例を実行してみよう。
# Example: PowerShell Set-Variable Global
Set-Variable AllOverPlace 99 -scope global
$AllOverPlace
2行目で、99 を AllOverPlace にグローバル変数として、格納します。
4行目で、$AllOverPlace を表示します。
それでは、実行してみましょう。下記のような結果が得られます。
99
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- $global:runners = 8
次の事例を実行してみよう。
# Set PowerShell Global Variable
$global:runners = 8
$runners
2行目で、8 を $runners にグローバル変数として、格納します。
4行目で、$runners を表示します。
それでは、実行してみましょう。下記のような結果が得られます。
8
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- $alert = Get-Service NetLogon
次の事例を実行してみよう。
# ----< PowerShell is Dot Properties >----
$alert = Get-Service NetLogon
$alert.status
# PS> Started
Write-Host "----< `$alert %gt;----"
$alert
2行目で、登録されたサービス NetLogon を取得して、$alert に格納します。
3行目で、$alert.status を表示します。
8行目で、$alert を表示します。
それでは、実行してみましょう。下記のような結果が得られます。
Stopped
----< $alert >----
Status Name DisplayName
------ ---- -----------
Stopped Netlogon NetLogon
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Get-Service | Where-Object {$_.status -eq "Running" }
次の事例を実行してみよう。
# PowerShell Pipeline $_. example
Get-Service | Where-Object {$_.status -eq "Running" }
登録されているサービスを取得して、.status が "Running" であるものを表示します。
それでは、実行してみましょう。下記のような結果が得られます。
Status Name DisplayName
------ ---- -----------
Running AdobeARMservice Adobe Acrobat Update Service
Running AMD External Ev... AMD External Events Utility
Running AppHostSvc Application Host Helper Service
Running Appinfo Application Information
Running Apple Mobile De... Apple Mobile Device
Running AudioEndpointBu... Windows Audio Endpoint Builder
Running Audiosrv Windows Audio
Running BFE Base Filtering Engine
Running BITS Background Intelligent Transfer Ser...
< 省略 >
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Get-WmiObject -List | Where-Object {$_.name -Like "CIM*"}
次の事例を実行してみよう。
# ----< Example to find all WmiObjects containing 'CIM' >----
Get-WmiObject -List | Where-Object {$_.name -Like "CIM*"}
WMI オブジェクトリストを取得して、.name の先頭が CIM であるものを表示します。
それでは、実行してみましょう。下記のような結果が得られます。
NameSpace: ROOT\cimv2
Name Methods Properties
---- ------- ----------
CIM_ManagedSystemElement {} {Caption, Description, InstallDate, Name...}
CIM_LogicalElement {} {Caption, Description, InstallDate, Name...}
CIM_LogicalDevice {SetPowerState, R... {Availability, Caption, ConfigManagerErrorC...
CIM_Processor {SetPowerState, R... {AddressWidth, Availability, Caption, Confi...
CIM_NetworkAdapter {SetPowerState, R... {AutoSense, Availability, Caption, ConfigMa...
CIM_StorageExtent {SetPowerState, R... {Access, Availability, BlockSize, Caption...}
CIM_StorageVolume {SetPowerState, R... {Access, Availability, BlockSize, Caption...}
CIM_LogicalDisk {SetPowerState, R... {Access, Availability, BlockSize, Caption...}
CIM_Memory {SetPowerState, R... {Access, AdditionalErrorData, Availability,...
< 省略 >
上記を実行すると
PowerShell 7.15 では、エラーです。何故か Object が無い??
Get-WmiObject: The term 'Get-WmiObject' is not recognized as a name
of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Get-Variable | Format-Table name, value -auto
次の事例を実行してみよう。
# ----< More Built-In PowerShell Variables >----
Get-Variable | Format-Table name, value -auto
変数オブジェクトを取得して、name, value のテーブルに整形して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
Name Value
---- -----
$ ./079_PS_Variables_12.ps1
? True
^ ./079_PS_Variables_12.ps1
AllOverPlace 99
args {}
ConfirmPreference High
ConsoleFileName
DebugPreference SilentlyContinue
< 省略 >
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- $Host
次の事例を実行してみよう。
# Information about the currently executing host
$Host
予約変数 $Host を表示します。
それでは、実行してみましょう。下記のような結果が得られます。
Name : ConsoleHost
Version : 5.0.10586.494
InstanceId : 5b732b29-4575-4bf7-9784-a6f0f4bef350
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : ja-JP
CurrentUICulture : ja-JP
PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled : True
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- $Env:Path = $Env:Path + ";F:\usr\PowerShell\sample_06"
次の事例を実行してみよう。
# ---- Environmental Path to files. ----
# Clear-Host
$Env:Path = $Env:Path + ";F:\usr\PowerShell\sample_06"
$Env:Path
3行目で、環境変数 $Env:Path の最交尾に ";F:\usr\PowerShell\sample_06" を追加しています。
5行目で、環境変数 $Env:Path を表示します。
それでは、実行してみましょう。下記のような結果が得られます。
< 省略 >
;F:\usr\PowerShell\sample_06
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Get-ChildItem Env:\
次の事例を実行してみよう。
# ---- Talking of Env variables, you can list them with gci thus: ----
Get-ChildItem Env:\
環境変数一覧を取得して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
Name Value
---- -----
ALLUSERSPROFILE C:\ProgramData
APPDATA C:\Users\kamifuji\AppData\Roaming
asl.log Destination=file
CommonProgramFiles C:\Program Files\Common Files
< 省略 >
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- $Files = "C:\Windows" | Get-Member
次の事例を実行してみよう。
$Files = "C:\Windows" | Get-Member
$Files
1行目で、"C:\Windows" 文字列のメンバーを $Files に格納します。
3行目で、$Files を表示します。
それでは、実行してみましょう。下記のような結果が得られます。
TypeName: System.String
Name MemberType Definition
---- ---------- ----------
Clone Method System.Object Clone(), System.Object ICloneable.Clone()
CompareTo Method int CompareTo(System.Object value), int CompareTo(string strB...
Contains Method bool Contains(string value)
CopyTo Method void CopyTo(int sourceIndex, char[] destination, int destinat...
EndsWith Method bool EndsWith(string value), bool EndsWith(string value, Syst...
< 省略 >
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- $Bit = 64 | Get-Member
次の事例を実行してみよう。
$Bit = 64 | Get-Member
$Bit
1行目で、64 整数(int32)のメンバーを $Bit に格納します。
3行目で、$Bit を表示します。
それでは、実行してみましょう。下記のような結果が得られます。
TypeName: System.Int32
Name MemberType Definition
---- ---------- ----------
CompareTo Method int CompareTo(System.Object value), int CompareTo(int value), int IComparable...
Equals Method bool Equals(System.Object obj), bool Equals(int obj), bool IEquatable[int].Eq...
GetHashCode Method int GetHashCode()
GetType Method type GetType()
GetTypeCode Method System.TypeCode GetTypeCode(), System.TypeCode IConvertible.GetTypeCode()
ToBoolean Method bool IConvertible.ToBoolean(System.IFormatProvider provider)
ToByte Method byte IConvertible.ToByte(System.IFormatProvider provider)
ToChar Method char IConvertible.ToChar(System.IFormatProvider provider)
< 省略 >
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Get-Help about_Variable
次の事例を実行してみよう。
Get-Help about_Variable
Help about_Automatic_Variables
Help about_Environment_Variables
Help about_Preference_Variables
1行目で、about_Variable のヘルプを表示します。
2行目で、about_Automatic_Variables のヘルプを表示します。
3行目で、about_Environment_Variables のヘルプを表示します。
4行目で、about_Preference_Variables のヘルプを表示します。
それでは、実行してみましょう。下記のような結果が得られます。
" > パイプ問題(?)" 関連で、下記のファイルを差し替えました。
表示内容は、079_PS_Variables_19_1.txt に保存しました。
上記を実行すると
PowerShell 7.15 では、エラーです。何故か Object が無い??
PS xxxx> Get-Help about_Variable
Get-Help: Get-Help could not find about_Variable in a help file in
this session. To download updated help topics type: "Update-Help".
To get help online, search for the help topic in the TechNet library
at https://go.microsoft.com/fwlink/?LinkID=107116.
PS xxxx> Help about_Automatic_Variables
Get-Help:
Line |
64 | $help = Get-Help @PSBoundParameters
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Get-Help could not find about_Automatic_Variables in a help
file in this session. To download updated help topics type: "Update-Help".
To get help online, search for the help topic in the TechNet library
at https://go.microsoft.com/fwlink/?LinkID=107116.
PS xxxx> Help about_Environment_Variables
Get-Help:
Line |
64 | $help = Get-Help @PSBoundParameters
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Get-Help could not find about_Environment_Variables in a help
file in this session. To download updated help topics type: "Update-Help".
To get help online, search for the help topic in the TechNet library
at https://go.microsoft.com/fwlink/?LinkID=107116.
PS xxxx> Help about_Preference_Variables
Get-Help:
Line |
64 | $help = Get-Help @PSBoundParameters
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Get-Help could not find about_Preference_Variables in a help file
in this session. To download updated help topics type: "Update-Help".
To get help online, search for the help topic in the TechNet library
at https://go.microsoft.com/fwlink/?LinkID=107116.
PS xxxx>
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Get-ChildItem $Files
次の事例を実行してみよう。
$Files = "C:\Windows" # Is so much easier than New-Variable
Get-ChildItem $Files
1行目で、"C:\Windows" を $Files に格納します。
2行目で、$Files から子アイテムを取得して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
ディレクトリ: C:\Windows
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2015/10/30 14:48 addins
d----- 2015/12/19 3:42 AppCompat
d----- 2016/07/13 20:05 apppatch
d----- 2016/07/31 13:43 AppReadiness
d-r--- 2016/08/10 11:56 assembly
< 省略 >
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- New-Variable files -value "C:\windows"
次の事例を実行してみよう。
# ---- Declaring Variables with New-Variable ----
$Files = "D:\"
Remove-Variable Files
New-Variable files -value "C:\windows"
Get-ChildItem $Files
2行目で、"D:\" を $Files に格納します。
3行目で、変数 $Files を削除します。
4行目で、$Files を作成して、"C:\windows" を $Files に格納します。
5行目で、$Files から子アイテムを取得して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
ディレクトリ: C:\Windows
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2015/10/30 14:48 addins
d----- 2015/12/19 3:42 AppCompat
d----- 2016/07/13 20:05 apppatch
d----- 2016/07/31 13:43 AppReadiness
d-r--- 2016/08/10 11:56 assembly
< 省略 >
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
|