PowerShell 技能连载 - 显示 WPF 消息提示

WPF (Windows Presentation Foundation) 是一种创建窗体和对话框的技术。WPF 的好处是窗体设计和程序代码可以分离。

以下是一个显示醒目消息的例子。消息内容定义在 XAML 代码中,看起来类似 HTML (不过是区分大小写的)。您可以很容易地调整字体大小、文字、颜色等。不需要改任何程序代码:

$xaml = @"
<Window
 xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>

 <Border BorderThickness="20" BorderBrush="Yellow" CornerRadius="9" Background='Red'>
  <StackPanel>
   <Label FontSize="50" FontFamily='Stencil' Background='Red' Foreground='White' BorderThickness='0'>
    System will be rebooted in 15 minutes!
   </Label>

   <Label HorizontalAlignment="Center" FontSize="15" FontFamily='Consolas' Background='Red' Foreground='White' BorderThickness='0'>
    Worried about losing data? Talk to your friendly help desk representative and freely share your concerns!
   </Label>
  </StackPanel>
 </Border>
</Window>
"@

$reader = [System.XML.XMLReader]::Create([System.IO.StringReader] $xaml)
$window = [System.Windows.Markup.XAMLReader]::Load($reader)
$Window.AllowsTransparency = $True
$window.SizeToContent = 'WidthAndHeight'
$window.ResizeMode = 'NoResize'
$Window.Opacity = .7
$window.Topmost = $true
$window.WindowStartupLocation = 'CenterScreen'
$window.WindowStyle = 'None'
# show message for 5 seconds:
$null = $window.Show()
Start-Sleep -Seconds 5
$window.Close()

PowerShell 技能连载 - 通过 Outlook 发送电子邮件

您显然可以通过 Send-MailMessage 直接发送邮件。但如果您希望通过缺省的 MAPI 客户端发送电子邮件,也不会太麻烦:

$subject = 'Sending via MAPI client'
$body = 'My Message'
$to = 'tobias@powertheshell.com'

$mail = "mailto:$to&subject=$subject&body=$body"

Start-Process -FilePath $mail

这个脚本利用了 mailto: 语法。如果您已安装了一个 MAPI 客户端,这将打开一个电子邮件表单并且将脚本指定的内容填充进去。不过您需要手工发送邮件。

PowerShell 技能连载 - 查找缺省的 MAPI 客户端

您机器上的 MAPI 客户端就是处理类似“mailto:” URL 的缺省电子邮件客户端。我们设计一个函数来查找是否有 MAPI 客户端,如果有的话,查看具体是哪一个。该函数从 Windows 注册表中获取这项信息:

function Get-MAPIClient
{
    function Remove-Argument
    {
      param
      (
        $CommandLine
      )

      $divider = ' '
      if ($CommandLine.StartsWith('"'))
      {
        $divider = '"'
        $CommandLine = $CommandLine.SubString(1)
      }

      $CommandLine.Split($divider)[0]
    }

  $path = 'Registry::HKEY_CLASSES_ROOT\mailto\shell\open\command'

  # create new object to return values
  $returnValue = 1 | Select-Object -Property HasMapiClient, Path, MailTo

  $returnValue.hasMAPIClient = Test-Path -Path $path

  if ($returnValue.hasMAPIClient)
  {
    $values = Get-ItemProperty -Path $path
    $returnValue.MailTo = $values.'(default)'
    $returnValue.Path = Remove-Argument $returnValue.MailTo
    if ((Test-Path -Path $returnValue.Path) -eq $false)
    {
      $returnValue.hasMAPIClient = $true
    }
  }


  $returnValue
}

Get-MAPIClient

以下是使用结果:

PowerShell 技能连载 - 从命令行获取参数

在前一个技巧中,我们演示了如何从命令行中提取命令名,并忽略所有参数。今天,您将学习到如何用一个函数同时获取到命令名和参数。该函数将命令行分割为实际的命令名和它的参数,并返回一个自定义对象:

function Get-Argument
{
  param
  (
    $CommandLine
  )

  $result = 1 | Select-Object -Property Command, Argument

  if ($CommandLine.StartsWith('"'))
  {
    $index = $CommandLine.IndexOf('"', 1)
    if ($index -gt 0)
    {
      $result.Command = $CommandLine.SubString(0, $index).Trim('"')
      $result.Argument = $CommandLine.SubString($index+1).Trim()
      $result
    }
  }
  else
  {
    $index = $CommandLine.IndexOf(' ')
    if ($index -gt 0)
    {
      $result.Command = $CommandLine.SubString(0, $index)
      $result.Argument = $CommandLine.SubString($index+1).Trim()
      $result
    }
  }
}



Get-Argument -CommandLine 'notepad c:\test'
Get-Argument -CommandLine '"notepad.exe" c:\test'

结果如下:

这是一个实际应用中的例子:它获取所有运行中的进程,并返回每个进程的命令名和参数:

Get-WmiObject -Class Win32_Process |
  Where-Object { $_.CommandLine } |
  ForEach-Object {
    Get-Argument -CommandLine $_.CommandLine
  }

以下是结果的样子:

既然命令和参数都分开了,您还可以像这样为信息分组:

Get-WmiObject -Class Win32_Process |
  Where-Object { $_.CommandLine } |
  ForEach-Object {
    Get-Argument -CommandLine $_.CommandLine
  } |
  Group-Object -Property Command |
  Sort-Object -Property Count -Descending |
  Out-GridView

PowerShell 技能连载 - 从命令行中提取可执行程序名

有些时候我们需要从命令行提取命令名。以下是实现的方法:

代码如下:

function Remove-Argument
{
  param
  (
    $CommandLine
  )

  $divider = ' '
  if ($CommandLine.StartsWith('"'))
  {
    $divider = '"'
    $CommandLine = $CommandLine.SubString(1)
  }

  $CommandLine.Split($divider)[0]
}

PowerShell 技能连载 - 弹出对话框时播放随机的音效

您也许了解了如何用脚本打开一个 MsgBox 对话框。今天,您将学习如何用一段代码打开一个 MsgBox,同时播放一段随机的音效,吸引用户的注意力并增加趣味性。当用户操作 MsgBox 的时候,音效立即停止:

# find random WAV file in your Windows folder
$randomWAV = Get-ChildItem -Path C:\Windows\Media -Filter *.wav |
  Get-Random |
  Select-Object -ExpandProperty Fullname

# load Forms assembly to get a MsgBox dialog
Add-Type -AssemblyName System.Windows.Forms

# play random sound until MsgBox is closed
$player = New-Object Media.SoundPlayer $randomWAV
$player.Load();
$player.PlayLooping()
$result = [System.Windows.Forms.MessageBox]::Show("We will reboot your machine now. Ok?", "PowerShell", "YesNo", "Exclamation")
$player.Stop()

PowerShell 技能连载 - 通过 Google 图片搜索自动下载图片

在前一个技巧中您学到了如何用 Invoke-WebRequest 从 Google 图片搜索中获取图片链接。Invoke-WebRequest 还可以做更多的东西。它可以获取图片 URL 并下载图片。

以下是具体做法:

$SearchItem = 'PowerShell'
$TargetFolder = 'c:\webpictures'

if ( (Test-Path -Path $TargetFolder) -eq $false) { md $TargetFolder }

explorer.exe $TargetFolder

$url = "https://www.google.com/search?q=$SearchItem&espv=210&es_sm=93&source=lnms&tbm=isch&sa=X&tbm=isch&tbs=isz:lt%2Cislt:2mp"

$browserAgent = 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36'
$page = Invoke-WebRequest -Uri $url -UserAgent $browserAgent
$page.Links |
  Where-Object { $_.href -like '*imgres*' } |
  ForEach-Object { ($_.href -split 'imgurl=')[-1].Split('&')[0]} |
  ForEach-Object {
    $file = Split-Path -Path $_ -Leaf
    $path = Join-Path -Path $TargetFolder -ChildPath $file
    Invoke-WebRequest -Uri $_ -OutFile $path
  }

您可以下载所有匹配关键字“PowerShell”的高分辨率的图片到您指定的 $TargetFolder 文件夹中。

PowerShell 技能连载 - 从 Google 图片搜索中获取图片 URL

当您想从互联网下载信息时,Invoke-WebRequest 是您的好帮手。例如,您可以发送一个请求到 Google 并使用 PowerShell 检验它的结果。

Google 也知道您在这么做,所以当您从 PowerShell 发送一个查询时,Google 返回加密的链接。要获取真实的链接,您需要告诉 Google 您使用的不是 PowerShell 而是一个普通的浏览器。这可以通过设置浏览器代理字符串。

这段脚本输入一个关键字并返回所有符合搜索关键字,并且大于 2 兆像素的所有图片的原始地址:

$SearchItem = 'PowerShell'

$url = "https://www.google.com/search?q=$SearchItem&espv=210&es_sm=93&source=lnms&tbm=isch&sa=X&tbm=isch&tbs=isz:lt%2Cislt:2mp"
$browserAgent = 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36'
$page = Invoke-WebRequest -Uri $url -UserAgent $browserAgent
$page.Links |
  Where-Object { $_.href -like '*imgres*' } |
  ForEach-Object { ($_.href -split 'imgurl=')[-1].Split('&')[0]}

PowerShell 技能连载 - 查找注册的事件源

每个 Windows 日志文件都有一个注册的事件源列表。要找出哪个事件源注册到哪个事件日志,您可以直接查询 Windows 注册表。

这段代码将列出所有注册到“System”事件日志的事件源:

$LogName = 'System'
$path = "HKLM:\System\CurrentControlSet\services\eventlog\$LogName"
Get-ChildItem -Path $path -Name

PowerShell 技能连载 - 智能感知显示变量的技巧

在 PowerShell ISE 编辑器中,当您键入一个美元符号,将弹出一个智能感知菜单列出当前定义的所有变量。此时当您键入更多字符时,您不仅看见以这些字符开头的变量,而且还能看见在名字任意位置包含这些字符的变量。

要想只看到以您键入的字符开头的变量,请按下 ESC 键关闭智能感知菜单,然后按下 CTRL+SPACE 重新打开它。现在,它将只显示以您键入的字符开头的变量。