PowerShell 技能连载 - 在 ISE 编辑器中打开文件

如果您想在 ISE 编辑器中打开一个脚本,一个快捷的方法是使用命令“ise”。例如要打开您的配置脚本(每次 ISE 启动时自动调用的脚本),请试试一下代码:

PS> ise $profile

您现在可以方便地增加或删除您希望 ISE 每次启动时自动执行的命令。

如果您的配置脚本不存在,以下单行的代码可以为您创建一个(它将覆盖已经存在的文件,所以只能在文件确实还不存在的时候运行它):

PS> New-Item -Path $profile -ItemType File -Force

您也可以从一个 PowerShell.exe 控制台中使用“ise”命令来启动一个 ISE 编辑器。

(顺便提一下:有一个叫做 psEdit 的函数使用效果十分相似,但是只在 PowerShell ISE 内部有效,在 PowerShell.exe 控制台中无效。)

PowerShell 技能连载 - 在 PowerShell ISE 中使用块注释

从 PowerShell 3.0 开始,您可以按住 ALT 键并选择一些内容,来获取一个矩形选区。

如果您尽可能地缩窄这个选区(您将只看到一条细细的蓝线),您可以方便地在选区之内增加或删除字符。只需要按下“#”键即可块注释它们,或者删除 # 号重新启用这段代码。

PowerShell 技能连载 - 查找远程计算机上已登录的用户

在上一个技巧当中我们使用 quser.exe 来查询本机当前登录的用户。以下是一个支持查询远程计算机上已登录用户的函数。有个额外的好处是,返回的信息附加了一个名为“ComputerName”的属性,所以当您查询多台计算机时,您将可以知道结果是属于那一台计算机的:

function Get-LoggedOnUser
{
  param([String[]]$ComputerName = $env:COMPUTERNAME)

    $ComputerName | ForEach-Object {
      (quser /SERVER:$_) -replace '\s{2,}', ',' |
        ConvertFrom-CSV |
        Add-Member -MemberType NoteProperty -Name ComputerName -Value $_ -PassThru
  }
}

以下是一个调用的例子,查询本地计算机以及一台远程计算机:

PowerShell 技能连载 - 查询已登录的用户

有一个十分有用的控制台程序叫做 quser.exe 可以告诉您哪些用户登录到了一台机器上。该可执行程序返回的是纯文本,但通过一点点正则表达式,该文本可以转换成 CSV 并导入 PowerShell。

以下代码以对象的形式返回所有当前登录到您机器上的用户信息:

(quser) -replace 's{2,}', ',' | ConvertFrom-Csv

PowerShell 技能连载 - 查询登录失败记录

只要有人使用错误的凭据登录,就会在安全日志中产生一条日志记录。以下是一个可以从安全日志中读取这些事件的函数(需要管理员特权)。它能够列出所有日志中非法的登录信息:

# requires Admin privileges!
function Get-LogonFailure
{
      param($ComputerName)
      try
      {
          Get-EventLog -LogName security -EntryType FailureAudit -InstanceId 4625 -ErrorAction Stop @PSBoundParameters |
                  ForEach-Object {
                    $domain, $user = $_.ReplacementStrings[5,6]
                    $time = $_.TimeGenerated
                    "Logon Failure: $domain\$user at $time"
                }
      }
      catch
      {
            if ($_.CategoryInfo.Category -eq 'ObjectNotFound')
            {
                  Write-Host "No logon failures found." -ForegroundColor Green
            }
            else
            {
                  Write-Warning "Error occured: $_"
            }

      }

}

请注意这个函数还可以在远程主机上运行。请使用 -ComputerName 参数来查询一台远程主机。远程主机需要运行 RemoteRegistry 服务,并且您需要在目标机器上的本地管理员权限。

PowerShell 技能连载 - 更改桌面背景

PowerShell 可以通过调用 Windows API,实现更改当前桌面背景并且立即生效。以下函数实现立刻更换桌面背景:

function Set-Wallpaper
{
    param(
        [Parameter(Mandatory=$true)]
        $Path,

        [ValidateSet('Center', 'Stretch')]
        $Style = 'Stretch'
    )

    Add-Type @"
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace Wallpaper
{
public enum Style : int
{
Center, Stretch
}
public class Setter {
public const int SetDesktopWallpaper = 20;
public const int UpdateIniFile = 0x01;
public const int SendWinIniChange = 0x02;
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
public static void SetWallpaper ( string path, Wallpaper.Style style ) {
SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
switch( style )
{
case Style.Stretch :
key.SetValue(@"WallpaperStyle", "2") ;
key.SetValue(@"TileWallpaper", "0") ;
break;
case Style.Center :
key.SetValue(@"WallpaperStyle", "1") ;
key.SetValue(@"TileWallpaper", "0") ;
break;
}
key.Close();
}
}
}
"@

    [Wallpaper.Setter]::SetWallpaper( $Path, $Style )
}

Set-Wallpaper -Path 'C:\Windows\Web\Wallpaper\Characters\img24.jpg'

PowerShell 技能连载 - 在 ISE 编辑器中使用块注释

在 PowerShell 3.0 中,引入了一个小技巧,可以整块注释/取消注释某一段代码。

在 ISE 编辑器中,单击插入点(比如说一段代码的开头)。然后,按住 SHIFT+ALT,然后按下 键。

这时在 ISE 编辑器中将显示一条细细的绿线。当您高亮选中了这个块以后,在绿线消失前,按下 #(或任何您打算放在标记的行前的字符)。类似地,您也可以整块删除字符。

PowerShell 技能连载 - 获取远程主机的系统信息

在上一个技巧当中您已学到如何用 systeminfo.exe 获取丰富的系统资料。systeminfo.exe 内置了远程的功能,所以如果您拥有了适当的权限,您可以获取远程主机的系统信息。

以下是一个简单的函数:

function Get-SystemInfo
{
  param($ComputerName = $env:ComputerName)

      $header = 'Hostname','OSName','OSVersion','OSManufacturer','OSConfig','Buildtype',`'RegisteredOwner','RegisteredOrganization','ProductID','InstallDate','StartTime','Manufacturer',`'Model','Type','Processor','BIOSVersion','WindowsFolder','SystemFolder','StartDevice','Culture',`'UICulture','TimeZone','PhysicalMemory','AvailablePhysicalMemory','MaxVirtualMemory',`'AvailableVirtualMemory','UsedVirtualMemory','PagingFile','Domain','LogonServer','Hotfix',`'NetworkAdapter'
      systeminfo.exe /FO CSV /S $ComputerName |
            Select-Object -Skip 1 |
            ConvertFrom-CSV -Header $header
}

以下是简单的调用示例:

PowerShell 技能连载 - 获取系统信息

PowerShell 和现有的控制台程序可以很好地共存。一个最有用的是 systeminfo.exe,它可以收集各种有用的系统信息。通过导入 systeminfo.exe 提供的 CSV 信息,PowerShell 可以将文本信息转化为对象:

$header = 'Hostname','OSName','OSVersion','OSManufacturer','OSConfig','Buildtype',`'RegisteredOwner','RegisteredOrganization','ProductID','InstallDate','StartTime','Manufacturer',`'Model','Type','Processor','BIOSVersion','WindowsFolder','SystemFolder','StartDevice','Culture',`'UICulture','TimeZone','PhysicalMemory','AvailablePhysicalMemory','MaxVirtualMemory',`'AvailableVirtualMemory','UsedVirtualMemory','PagingFile','Domain','LogonServer','Hotfix',`'NetworkAdapter'

systeminfo.exe /FO CSV |   Select-Object -Skip 1 |   ConvertFrom-CSV -Header $header

当您运行这段代码时,它将停顿数秒钟,以供 systeminfo.exe 收集信息。然后,您将会获得大量的信息:

请注意 $header:这个变量定义了属性名称,并且用自定义的列表替换了缺省的表头。所以,无论操作系统是哪种语言的,这些表头永远是相同的。

您还可以将这些信息存储在一个变量中,然后分别存取其中的信息:

$header = 'Hostname','OSName','OSVersion','OSManufacturer','OSConfig','Buildtype',`'RegisteredOwner','RegisteredOrganization','ProductID','InstallDate','StartTime','Manufacturer',`'Model','Type','Processor','BIOSVersion','WindowsFolder','SystemFolder','StartDevice','Culture',`'UICulture','TimeZone','PhysicalMemory','AvailablePhysicalMemory','MaxVirtualMemory',`'AvailableVirtualMemory','UsedVirtualMemory','PagingFile','Domain','LogonServer','Hotfix',`'NetworkAdapter'

$result = systeminfo.exe /FO CSV |
  Select-Object -Skip 1 |
  ConvertFrom-CSV -Header $header

PowerShell 技能连载 - 远程启动服务

由于 Start-Service 命令没有 -ComputerName 参数,所以您无法简单地远程启动一个服务。然而您可以在一个 PowerShell 远程管理会话中运行 Start-Service 命令。在某些场景下,一个更简单的方法是使用 Set-Service 命令。以下代码可以在名为 Server12 的服务器上远程启动 Spooler 服务:

Set-Service -Name Spooler -Status Running -ComputerName Server12

不幸的是,这个命令没有 -Force 开关。所以虽然您可以简单地启动服务,但您可能无法用这种方式停止它们。当一个服务依赖于另一个服务时,它必须使用“强制”的方式来停止。