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 及其它知名编辑的位置

PowerShell 技能连载 - 查找由 DHCP 分配的 IP 地址

从 Windows 8 和 Server 2012 起,随着操作系统一起分发了一些扩展的 PowerShell 模块,用于管理服务器和客户端,例如 Get-NetIPAddress 等 cmdlet。

如果您想获得一个所有由 DHCP 分配的 IP 地址列表,可以试试以下方法:

1
2
3
4
5
#requires -Version 3.0 -Modules NetTCPIP

Get-NetIPAddress |
Where-Object PrefixOrigin -eq dhcp |
Select-Object -ExpandProperty IPAddress

PowerShell 技能连载 - Enum 之周: PowerShell 5 中的枚举

支持 PowerShell 5 以上版本

这周我们将关注枚举类型:它们是什么,以及如何利用它们。

从 PowerShell 5 开始,您可以用 “Enum“创建您自己的枚举类型。通过这种方式,用户可以用可阅读的名字,而不是幻数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#requires -Version  5.0

Enum ComputerType
{
ManagedServer
ManagedClient
Server
Client
}

function Connect-Computer
{
param
(
[ComputerType]
$Type,

[string]
$Name
)

"Computername: $Name Type: $Type"
}

当您运行完这段代码,并调用 “Connect-Computer“ 函数之后,PowerShell 自动为您的枚举值提供智能提示,并且只接受枚举类型里规定的值。

1
2
3
4
PS C:\> Connect-Computer -Type Client -Name Test
Computername: Test Type: Client

PS C:\>