AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
在命令周围加上花括号,Powershell会将包含的内容作为一个ScriptBlock对象。 ~~~powershell PS D:\Projects\htk\asr> {ping 127.0.0.1} | gm TypeName:System.Management.Automation.ScriptBlock Name MemberType Definition ---- ---------- ---------- CheckRestrictedLanguage Method void CheckRestrictedLanguage(System.Collections.Generic.IEnumer... Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetNewClosure Method scriptblock GetNewClosure() GetObjectData Method void GetObjectData(System.Runtime.Serialization.SerializationIn... GetPowerShell Method powershell GetPowerShell(Params System.Object[] args), powershe... GetSteppablePipeline Method System.Management.Automation.SteppablePipeline GetSteppablePipe... GetType Method type GetType() Invoke Method System.Collections.ObjectModel.Collection[psobject] Invoke(Para... InvokeReturnAsIs Method System.Object InvokeReturnAsIs(Params System.Object[] args) InvokeWithContext Method System.Collections.ObjectModel.Collection[psobject] InvokeWithC... ToString Method string ToString() Ast Property System.Management.Automation.Language.Ast Ast {get;} Attributes Property System.Collections.Generic.List[System.Attribute] Attributes {g... DebuggerHidden Property bool DebuggerHidden {get;set;} File Property string File {get;} Id Property guid Id {get;} IsConfiguration Property bool IsConfiguration {get;set;} IsFilter Property bool IsFilter {get;set;} Module Property psmoduleinfo Module {get;} StartPosition Property System.Management.Automation.PSToken StartPosition {get;} ~~~ ScriptBlock对象默认不会执行,只是原样输出文本,如下所示 ~~~powershell PS D:\Projects\htk\asr> ping 127.0.0.1 正在 Ping 127.0.0.1 具有 32 字节的数据: 来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128 来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128 来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128 来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128 127.0.0.1 的 Ping 统计信息: 数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失), 往返行程的估计时间(以毫秒为单位): 最短 = 0ms,最长 = 0ms,平均 = 0ms PS D:\Projects\htk\asr> {ping 127.0.0.1} ping 127.0.0.1 ~~~ 但是我们可以通过调用操作符`&`来执行ScriptBlock的内容,ScriptBlock中的命令会被逐行执行。 ~~~powershell PS D:\Projects\htk\asr> & {ping 127.0.0.1} 正在 Ping 127.0.0.1 具有 32 字节的数据: 来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128 来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128 来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128 来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128 127.0.0.1 的 Ping 统计信息: 数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失), 往返行程的估计时间(以毫秒为单位): 最短 = 0ms,最长 = 0ms,平均 = 0ms ~~~