所在位置 行:1 字符: 16
+ $Host.UI.RawUI. <<<< ForegroundColor="Pink"
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
可以使用[System.Enum]::GetNames 方法查看ConsoleColor定义的所有颜色。
PS C:Powershell> [System.Enum]::GetNames([System.ConsoleColor])
Black
DarkBlue
DarkGreen
DarkCyan
DarkRed
DarkMagenta
DarkYellow
Gray
DarkGray
Blue
Green
Cyan
Red
Magenta
Yellow
White
有时一个属性期望的赋值必须是一个指定类型的对象。例如WindowSize,如果想改变Powershell的窗口大小,可是设置WindowSize属性,但是它是一个System.Management.Automation.Host.Size对象,怎样获取这个对象呢?
1.先读取属性,保存为临时变量,更改临时变量,将临时变量赋给WindowSize
2.直接创建一个System.Management.Automation.Host.Size,赋给WindowSize
PS C:Powershell> $tmp=$Host.UI.RawUI.WindowSize
PS C:Powershell> $tmp
Width Height
----- ------
100 60
PS C:Powershell> $tmp.Height=30
PS C:Powershell> $tmp.Width=60
PS C:Powershell> $Host.UI.RawUI.WindowSize=$tmp
Width Height
----- ------
60 30
PS C:Powershell> $Host.UI.RawUI.WindowSize=New-Object System.Management.Automation.Host.Size(60,40)
PS C:Powershell> $Host.UI.RawUI.WindowSize
Width Height
----- ------
60 40
查看所有属性
因为属性和方法都是对象的成员,可以使用Get-Member可以返回它们的成员的详细信息,如果只显示属性可以使用参数 memberType 为“Property”
PS C:Powershell> $host | Get-Member -memberType property
TypeName: System.Management.Automation.Internal.Host.InternalHost
Name MemberType Definition
---- ---------- ----------
CurrentCulture Property System.Globalization.CultureInfo CurrentCulture {get;}
CurrentUICulture Property System.Globalization.CultureInfo CurrentUICulture {get;}
InstanceId Property System.Guid InstanceId {get;}
IsRunspacePushed Property System.Boolean IsRunspacePushed {get;}
Name Property System.String Name {get;}
PrivateData Property System.Management.Automation.PSObject PrivateData {get;}










