Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 有用的PowerShell一行程序_.net_Powershell_Scripting_Scripting Language - Fatal编程技术网

.net 有用的PowerShell一行程序

.net 有用的PowerShell一行程序,.net,powershell,scripting,scripting-language,.net,Powershell,Scripting,Scripting Language,请提供一行您认为有用的PowerShell脚本,每个答案一个脚本 有一个类似的,但这一个只提供链接到脚本页面,让答案一个接一个在这里,并有一个最常用或最有用的脚本的贡献列表 列出文件的最新版本 ls-r-fi*.lis | sort@{expression={$\.Name},@{expression={$\.LastWriteTime};Descending=$true}选择目录,名称,LastWriteTime |组对象名称|%{$\.Group |选择-first 1} gps程序对我不利

请提供一行您认为有用的PowerShell脚本,每个答案一个脚本

有一个类似的,但这一个只提供链接到脚本页面,让答案一个接一个在这里,并有一个最常用或最有用的脚本的贡献列表

  • 列出文件的最新版本

    ls-r-fi*.lis | sort@{expression={$\.Name},@{expression={$\.LastWriteTime};Descending=$true}选择目录,名称,LastWriteTime |组对象名称|%{$\.Group |选择-first 1}

  • gps程序对我不利|杀死

  • 打开带有注册程序的文件(如
    start
    例如
    start foo.xls

    ii foo.xls


  • 检索并显示系统特殊文件夹的路径

    dir | wus
    dir | wus "1/1/2009"
    
    [enum]::getvalues([system.environment+specialfolder])| foreach{“$”映射到“+[system.environment]::GetFolderPath($)}

  • 将环境值复制到剪贴板(这样现在您就知道如何使用剪贴板了!)

    $env:appdata |%{[windows.forms.clipboard]::SetText($input)}


    ls|剪辑

  • 带有管理单元

  • TFS中两个变更集编号之间的文件

    Get-TfsItemHistory-Recurse-Version~ |
    %{$(Get-TfsChangeset$\ u0.ChangeSetID).Changes}|
    %{$\ Item.ServerItem}|排序对象-唯一


  • 获取exchange 200中所有集线器服务器上出现错误的队列消息

    Get-ExchangeServer |?{$|.IsHubTransportServer-eq$true}Get-Queue
    |?{$\.LastError-ne$null}排序对象-降序-属性MessageCount
    |ft-Property-NextHopDomain,{l=“Count”;e={$\uu.MessageCount},{l=“Last Try”e={$\uu.lastretytime.tosting(“M/dd hh:mm”)},@{l=“Retry”;e={$\uu.NextRetryTime.tostring(“M/dd hh:mm”)},状态,LastError-AutoSize


  • 我最喜欢的powershell一号客轮

    gps programThatIsAnnoyingMe | kill
    

    好吧,这里有一个我经常使用的方法,并附有一些解释

    二,。

    ii是调用项的别名。此commandlet实质上调用在windows中为以下项注册的任何命令。因此:

    ii foo.xls

    将在Excel中打开
    foo.xls
    (假设已安装Excel并且.xls文件与Excel关联)


    ii.
    中,
    引用当前工作目录,因此该命令将导致windows资源管理器在当前目录下打开。

    列出我今天更新的所有文件:

    ii . 经常使用它,我实际上在我的个人资料中创建了一个小功能:

    ii foo.xls 所以我可以说:

    dir | ?{$_.LastWriteTime -ge [DateTime]::Today}
    
    要查看今天之前更新的内容,请执行以下操作:

    function Where-UpdatedSince{
    Param([DateTime]$date = [DateTime]::Today,
          [switch]$before=$False)
    Process
    { 
        if (($_.LastWriteTime -ge $date) -xor $before)
        {
            Write-Output $_
        }
    } 
    };  set-item -path alias:wus -value Where-UpdatedSince
    

    检索并显示系统特殊文件夹的路径

    dir | wus
    dir | wus "1/1/2009"
    

    我最喜欢的:这是一个slashdot阅读器,没有kdawson先生提交的可怕的意见书。它被设计为少于120个字符,允许在/

    上用作签名。这显示了哪些进程正在使用哪些版本的MS CRT DLL:

    [enum]::getvalues([system.environment+specialfolder]) | foreach {"$_ maps to " + [system.Environment]::GetFolderPath($_)}
    

    这可能是作弊,因为我安装了TFS PowerTools snap,但这对于找出在两个变更集、版本或标签之间更改了哪些文件非常有用

    ($x=new-object xml).Load("http://rss.slashdot.org/Slashdot/slashdot");$x.RDF.item|?{$_.creator-ne"kdawson"}|fl descr*
    
    Get-TfsItemHistory-Recurse-Version~ |
    %{$(Get-TfsChangeset$\ u0.ChangeSetID).Changes}|
    %{$\ Item.ServerItem}|排序对象-唯一
    
    我发现显示环境变量的值很有用

    gps | select ProcessName -exp Modules -ea 0 | 
      where {$_.modulename -match 'msvc'} | sort ModuleName | 
      Format-Table ProcessName -GroupBy ModuleName
    
    您还可以将环境值复制到剪贴板

    Get-TfsItemHistory <location> -Recurse -Version <label1>~<label2> | 
    % { $(Get-TfsChangeset $_.ChangeSetID).Changes } |
    % { $_.Item.ServerItem } | Sort-Object -Unique
    
    get-winevent -listprovider microsoft-windows-Securit* | % {$_.Name} | sort
    

    (您需要在调用之前加载windows.forms:添加类型–a system.windows.forms;并使用-STA开关运行PowerShell)

    获取exchange 2007中所有集线器服务器上出现错误的队列消息(使用某些格式)

    下午6点左右

    $env:appdata | % { [windows.forms.clipboard]::SetText($input) }
    

    函数显示的系统正常运行时间 我用这个做我的会计电子表格

    Get-ExchangeServer | ?{$_.IsHubTransportServer -eq $true} | Get-Queue | ?{$_.LastError -ne $null} | Sort-Object -Descending -Property MessageCount | ft -Property NextHopDomain,@{l="Count";e={$_.MessageCount}},@{l="Last Try";e={$_.LastRetryTime.tosting("M/dd hh:mm")}},@{l="Retry";e={$_.NextRetryTime.tostring("M/dd hh:mm")}},Status,LastError -AutoSize        
    

    我不喜欢复杂的应用程序来计算代码行,尤其是因为我认为它是一个虚假的度量标准。我最终使用的是PS one liner:

    exit
    

    我只需要将要包含在行计数中的文件的扩展名包括在内,然后从项目的根目录中获取扩展名。

    将一些文件复制到桌面:

        function get-uptime
    {
    $PCounter = "System.Diagnostics.PerformanceCounter"
    $counter = new-object $PCounter System,"System Up Time"
    $value = $counter.NextValue()
    $uptime = [System.TimeSpan]::FromSeconds($counter.NextValue())
    "Uptime: $uptime"
    "System Boot: " + ((get-date) - $uptime)
    }
    

    按字母顺序列出所有Windows提供程序:

    PS C:\Path> (dir -include *.cs,*.xaml -recurse | select-string .).Count
    
    实际上,您可以对任何特定的提供者组使用通配符:

    Copy-Item $home\*.txt ([Environment]::GetFolderPath("Desktop"))
    

    抑制Visual Studio 2012所有CAPS菜单-安装VS2012后我要做的第一件事

    get-winevent -listprovider microsoft-windows* | % {$_.Name} | sort
    

    多亏了发现注册表值的人。

    通过管道将某些内容输出到剪贴板

    Get-TfsItemHistory <location> -Recurse -Version <label1>~<label2> | 
    % { $(Get-TfsChangeset $_.ChangeSetID).Changes } |
    % { $_.Item.ServerItem } | Sort-Object -Unique
    
    get-winevent -listprovider microsoft-windows-Securit* | % {$_.Name} | sort
    

    在文件中生成一些伪随机字节

    Set-ItemProperty -Path HKCU:\Software\Microsoft\VisualStudio\11.0\General -Name SuppressUppercaseConversion -Type DWord -Value 1
    

    Get Random算法从系统时钟中提取一个种子,因此不要将其用于严重的加密需求。

    我不太愿意逐一列举我的PowerShell一行程序列表,因为列表编号目前只有大约400个条目!:-)但以下是我的一些最爱,激起你的兴趣:

    • 列出所有类型的加速器(需要):
      [加速器]::get
    • 将XML的字符串表示形式转换为实际的XML:
      [XML]”:
      ls.|选择名称、长度| Out ConsoleGraph-prop length-grid
    整个收藏集在Simple-Talk.com上发布,共分为四个部分。我希望这些内容对SO读者有用

    • 第1部分:
    • 第2部分:
    • 第3部分:
    • 第4部分:
    我想将该系列称为“在PowerShell的一行中做任何事情”,但我的编辑想要更简洁的东西,所以我们选择了PowerShell一行。尽管为了充分披露,只有98%左右是真正符合术语精神的一行;我认为四舍五入就足够了……:-)


    在我每次尝试命令后,去掉所有难以处理的、冗长的红色标记,让我以一个漂亮的、漂亮的、清晰的屏幕继续。投票应该是社区维基,通常不属于社区维基。@EBGreen:通常不属于,他们不属于。我认为这一个很好
    [Byte[]]$out=@(); 0..9 | %{$out += Get-Random -Minimum 0 -Maximum 255}; [System.IO.File]::WriteAllBytes("random",$out)
    
    cls