Powershell

常用指令

$pshome :powershell的主目录

$profile :显示 Windows PowerShell 配置文件的路径

test-path $profile :确定是否已经在系统上创建了 Windows PowerShell 配置文件

使用如图所示:

    

使用配置

powershell.exe 主机配置文件(在 Windows Vista 中)的位置如下所示:

%windir%\system32\Windows­PowerShell\v1.0\profile.ps1用于计算机的所有用户和所有外壳。

%windir%\system32\Windows­PowerShell\v1.0\Microsoft.Power­Shell_profile.ps1 用于计算机的所有用户,但仅用于 Microsoft.PowerShell 外壳。

%UserProfile%\Documents\Windows­PowerShell\profile.ps1仅用于当前用户和所有外壳。

%UserProfile%\Documents\WindowsPowerShell\Micro­soft.PowerShell_profile.ps1仅用于当前用户和 Microsoft.PowerShell 外壳。

  启动时按顺序加载,最后一个优先级最高,会覆盖之前的配置文件 这些配置文件并不是在默认情况下创建的。必须在您手动创建后,它们才会出现。任何创建文本文件的方式,在这里都适用。但请注意文件的扩展名必须是.ps1。简单起见,我们使用命令类创建,创建适用于所有用户和所有 shell 的配置文件,键入:

  new-item -path $env:windir\System32\WindowsPowerShell\v1.0\profile.ps1 -itemtype file -force

  

便在所示目录下创建了配置文件,然后使用notepad(如果安装的话)编辑文件:

  notepad $env:windir\System32\WindowsPowerShell\v1.0\profile.ps1

输入如下内容:

d:
function pp 
{ 
   write-host "ppc" 
}

  编辑后保存,然后再重新运行powershell.exe,会加载profile.ps1中的内容,启动后如果自动跳转到D:路径下,说明文件已加载,可进一步测试 pp函数。

  如果出现PowerShell 默认不允许执行*.ps1脚本文件。如下图所示:

   

  可以通过Get-ExecutionPolicy,来取得当前策略。用Set-ExecutionPolicy设置当前策略。下面的命令可以解决上面的错误

PS C:\Windows\system32> Set-ExecutionPolicy RemoteSigned  <按回车>
Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose 
you to the security risks described in the about_Execution_Policies help topic. Do you want to change the execution
policy?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"):<按Y>

Policy的有效参数:

-- Restricted:  不载入任何配置文件,不运行任何脚本。 "Restricted" 是默认的。

-- AllSigned: 只有被Trusted publisher签名的脚本或者配置文件才能使用,包括你自己再本地写的脚本

-- RemoteSigned:  对于从Internet上下载的脚本或者配置文件,只有被Trusted publisher签名的才能使用。

-- Unrestricted: 可以载入所有配置文件,可以运行所有脚本文件. 如果你运行一个从internet下载并且没有签名的脚本,在运行之前,你会被提示需要一定的权限。

-- Bypass: 所有东西都可以使用,并且没有提示和警告.

-- Undefined: 删除当前scope被赋予的Execution Policy.  但是Group Policy scope的Execution Policy不会被删除.

 自定义配置

需要注意的是使用Notepad++编辑文件会有问题,可以用Windows自带的notepad编辑,建议使用ps1文件右键中的编辑菜单调用powershell_ise.exe进行编辑。

如图所示:

编辑完成后可直接点击运行按钮来测试脚本文件。

function wac ($x){
    if($x.StartsWith(“.”)){
        $ip='192.168'+$x
        write-host 'adb connect'$ip
        adb connect $ip
    }else{
        write-host 'adb connect'$x
        adb connect $x
    }
    adb root
}
function wad{
    write-host 'adb devices'
    adb devices
}
function was{
    write-host 'adb shell'
    adb shell
}
function wa($param){
    if ($param -eq 'server'){
        write-host 'adb kill-server'
        adb kill-server
        write-host 'adb start-server'
        adb start-server
        wad
    }elseif ($param -eq 'remount'){
        write-host 'adb remount'
        adb shell 'mount -o remount,rw /system'
    }  
}
function wak($value){
    if ($value -eq 'home'){
        write-host 'key home 3'
        adb shell input keyevent "3"
    }elseif ($value -eq 'menu'){
        write-host 'key menu 82'
        adb shell input keyevent "82"
    }elseif ($value -eq 'back'){
        write-host 'key back 4'
        adb shell input keyevent "4"
    }elseif ($value -eq 'u'){
        write-host 'key up 19'
        adb shell input keyevent "19"
    }elseif ($value -eq 'd'){
        write-host 'key down 20'
        adb shell input keyevent "20"
    }elseif ($value -eq 'l'){
        write-host 'key left 21'
        adb shell input keyevent "21"
    }elseif ($value -eq 'r'){
        write-host 'key right 22'
        adb shell input keyevent "22"
    }elseif ($value -eq 'ok'){
        write-host 'key ok 23'
        adb shell input keyevent "23"
    }else{
        write-host 'key back null'
    }
}

function test 
{
    write-host 'function test'
}

 

PowerShell String对象方法 

从之前的章节中,我们知道PowerShell将一切存储在对象中,那这些对象中包含了一系列中的称之为方法的指令。默认文本存储在String对象中,它包含了许多非常有用的处理文本的命令。例如,要确定一个文件的扩展名,可以使用LastIndexOf()获取最后一个字符“.”的位置,继续使用Substring()获取扩展名子串。

PS> $path = "C:\prefs.js" 
PS> $path.Substring( $path.LastIndexOf(".")+1 ) 
Js

另外一条途径,使用Split方法,对文件的完整名称进行分割,得到一个字符串数组,取最后一个元素,PowerShell中可以通过索引-1来获取数组中最后一个元素。

PS> $path.Split(".")[-1] 
Js

下面的表格会给出String对象的所有方法:

函数 描述 示例
CompareTo() 与另一个字符串比较 (“Hello”).CompareTo(“Hello”)
Contains() 是否包含制定子串 (“Hello”).Contains(“ll”)
CopyTo() 拷贝子串至新字符串中 $a = (“HelloWorld”).toCharArray()(“User!”).CopyTo(0,

 

$a, 6, 5)$a

EndsWith() 是否以制定子串结尾 (“Hello”).EndsWith(“lo”)
Equals() 是否与另一个字符串相同 (“Hello”).Equals($a)
IndexOf() 返回第一次匹配的所索引 (“Hello”).IndexOf(“l”)
IndexOfAny() 返回字符串中任意字符的首次匹配索引 (“Hello”).IndexOfAny(“loe”)
Insert() 在指定位置插入字符串 (“HelloWorld”).Insert(6,”brave “)
GetEnumerator() 枚举字符串中所有字符 (“Hello”).GetEnumerator()
LastIndexOf() 字符的最后匹配位置 (“Hello”).LastIndexOf(“l”)
LastIndexOfAny() 任意字符的最后匹配位置 (“Hello”).LastIndexOfAny(“loe”)
PadLeft() 左边补齐空白是字符串至指定长度 (“Hello”).PadLeft(10)
PadRight() 右边填充空白是字符串至指定长度 (“Hello”).PadRight(10) + “World!”
Remove() 从指定位置开始移除指定长度 (“PsTips”).Remove(2,2)
Replace() 替换指定字符串 (“PsTips”).replace(“Ps”,”PS1″)
Split() 以指定分隔符切割字符串 (“HelloWorld”).Split(“l”)
StartsWith() 是否以指定子串开始 (“HelloWorld”).StartsWith(“He”)
Substring() 从指定位置取指定长度子串 “HelloWorld”).Substring(4,3)
ToCharArray() 转换成字符数组 (“HelloWorld”).toCharArray()
ToLower() 转换成小写 (“HelloWorld”).toLower()
ToLowerInvariant

 

()

以区域规则转换成小写 (“HelloWorld”).ToUpperInvariant()
ToUpper() 转换成大写 (“HelloWorld”).ToUpper()
ToUpperInvariant

 

()

以区域规则转换成大写 (“HelloWorld”).ToUpperInvariant

 

()

Trim() 移除字符串前后空格 (” HelloWorld “). Trim()
TrimEnd() 移除字符串结尾的空格 (“HelloWorld “). TrimEnd()
TrimStart() 移除字符串开始的空格 (” HelloWorld”). TrimStart()
Chars() 返回指定位置的字符 (“Hello”).Chars(0)

Split()为例来分析方法

在之前的章节中,我们已经知道可以通过Get-Member来查看一个对象中包含了那些可以被调用的方法。正好最为一个简单的回顾,来查看Split的定义。

PS C:\> ("Pstips.net" | Get-Member Split).definition 
string[] Split(Params char[] separator), string[] Split(char[] separator, int count), string[] Split(char[] separator, System.StringSplitOptions options), string[] Split(char[] separator, int count, System.StringSplitOptions options), string[] Split(string[] separator, System.StringSplitOptions options), string[] Split(string[] sepa 
rator, int count, System.StringSplitOptions options)

Define属性可以获取方法参数定义,但是可读性比较坑爹。我们仍然用上面表格中的Replace方法,将分隔符稍作替换,即可增强可读性。

PS C:\> ("Pstips.net" | Get-Member Split).definition.Replace("), ", ")`n")
string[] Split(Params char[] separator)
string[] Split(char[] separator, int count)
string[] Split(char[] separator, System.StringSplitOptions options)
string[] Split(char[] separator, int count, System.StringSplitOptions options)
string[] Split(string[] separator, System.StringSplitOptions options)
string[] Split(string[] separator, int count, System.StringSplitOptions options)

之前说过反引号,类似高级语言中的转义符反斜杠。

从上面的输出可以发现Split有6种不同的调用方法,而之前可能更多的只使用过一个参数的方法。PowerShell在处理文本时,可能会碰到多个分隔符,而Split方法调用只须一次即可。

PS C:\> "http://www.pstips.net".split(":./")
http

www
pstips
net

中间有空白,咋整,能移除吗,StringSplitOptions轻装上阵:

PS C:\> "http://www.pstips.net".split(":./",[StringSplitOptions]::RemoveEmptyEntries)
http
www
pstips
net

之前有一个小算法题,移除字符串中相邻的重复的空格。在不考虑效率的前提下,可以使用Split先分割,分割后再将得到的元素以指定分隔符拼接。但是拼接用到的Join方法,并不属于string对象,而属于String类,也正是下面要讲的。

http://www.pstips.net/string-object-methods.html

 
posted @ 2014-10-30 16:22  Qiengo  阅读(535)  评论(0编辑  收藏  举报