PowerShell 技能连载 - 获取最新的地震信息
现代社会中,所有东西都是彼此相连的。PowerShell 可以从 web service 中获取公共数据。以下仅仅一行代码就可以为您获取最新检测到的地震以及它们的震级:
Invoke-RestMethod -URI "http://www.seismi.org/api/eqs" |
Select-Object -First 30 -ExpandProperty Earthquakes |
Out-GridView
现代社会中,所有东西都是彼此相连的。PowerShell 可以从 web service 中获取公共数据。以下仅仅一行代码就可以为您获取最新检测到的地震以及它们的震级:
Invoke-RestMethod -URI "http://www.seismi.org/api/eqs" |
Select-Object -First 30 -ExpandProperty Earthquakes |
Out-GridView
Get-EventLog
命令每次只能读取一个事件日志。然而如果您希望从多个事件日志中读取事件,您可以传入数组信息:
$events = @(Get-EventLog -LogName System -EntryType Error)
$events += Get-EventLog -LogName Application -EntryType Error
$events
在这些例子中,使用 WMI 来查询可能会更简单一些——它可以一次性查询任意多个系统日志。
以下代码将从应用程序和系统日志中获取前 100 条错误日志(指的是总计 100 条,所以如果前 100 条错误都是应用程序日志,则当然不会包括系统错误):
Get-WmiObject -Class Win32_NTLogEvent -Filter 'Type="Error" and (LogFile="System" or LogFile="Application")' |
Select-Object -First 100 -Property TimeGenerated, LogFile, EventCode, Message
当您将 Get-WmiObject
换成 Get-CimInstance
(PowerShell 3.0 新增的命令),那么诡异的 WMI 日期格式将会被自动转换为普通的日期和时间:
Get-CimInstance -Class Win32_NTLogEvent -Filter 'Type="Error" and (LogFile="System" or LogFile="Application")' |
Select-Object -First 100 -Property TimeGenerated, LogFile, EventCode, Message
有序哈希表是在 PowerShell 3.0 中增加的新特性,它在创建新对象的时候十分有用。和常规的哈希表不同,有序哈希表保存了您添加键时的顺序,您还可以控制这些键转化为对象属性时的顺序。以下是一个例子:
$hashtable = [Ordered]@{}
$hashtable.Name = 'Tobias'
$hashtable.ID = 12
$hashtable.Location = 'Germany'
New-Object -TypeName PSObject -Property $hashtable
这段代码创建了一个对象,它的属性定义严格按照它们指定时的先后顺序排列。
那么如果您希望不在尾部,例如在列表的头部增加一个属性,要怎么做呢?
$hashtable = [Ordered]@{}
$hashtable.Name = 'Tobias'
$hashtable.ID = 12
$hashtable.Location = 'Germany'
$hashtable.Insert(0, 'Position', 'CSA')
New-Object -TypeName PSObject -Property $hashtable
当您了解了每个 DateTime
对象支持 Add...()
方法之后,获取相对日期(例如昨天或下周)就十分容易了。以下代码可以获取昨天的时间值:
$today = Get-Date
$yesterday = $today.AddDays(-1)
$yesterday
$yesterday
的值是当前时间之前 24 小时整的值。那么如果您希望得到昨天特定时刻的值,比如说昨天午夜呢?
如果您希望得到今天午夜的值,那么十分简单:
$todayMidnight = Get-Date -Hour 0 -Minute 0 -Second 0
$todayMidnight
如果您希望得到另一天的该时间值,那么再次使用 Get-Date 来修改时间值。以代码获取昨天午夜的时间值:
$today = Get-Date
$yesterday = $today.AddDays(-1)
$yesterday | Get-Date -Hour 0 -Minute 0 -Second 0
译者注:如果您只是需要获取昨天午夜的日期值,还可以有其它方法。如:
(Get-Date).AddDays(-1).Date
或[System.DateTime]::Today.Subtract([System.TimeSpan]::FromDays(1))
。
PowerShell 是令人惊叹的。它可以根据您选择的关键词搜索 YouTube 视频,然后为您呈现视频,以及根据选择播放视频。
以下这段简单的脚本(需要 Internet 连接)可以列出 YouTube 上最新的“Learn PowerShell”视频。该列表使用一个 Grid View 窗口呈现,您可以在顶部使用全文搜索或者按列排序,来查找您需要的视频。
下一步,单击视频项选中它,然后单击网格右下角的“确定”按钮。
PowerShell 将会启动您的 Web 浏览器并且播放视频。太棒了!
$keyword = "Learn PowerShell"
Invoke-RestMethod -Uri "https://gdata.youtube.com/feeds/api/videos?v=2&q=$($keyword.Replace(' ','+'))" |
Select-Object -Property Title, @{N='Author';E={$_.Author.Name}}, @{N='Link';E={$_.Content.src}}, @{N='Updated';E={[DateTime]$_.Updated}} |
Sort-Object -Property Updated -Descending |
Out-GridView -Title "Select your '$Keyword' video, then click OK to view." -PassThru |
ForEach-Object { Start-Process $_.Link }
只需要改变第一行的 $keyword
变量就可以搜索不同的视频或者主题。
请注意由于 PowerShell 3.0 的一个 bug,Invoke-RestMethod
只会返回一部分结果。PowerShell 4.0 修复了这个 bug。
译者注:由于国内暂时不可直接访问 YouTube 服务,验证本脚本需要合适的代理服务器或 VPN。
Windows 8.1 发布了一个称为“Defender”的新模块。内置的 cmdlet 使您能够管理、查看和修改 Windows Defender 反病毒程序的每一个方面。
要列出所有可用的 cmdlet,请使用以下代码:
Get-Command -Module Defender
如果您没有获得任何返回信息,那么您正在运行的很可能不是 Windows 8.1,所以该模块不可用。
下一步,试着浏览这些 cmdlet。例如 Get-MpPreference
,将列出当前所有偏好设置。类似地,Set-MpPreference
可以改变它们的值。
Get-MpThreatDetection
将会列出当前检测到的所有威胁(如果当前没有任何威胁,则返回空)。
您知道吗?您可以搜索计算机上的本地用户,就像搜索域账户一样。
以下的示例代码搜索所有以“A”开头并且是启用状态的本地用户:
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$type = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext('Machine', $env:COMPUTERNAME)
$UserPrincipal = New-Object System.DirectoryServices.AccountManagement.UserPrincipal($type)
# adjust your search criteria here:
$UserPrincipal.Name = 'A*'
# you can add even more:
$UserPrincipal.Enabled = $true
$searcher = New-Object System.DirectoryServices.AccountManagement.PrincipalSearcher
$searcher.QueryFilter = $UserPrincipal
$results = $searcher.FindAll();
$results | Select-Object -Property Name, LastLogon, Enabled
类似地,要查找所有设置了密码、密码永不过期,并且是启用状态的本地用户,试试以下代码:
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$type = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext('Machine', $env:COMPUTERNAME)
$UserPrincipal = New-Object System.DirectoryServices.AccountManagement.UserPrincipal($type)
# adjust your search criteria here:
$UserPrincipal.PasswordNeverExpires = $true
$UserPrincipal.Enabled = $true
$searcher = New-Object System.DirectoryServices.AccountManagement.PrincipalSearcher
$searcher.QueryFilter = $UserPrincipal
$results = $searcher.FindAll();
$results | Select-Object -Property Name, LastLogon, Enabled, PasswordNeverExpires
在 PowerShell 中,通过 .NET Framework 3.51 或更高的版本,可以使用面向对象的方式管理本地用户和组。以下代码可以列出本机上的管理员用户:
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$type = New-Object DirectoryServices.AccountManagement.PrincipalContext('Machine', `$env:COMPUTERNAME)
$group = [DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity($type, `'SAMAccountName', 'Administrators')
$group.Members | Select-Object -Property SAMAccountName, LastPasswordSet, LastLogon, Enabled
您还可以获取更多的信息,比如试着查询组本身的信息:
或者试着列出所有成员的所有属性:
Windows 8.1 仍然在它的一些上下文菜单中提供旧的 cmd.exe 命令行窗口。在 Windows 8.1 中,要将它由 cmd.exe 改为 powershell.exe,请右键单击任务栏,然后选择属性。
然后,单击“导航”标签页,然后选中第三个选项。
下一次,当您在 Windows 8.1 中按下 WIN+X
键时,迷你菜单上将显示“PowerShell”。
您知道吗?您也可以用 PowerShell 管理您的 Office365 账户。如果您拥有一个 Office365 账户,请试试以下脚本:
$OfficeSession = New-PSSession -ConfigurationName Microsoft.Exchange `-ConnectionUri https://ps.outlook.com/powershell/ -Credential (Get-Credential) `-Authentication Basic -AllowRedirection
$import = Import-PSSession $OfficeSession -Prefix Off365
Get-Command -Noun Off365*
这段代码将使用您的凭据连接 Office 365,然后导入用于管理 Office 365 的 PowerShell cmdlet。您大约可以获得 400 个新的 cmdlet。如果您收到“Access Denied”提示,那么有可能您的账户没有足够的权限,或者您敲错了密码。
注意所有导入的 cmdlet 都是以 Off365
为前缀的,所以要查看所有的邮箱,请试试以下代码:
PS> Get-Off365Mailbox
您可以自己选择前缀(见前面的代码),这样您可以同时通过不同的前缀连接到多个 Office365 账户。当您执行 Import-PSSession
时,您还可以省略前缀。
要查看 Office365 导出的所有命令,请使用以下代码:
$import.ExportedCommands