PowerShell 技能连载 - 查找隐藏的自启动程序

Ever wondered why some programs launch whenever you log into Windows? Here’s a one liner listing autostarts that affect your login:
是否好奇为什么有些程序在登录 Windows 的时候会自动启动?这是一行列出登录时自启动项的代码:

1
2
3
4
5
#requires -Version 3

Get-CimInstance -ClassName Win32_StartupCommand |
Select-Object -Property Command, Description, User, Location |
Out-GridView

PowerShell 技能连载 - Getting List of Current Group Memberships

当您可以通过 Active Directory 来获取一个用户的组成员,有一个简单的方法是直接通过用户的 access token 获取信息,而不需要 AD 联系人。

这行代码将取出当前用户所在的所有组的 SID:

1
2
#requires -Version 3.0
[System.Security.Principal.WindowsIdentity]::GetCurrent().Groups.Value

这是获取翻译后的组名的方法:

1
2
#requires -Version 3.0
[System.Security.Principal.WindowsIdentity]::GetCurrent().Groups.Translate( [System.Security.Principal.NTAccount])

如果这个列表中有重复,那么您就可以知道有多个 SID 指向同一个名字。这种情况在您曾经迁移过 AD(SID 历史)时可能会发生。只需要将结果用管道输出到 Sort-Object -Unique 就能移除重复。

PowerShell 技能连载 - 将 SID 翻译为用户名

是否希望将安全标识符(SID)翻译为一个真实的名称?这个函数将能够帮助您:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#requires -Version 3.0

function ConvertFrom-SID
{
param
(
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias('Value')]
$Sid
)

process
{
$objSID = New-Object System.Security.Principal.SecurityIdentifier($sid)
$objUser = $objSID.Translate( [System.Security.Principal.NTAccount])
$objUser.Value
}
}

您可以通过参数传入 SID,或通过管道传入一个或多个 SID 给这个函数。

PowerShell 技能连载 - 获取计算机的地理位置

这是获取地理位置信息的另一个免费源,它能够获取您当前的公网 IP 和位置信息:

1
2
3
#requires -Version 3.0

Invoke-RestMethod -Uri 'http://ipinfo.io'

结果大概如下:

1
2
3
4
5
6
ip       : 80.187.113.144
hostname : tmo-113-144.customers.d1-online.com
region :
country : DE
loc : 51.2993,9.4910
org : AS3320 Deutsche Telekom AG

PowerShell 技能连载 - 查找操作系统架构信息

这是获取您操作系统信息的一行代码:

1
Get-WmiObject -Class Win32_OperatingSystem | Select-Object -Property *OS*

结果看起来类似如下:

1
2
3
4
5
ForegroundApplicationBoost : 2
OSArchitecture : 64-bit
OSLanguage : 1031
OSProductSuite : 256
OSType : 18

如果您想知道这些数字代表什么意思,可以在网上搜索 Win32_OperatingSystem 第一个链接就会引导您到对应的 MSDN 文档。

如果您想从一个远程系统中获取信息,请使用 -ComputerName–Credential 参数。

PowerShell 技能连载 - 在 Active Directory 中查找操作系统版本

如果您安装了带有 “ActiveDirectory” PowerShell 模块的 Microsoft RSAT 工具,以下是一个快速获取您环境中操作系统清单的快速方法:

1
2
3
4
#requires -Module ActiveDirectory

Get-ADComputer -Filter * -Properties OperatingSystem, OperatingSystemServicePack, OperatingSystemVersion |
Select-Object -Property Name, OperatingSystem, OperatingSystemServicePack, OperatingSystemVersion

这将获得所有计算机的信息。您可以将搜索范围限制在指定的计算机名和 AD 位置中。以下命令将搜索范围限制在 $root AD 范围内,以及只包含名字以 “Serv” 开头的计算机中:

1
2
3
4
5
6
#requires -Module ActiveDirectory

$root = 'OU=North,OU=Clients,DC=yourcompany,DC=com'

Get-ADComputer -Filter { Name -like 'Serv*' } -Properties OperatingSystem, OperatingSystemServicePack, OperatingSystemVersion <#-ResultSetSize 10#> -SearchBase $root -SearchScope Subtree |
Select-Object -Property Name, OperatingSystem, OperatingSystemServicePack, OperatingSystemVersion

PowerShell 技能连载 - 查找一个月中的第一天和最后一天

您是否曾需要了解某个月的第一天和最后一天?

以下是一个简单的实现方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
# specify the date you want to examine
# default is today
$date = Get-Date
$year = $date.Year
$month = $date.Month

# create a new DateTime object set to the first day of a given month and year
$startOfMonth = Get-Date -Year $year -Month $month -Day 1 -Hour 0 -Minute 0 -Second 0 -Millisecond 0
# add a month and subtract the smallest possible time unit
$endOfMonth = ($startOfMonth).AddMonths(1).AddTicks(-1)

$startOfMonth
$endOfMonth

PowerShell 技能连载 - 在地图上定位您的地理位置

在前一个技能当中,通过 Internet 能知道您的 IP 地址,以及地理位置。您可以获得当前公开 IP 地址的经纬度,代码如下:

1
2
3
4
5
6
$ip = Invoke-RestMethod -Uri http://checkip.amazonaws.com/
$geo = Invoke-RestMethod -Uri "http://geoip.nekudo.com/api/$IP"
$latitude = $geo.Location.Latitude
$longitude = $geo.Location.Longitude

"Lat $latitude Long $longitude"

如果您希望看到它究竟在什么位置,可以将这些信息连到 Google Maps 上:

1
2
3
4
5
6
7
$ip = Invoke-RestMethod -Uri http://checkip.amazonaws.com/
$geo = Invoke-RestMethod -Uri "http://geoip.nekudo.com/api/$IP"
$latitude = $geo.Location.Latitude
$longitude = $geo.Location.Longitude

$url = "https://www.google.com/maps/preview/@$latitude,$longitude,8z"
Start-Process -FilePath $url

这段代码将打开浏览器,导航到 Google Maps,并且在地图上显示当前位置。当前通过 IP 地址定位地理位置还比较粗糙,至少在使用公开地理数据时。

PowerShell 技能连载 - 查找公开 IP 地址

您是否希望了解您当前连接到 Internet 的公开 IP 地址?以下是一行代码:

1
2
#requires -Version 3.0
Invoke-RestMethod -Uri http://checkip.amazonaws.com/

通过这个 IP 地址,您还可以向 Internet 请求您的地理地址:

1
2
3
4
#requires -Version 3.0
$ip = Invoke-RestMethod -Uri http://checkip.amazonaws.com/
Invoke-RestMethod -Uri "http://geoip.nekudo.com/api/$IP" |
Select-Object -ExpandProperty Country

PowerShell 技能连载 - 查找 IP 地址的地理位置

大多数 IP 地址可以用 Web Service 定位到物理地址。以下是一个很简单的函数,能够输入一个 IP 地址并返回它的物理地址:

1
2
3
4
5
6
7
#requires -Version 3.0

function Get-IPLocation([Parameter(Mandatory)]$IPAddress)
{
Invoke-RestMethod -Method Get -Uri "http://geoip.nekudo.com/api/$IPAddress" |
Select-Object -ExpandProperty Country -Property City, IP, Location
}

这个例子能够演示如何用 Select-Object 配合 -Property-ExpandProperty 参数将一些嵌套的属性移到上一层。

让我们查找 Google DNS 服务器位于什么位置:

1
2
3
4
5
6
7
PS C:\> Get-IPLocation -IPAddress 8.8.8.8

name : United States
code : US
city : Mountain View
ip : 8.8.8.8
location : @{accuracy_radius=1000; latitude=37,386; longitude=-122,0838; time_zone=America/Los_Angeles}

And here is how you can resolve any hostname to an IP address, for example, the famous powershellmagazine.com:
以下是如何将任意主机名解析成 IP 地址的代码,例如知名的 powershellmagazine.com:

1
2
PS> [Net.Dns]::GetHostByName('powershellmagazine.com').AddressList.IPAddressToString
104.131.21.239

所以如果您想知道该 IP 地址位于哪里,请加上这段代码:

1
2
3
4
5
6
7
8
PS> Get-IPLocation -IPAddress 104.131.21.239

name : United States
code : US
city : New York
ip : 104.131.21.239
location : @{accuracy_radius=1000; latitude=40,7143; longitude=-74,006;
time_zone=America/New_York}

(of course this is just where the server sits, not Aleksandar or Ravi or all the other fine editors
(当然这只代表了服务器的所在地,而不是 Aleksandar or Ravi 及其它知名编辑的位置