PowerShell: 入门

From:Wrox_-_Professional_Windows_PowerShell_(2008)

PowerShell 是基于.Net Framework2.0的,另外它在一个运用到另一个运用中传递的是对象(.Net Framework 对象)而不是字符串或文本。

在运行时可通过get-member命令获取.Net类的成员。PowerShell也提供语法允许利用.net FrameWork2.0类的静态成员,例如$now=[System.DateTime]::Now. 为了调用.net framework的静态类成员,需要将类名有方括号括起来,用两个冒号指定需要的属性名或静态方法。

PowerShell提供get-wmiobject命令,允许获取机器状态信息。 例如get-wmiobject Win32-currentTime可返回当前使用WMI的日期和时间。还可以运用WMI访问远端机器(核心的Powershell命令只能访问本地机器)

Windows PowerShell team的目标是下一代的微软管理控制台(MWC3.0) PowerShell命令之上的图形化用户接口。

-Whatif 参数可让你看见当命令执行时将发生什么,用此参数可防止不必要的系统损坏。

Where-object用于过滤对象,sort-object用于排序对象

PowerShell有两个命令Start-transcript,Stop-transcript。可用于将在命令行中输入的命令和显示的相关信息copy到指定的文件中。

Powershell中几个数据存储都作为Dirve,比如files, folders, registry, aliases, cerfificates, environment variables, functions… get-psdrive 命令可返回所有系统中定义的drive。另一个方法是get-psprovider. 由于注册表也是作为drive使用,可像文件系统那样导航。Set-location,设置当前位置,比如 set-location HKLM:, 也可用命令cd HKLM:, 设置当前位置是HKEY_LOCAL_MACHINE.

当起路径保存在$pwd中。 Dir命令等同于get-childitem 可返回指定路径下符合查询条件的文件

Active Directory provider 在以前的版本中有但是PowerShell1.0中给去掉了,可能在以后的版本中还会出现。现在可以用.Net Framework2.0相关类来呈现或维护Active Directory。例如$AD=new-object System.DirectoryServices.DirectoryEntry

证书(Certificates)在PowerShell中用cert drive呈现证书信息。证书有层次结构,例如CurrentUser, LocalMachinePowerShelldrives提供一套统一的参数,但是一些特殊情况也有自己的参数,比如Certificatecodesigning参数而其它的就没有。

可以用new-alias自定义别名,用get-alias获取当前的别名信息。

在未来的PowerShell版本上,COM将用越来越少。WMI对象可弥补PowerShell对访问远程文件的缺陷。

通配符

?           指点位置精确匹配一个字符.

*           匹配0到多个字符.

[abc]     匹配一类字符. 方括号中的任意字符出现就算匹配

[a-c]     匹配一定范围的字符. 如果连字符前的字符与连字符后面字符之间的字符出现就算匹配

 

自动变量

Variable

 Description

描述

$$

Contains the last token received from the last line of code received by the command shell.

保存最后一个从最后一个代码行接收到的token

$?

 Contains the success/fail status of the last operation carried out by the command shell. Holds the boolean value True or False.

保存最后一个操作执行后的成功或失败状态,Ture/False

$^

Contains the first token received from the last line of code received by the command shell.

保存第一个从最后一个代码行接收的token

$_

Contains the current pipeline object. Used by the where-object cmdlet, for example.

保存当前的pipeline对象,例如where-object命令使用

$Args

An array of the parameters, not explicitly defined by name, passed to a function.

参数数组,没有特别地用名字指定传到函数

$ConfirmPreference

Specifies what to do before PowerShell carried out an action that has side effects.

指定Powershell执行一个有副作用的动作前做什么

$ConsoleFileName

The name of the current console file.

当前的console文件名

$DebugPreference

Specifies the debugging policy.

指定debuging策略

$Error

An array of error objects.

错误对象数组

$ErrorActionPreference

Specifies how errors are to be responded to.

指定错误如何响应

$ErrorView

Specifies the mode for displaying errors.

指定显示错误的方式

Variable

 Description

描述

$ExecutionContext

Specifies the execution objects available to cmdlets.

指定对cmdlet有效的执行对象

$False

The boolean value False.

布尔值False

$FormatEnumerationLimit

Specifies the limit on the enumeration of IEnumerable objects.

指定列举接口对象的列举限制

$Home

Specifies the home directory for the current user.

指定当前用户的主目录

$Host

Contains information about the PowerShell console.

保存PowerShell console的信息

$Input

Specifies the input to a script block in a pipeline.

指定一个pipeline中脚本段的输入

$MaximumAliasCount

Specifies the maximum number of aliases allowed.

指定允许的Alias最大数

$MaximumDriveCount

Specifies the maximum number of drives allowed.

指定允许的drive最大数

$MaximumErrorCount

Specifies the maximum number of errors stored in the $Error array.

指定允许的存储在Error数组中的error最大数

$MaximumFunctionCount

Specifies the maximum number of functions allowed in a session.

指定一个session中最大函数数

$MaximumHistoryCount

Specifies the maximum number of PowerShell commands stored in history.

指定Powershell命令最大历史保存数

$MaximumVariableCount

Specifies the maximum number of variables available in a session.

指定一个session中最大变量数

$MyInvocation

Contains information about how a script was called.

保存一个脚本如果调用信息

$NestedPromptLevel

 The level of nesting of a PowerShell prompt. The level is 0 for the outermost shell.

PowerShell提示的嵌套级别。级别是0到最外层

$null

The NULL value.

空值

$PID

The process ID for the PowerShell process.$pi.

 

$Profile

 The location of a users profile file (Profile.ps1).

用户profile文件位置

$ProgressPreference

 Specifies the action taken when progress records are delivered.

progress记录deliver时应采取的行动

$PSHome

 The directory that PowerShell is installed into.

PowerShell的安装目录

$PWD

The current (or present) working directory.

当前的工作目录

Variable

 Description

描述

$ReportErrorShow InnerException

When set to TRUE (1) causes the chain of inner exceptions to be  displayed.

设置为True(1)时显示异常的内部链

$ReportErrorShowSource

When set to TRUE (1) causes the assembly names of exceptions to be displayed.

设置为True(1)时显示异常的assemble

$ReportErrorShowStackTrace

When set to TRUE (1) causes the stack trace for exceptions to be  displayed.

设置为True(1)时显示异常的stack trace

$ShellId

Name of the PowerShell shell running (default is Microsoft.PowerShell.

Shell运行的名

$True

 The boolean value TRUE.

 

$VerbosePreference

 Specifies the action to take when the write-verbose cmdlet is used in a script to write data.

write-verbose cmdlet在脚本中使用时指定的动作

$WarningPreference

 Specifies the action to take after text is written using the writewarning cmdlet.

当用writewarning cmdlet用于写文本完成后采取的动作

$WhatIfPreference

 Specifies whether or not whatif is enabled for all command

指定-whatif是否对所有命令启用

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值