[TOC]
0x00 前言简述
描述: 人生莫大的痛苦,莫过于学习下面这些命令参数,但是为了更好的掌握它又不得不去了解;
PS 内置变量 - Variable
1 | $PSVersionTable # 显示相关 PowerShell 版本信息的哈希表 |
变量操作
描述:为了管理变量PS提供了五个专门管理变量的命令
- Get-Variable
- Set-Variable
- New-Variable
- Remove-Variable
- Clear-Variable
GetType
描述:获取变量的类型1
2
3
4
5PS C:\Users\WeiyiGeek> Get-Help gettype*
PS C:\Users\WeiyiGeek> $var=1024;$var.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType
New-Variable
描述:以在定义变量时指定变量的一些其它属性,比如访问权限描述;
变量的选项是一个枚举值包含:
- “None”:默认设置
- “ReadOnly”:变量只读,但是可以通过-Force 选项更新。
- “Constant”:常量一旦声明,在当前控制台不能更新。
- “Private”:只在当前作用域可见,不能贯穿到其它作用域
- “AllScope”:全局,可以贯穿于任何作用域
基础实例:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#1.使用New-Variable命令实例
PS C:\test> New-Variable num -Value 100 -Force -Option readonly #option选项 在创建变量时给变量加上只读属性
PS C:\test> new-variable num -Value "strong" -Option constant #常量一旦声明不可修改,权限更高的变量选项Constant,
PS C:\test> $num=101
# Cannot overwrite variable num because it is read-only or constant.
#2.变量描述可以通过-description 添加变量描述,但是变量描述默认不会显示,可以通过Format-List 查看。
PS C:\test> new-variable name -Value "me" -Description "This is my name"
PS C:\test> ls Variable:name | fl *
# PSPath : Microsoft.PowerShell.CoreVariable::name
# PSDrive : Variable
# PSProvider : Microsoft.PowerShell.CoreVariable
# PSIsContainer : False
# Name : name
# Description : This is my name
Set-Variable
描述:设置变量1
2
3#1.设置变量为只读属性并且进行描述强制执行
$var="SetVariable"
Set-Variable var -Option "ReadOnly" -Description "This is readOnly" -Force
Get-Variable
描述:利用该命令获取创建的变量相关的信息;
1 | #0.常规查看变量 |
时间日期
Get-Date
描述:时间日期对象可以直接利用其属性计算出当前年中的天数以及几天前和一天后的日期等等;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34Get-Date | Format-Custom {$_} #$_表示管道中当前对象
class DateTime
{
$_ = class DateTime
{
Day = 27
DayOfWeek = Wednesday
DayOfYear = 331
Hour = 15
Kind = Local
Millisecond = 132
Minute = 41
Month = 11
Second = 5
Ticks = 637104660651327983
TimeOfDay =
class TimeSpan
{
Ticks = 564651327983
Days = 0
Hours = 15
Milliseconds = 132
Minutes = 41
Seconds = 5
TotalDays = 0.653531629609954
TotalHours = 15.6847591106389
TotalMilliseconds = 56465132.7983
TotalMinutes = 941.085546638333
TotalSeconds = 56465.1327983
}
Year = 2019
DateTime = 2019年11月27日 15:41:05
}
}
1 | #系统当前时间 |