Windows PowerShell已经推出相当长的时间了。与好的旧窗户外壳相比,它的威力要大得多。作为一名开发人员,您是否使用任何脚本来加速和简化您每天的工作?如果你能用PowerShell ->做魔法,请和我们分享!
更新并不是真正的脚本,但PowerShell社区扩展也非常有用。这个包包含了许多新的Cmdlet和PowerShell修改。
发布于 2009-07-10 17:05:25
我在命令行中组装了一堆脚本来使用Subversion。它们中的大多数只是使用-xml选项将各种信息以对象形式放置。以下是几个例子:
function Get-SvnStatus( [string[]] $Path = ".",
[string] $Filter = "^(?!unversioned|normal|external)",
[switch] $NoFormat )
{
# powershell chokes on "wc-status" and doesn't like two definitions of "item"
[xml]$status = ( ( Invoke-Expression "svn status $( $Path -join ',' ) --xml" ) -replace "wc-status", "svnstatus" ) `
-replace "item=", "itemstatus="
$statusObjects = $status.status.target | Foreach-Object { $_.entry } | Where-Object {
$_.svnstatus.itemstatus -match $Filter
} | Foreach-Object {
$_ | Select-Object @{ Name = "Status"; Expression = { $_.svnstatus.itemstatus } },
@{ Name = "Path"; Expression = { Join-Path ( Get-Location ) $_.path } }
} | Sort-Object Status, Path
if ( $NoFormat )
{
$statusObjects
}
else
{
$statusObjects | Format-Table -AutoSize
}
}
function Get-SvnLog( [string] $Path = ".",
[int] $Revision,
[int] $Limit = -1,
[switch] $Verbose,
[switch] $NoFormat )
{
$revisionString = ""
$limitString = ""
$verboseString = ""
if ( $Revision )
{
$revisionString = "--revision $Revision"
}
if ( $Limit -ne -1 )
{
$limitString = "--limit $Limit"
}
if ( $Verbose )
{
$verboseString = "--verbose"
}
[xml]$log = Invoke-Expression "svn log $( $path -join ',' ) --xml $revisionString $limitString $verboseString"
$logObjects = $log.log.logentry | Foreach-Object {
$logEntry = $_
$logEntry | Select-Object `
@{ Name = "Revision"; Expression = { [int]$logEntry.revision } },
@{ Name = "Author"; Expression = { $logEntry.author } },
@{ Name = "Date";
Expression = {
if ( $NoFormat )
{
[datetime]$logEntry.date
}
else
{
"{0:dd/MM/yyyy hh:mm:ss}" -f [datetime]$logEntry.date
}
} },
@{ Name = "Message"; Expression = { $logEntry.msg } } |
Foreach-Object {
# add the changed path information if the $Verbose parameter has been specified
if ( $Verbose )
{
$_ | Select-Object Revision, Author, Date, Message,
@{ Name = "ChangedPaths";
Expression = {
$paths = $logEntry.paths.path | Foreach-Object {
$_ | Select-Object `
@{ Name = "Change";
Expression = {
switch ( $_.action )
{
"A" { "added" }
"D" { "deleted" }
"M" { "modified" }
"R" { "replaced" }
default { $_.action }
}
} },
@{ Name = "Path"; Expression = { $_."#text" } }
}
if ( $NoFormat )
{
$paths
}
else
{
( $paths | Sort-Object Change | Format-Table -AutoSize | Out-String ).Trim()
}
}
}
}
else
{
$_
}
}
}
if ( $NoFormat )
{
$logObjects
}
else
{
$logObjects | Format-List
}
}我把这些分别命名为svns和svnl。我说的是其他几个这里。
发布于 2009-07-10 14:45:20
我一直使用它,因为Windows Explorer对文件内容的搜索从来不适合我:
Get-ChildItem -Recurse -Filter *.extension |
Select-String -List somestring |
Format-Table filename,linenumber -AutoSize只需将“扩展名”替换为您感兴趣的文件类型的文件扩展名(或完全删除-Filter参数),并将"somestring“替换为您希望在文件中找到的文本。
发布于 2009-07-13 03:57:11
这不是一个脚本,但一般来说,它是有帮助的学习什么时候你可以简短的参数,包括名称和位置。
从名称上讲,PowerShell只需要将其缩小到一个。例如,gci -r可以工作,但gci -f可能是-filter或-force。
在没有参数标签的情况下指定的值是按位置应用的。因此,如果要指定-filter,可以这样做:
gci -r -fil *.cs或者以-path的形式提供-path,以便您也可以按位置指定-filter:
gci -r . *.cshttps://stackoverflow.com/questions/1109766
复制相似问题