Powershell点滴

1. 如何表达枚举类型?
[StringSplitOptions] $option = "None"
[StringSplitOptions] $option = "RemoveEmptyEntries"
 
2. 在bat中调用执行Powershell文件(相对路径)
powershell.exe -Command "& {.\Script1.ps1}"
 
3. 加入-NoExit参数执行ps1文件
PowerShell.exe -NoExit "& 'C:\Program Files\...\sharepoint.ps1'"
 
4. 通用参数-confirm -ErrorAction
-Confirm $false
-ErrorAction "SilentlyContinue"
 
5. 创建对象并传递参数
$date = New-Object -TypeName System.DateTime -ArgumentList @(1882,7,4,0,0,0)
 
6. 创建COM对象
$ie = New-Object -ComObject "InternetExplorer.Application"
 
7. 执行Powershell脚本文件并传入命名参数
#powershell -noexit -file "C:\Users\Terry\MyPSScripts\test01.ps1" -par1 "hello world" -par2 great
在文件中有对参数的定义:param($par1, $par2)
 
8. 获取给定路径的父目录
split-path “c:\test\error.log” #会返回 c:\test, 等价于 split-path “c:\test\error.log” –Parent
split-path “c:\test\error.log” -leaf #会返回 error.log
 
9. 得到脚本文件所在的目录
$MyInvocation.MyCommand.Path 比较好用
$MyInvocation.InvocationName 不可靠,它可以是”&”也可以是实际的脚本全路径

posted @ 2012-01-11 09:07  队长  阅读(315)  评论(0编辑  收藏  举报