您最喜欢/有用的 powershell 脚本

您最喜欢/有用的 powershell 脚本

与关于 autohotkey 脚本的问题类似:https://superuser.com/questions/7271/,刚才讲的是Powershell。

任何有用、方便或强大的脚本。请描述它们的用途。请随意添加与服务器作业或系统管理员任务无关的脚本。

例如:

此命令将递归地将您的 *.jpg 图像移动到当前目录中,并添加最初包含的目录名称作为图像文件的前缀。

(ls -r -include *.jpg) | % { mv -literal $_ $_.Name.Insert(0, [String]::Format("{0} - ", $_.Directory.Name))}

或者你可以在文本文档上运行一些内容:

$badString = "This        is         not        the way   it   should     be."
$badString #for debug only
while ($badString.Contains("  ")){
    $badString = $badString -replace "  "," "
    }
$badString #tada

因为 Powershell 代表着未来。

答案1

Get-RecentUpdates.ps1,这将列出来自 Microsoft Update 的最近(默认值:最近 7 天)更新(这项工作仍在进行中,一些格式应该移至.xmlps1格式定义):

#requires -Version 2.0
# Copyright Richard J Cox 2009. Use freely at your own risk

param([switch]$RawDisplay,
      [DateTime]$After = $([datetime]::Today.AddDays(-7)),
      [string]$Computer = '')

$extraArgs = @{}
if ($Computer.Length -gt 0) {
    $extraArgs.Computer = $Computer
}

$events = get-eventlog -After $after -logname system -InstanceId 19 -source "Microsoft-Windows-WindowsUpdateClient" @extraArgs | 
          select-Object -property EventId, Index, Source, TimeGenerated,
                        @{n='Message';e={$_.ReplacementStrings | Select-Object -first 1}}


if ($rawDisplay) {
    $events
} else {
    $events | ft -a -wrap Index, TimeGenerated, Message
}

答案2

我的最爱从这里... 以真实形式获取 Windows 序列密钥!..

function Get-WindowsKey {
    ## function to retrieve the Windows Product Key from any PC
    ## by Jakob Bindslet ([email protected])
    param ($targets = ".")
    $hklm = 2147483650
    $regPath = "Software\Microsoft\Windows NT\CurrentVersion"
    $regValue = "DigitalProductId"
    Foreach ($target in $targets) {
        $productKey = $null
        $win32os = $null
        $wmi = [WMIClass]"\\$target\root\default:stdRegProv"
        $data = $wmi.GetBinaryValue($hklm,$regPath,$regValue)
        $binArray = ($data.uValue)[52..66]
        $charsArray = "B","C","D","F","G","H","J","K","M","P","Q","R","T","V","W","X","Y","2","3","4","6","7","8","9"
        ## decrypt base24 encoded binary data
        For ($i = 24; $i -ge 0; $i--) {
            $k = 0
            For ($j = 14; $j -ge 0; $j--) {
                $k = $k * 256 -bxor $binArray[$j]
                $binArray[$j] = [math]::truncate($k / 24)
                $k = $k % 24
            }
            $productKey = $charsArray[$k] + $productKey
            If (($i % 5 -eq 0) -and ($i -ne 0)) {
                $productKey = "-" + $productKey
            }
        }
        $win32os = Get-WmiObject Win32_OperatingSystem -computer $target
        $obj = New-Object Object
        $obj | Add-Member Noteproperty Computer -value $target
        $obj | Add-Member Noteproperty Caption -value $win32os.Caption
        $obj | Add-Member Noteproperty CSDVersion -value $win32os.CSDVersion
        $obj | Add-Member Noteproperty OSArch -value $win32os.OSArchitecture
        $obj | Add-Member Noteproperty BuildNumber -value $win32os.BuildNumber
        $obj | Add-Member Noteproperty RegisteredTo -value $win32os.RegisteredUser
        $obj | Add-Member Noteproperty ProductID -value $win32os.SerialNumber
        $obj | Add-Member Noteproperty ProductKey -value $productkey
        $obj
    }
}

然后使用 -

获取WindowsKey

甚至 -

获取 WindowsKey“pc1”、“pc2”、“server999”、“server777”

答案3

我的配置文件脚本 - 包含一堆有用脚本的共享目录:

$SharedScripts = '\\FileServer\Share\PowerShell\Scripts' 
Get-ChildItem "$SharedScripts\*.ps1" | % {  
  . $_ 
  Write-Host ('Loaded shared library: ' + [System.IO.Path]::GetFileNameWithoutExtension($_)) 
}

相关内容