PowerShell 技能连载 - 分析并移除打印任务

适用于 Windows 8.1 或 Server 2012 R2

Windows 8.1 和 Server 2012 R2 引入了一个名为“PrintManagement”的模块。它包含了管理本地和远程打印机所需的所有 cmdlet。

在前一个技能中我们演示了如何读取打印任务。每个打印任务都有一个 JobStatus 属性告诉您该 PrintJob 是否成功完成。

可以通过这种方式获取所有的状态码:

PS> Import-Module PrintManagement

PS> [Microsoft.PowerShell.Cmdletization.GeneratedTypes.PrintJob.JobStatus]::GetNames([Microsoft.PowerShell.Cmdletization.GeneratedTypes.PrintJob.JobStatus])
Normal
Paused
Error
Deleting
Spooling
Printing
Offline
PaperOut
Printed
Deleted
Blocked
UserIntervention
Restarted
Complete
Retained
RenderingLocally

接下来,您可以过滤已有的打印任务。并且,比如打印出所有已完成或有错误的打印任务。这段代码将列出所有有错误或已完成的打印任务:

$ComputerName = $env:COMPUTERNAME

Get-Printer -ComputerName $ComputerName |  ForEach-Object {
  Get-PrintJob -PrinterName $_.Name -ComputerName $ComputerName |
    Where-Object { $_.JobStatus -eq 'Complete' -or $_.JobStatus -eq 'Error' -or $_.JobStatus -eq 'Printed'}
 }

要移除这些打印任务,只需要加上 Remove-PrintJob 命令:

$ComputerName = $env:COMPUTERNAME

Get-Printer -ComputerName $ComputerName |  ForEach-Object {
  Get-PrintJob -PrinterName $_.Name -ComputerName $ComputerName |
    Where-Object { $_.JobStatus -eq 'Complete' -or $_.JobStatus -eq 'Error' -or $_.JobStatus -eq 'Printed'}
 } |
 Remove-PrintJob -CimSession $ComputerName

AngularJS 最佳实践

johnpapa/angularjs-styleguide

  • Watch 419
  • Star 5,141
  • Fork 569

mgechev/angularjs-style-guide

  • Watch 191
  • Star 2,625
  • Fork 313

中文版:
README-zh-cn.md

turingou/Angular-Best-Practices

本repo由支付宝前端开发工程师 @莫登(新浪微博@郭宇)维护,部分案例包括Angular在 支付宝某些系统上的使用经验

  • Watch 7
  • Star 21
  • Fork 6

AngularJS移动开发中的坑汇总

  • Watch 7
  • Star 21
  • Fork 6

AngularJS 最佳实践 – 尘埃落定

PowerShell 技能连载 - 列出所有打印任务

适用于 Windows 8.1 或 Server 2012 R2

Windows 8.1 和 Server 2012 R2 引入了一个名为“PrintManagement”的模块。它包含了管理本地和远程打印机所需的所有 cmdlet。

要列出指定计算机的所有打印任务,首先确定可用的打印机,然后用循环取出每个打印机的打印任务。这实际做起来十分简单:

$ComputerName = $env:COMPUTERNAME

Get-Printer -ComputerName $ComputerName |  ForEach-Object {
  Get-PrintJob -PrinterName $_.Name -ComputerName $ComputerName
 }

如果该代码返回空,那么说明没有打印任务(或者您没有读取它们的权限)。

PowerShell 技能连载 - 远程更新组策略

适用于 Windows 8.1 或 Server 2012 R2

要更新远程计算机上的组策略设置,请使用 Invoke-GPUpdate,并且传入希望更新设置的计算机名。

Invoke-GPUpdate 在远程计算机上创建“gpupdate”计划任务。您可以使用 –RandomDelayInMinutes 指定一个 0 至 44640 分钟(31 天)之间的值。该 cmdlet 将使用一个随机的时间因子来避免网络阻塞。

PowerShell 技能连载 - 管理打印机

适用于 Windows 8.1 和 Server 2012 R2

Windows 8.1 和 Server 2012 R2 带来了一个叫做“PrintManagement”的模块。它包含了管理本地和远程打印机所需的所有 Cmdlet。

以下是一个示例脚本,演示了安装打印机驱动、设置打印机端口、安装打印机、共享该打印机,以及设置某些打印机属性的过程。

$ComputerName = $env:COMPUTERNAME

$DriverName = 'Samsung SCX-483x 5x3x Series XPS'
$IPAddress = '192.168.2.107'
$PortName = 'NetworkPrint_192.168.2.107'
$PrinterName = 'BWPrint'
$ShareName = 'Office 12'

Add-PrinterDriver -ComputerName $ComputerName -Name $DriverName
Add-PrinterPort -Name $PortName -ComputerName $ComputerName
Add-Printer -ComputerName $ComputerName -Name $PrinterName -DriverName $DriverName -Shared -ShareName $ShareName -PortName $PortName
Set-PrintConfiguration -ComputerName $ComputerName -PrinterName $PrinterName -PaperSize A4

要使用它,请确保您修改了 $IPAddress 并指向一个存在的打印机。请将 $ComputerName 修改指向一个远程计算机而不是您的本地计算机。

要列出 PrintManagement 模块所带的所有 Cmdlet,请试试以下代码:

PS> Get-Command -Module PrintManagement

CommandType     Name                                               ModuleName
-----------     ----                                               ----------
Function        Add-Printer                                        PrintManagement
Function        Add-PrinterDriver                                  PrintManagement
Function        Add-PrinterPort                                    PrintManagement
Function        Get-PrintConfiguration                             PrintManagement
Function        Get-Printer                                        PrintManagement
Function        Get-PrinterDriver                                  PrintManagement
Function        Get-PrinterPort                                    PrintManagement
Function        Get-PrinterProperty                                PrintManagement
Function        Get-PrintJob                                       PrintManagement
Function        Read-PrinterNfcTag                                 PrintManagement
Function        Remove-Printer                                     PrintManagement
Function        Remove-PrinterDriver                               PrintManagement
Function        Remove-PrinterPort                                 PrintManagement
Function        Remove-PrintJob                                    PrintManagement
Function        Rename-Printer                                     PrintManagement
Function        Restart-PrintJob                                   PrintManagement
Function        Resume-PrintJob                                    PrintManagement
Function        Set-PrintConfiguration                             PrintManagement
Function        Set-Printer                                        PrintManagement
Function        Set-PrinterProperty                                PrintManagement
Function        Suspend-PrintJob                                   PrintManagement
Function        Write-PrinterNfcTag                                PrintManagement

如您所见,它们实际上是 PowerShell 函数而不是二进制 Cmdlet。

用 PowerShell 显示 黑客帝国数码雨动画

请在 PowerShell 控制台中执行本脚本

今天在群里看到一个数码雨的课题,试着实现了一下:

【话痨】powershell传教士(1328486072) 12:58:11
话说有人用bat写出了数码雨,谁也用powershell写一个,我用powershell写了几个,总感觉不对。
【话痨】powershell传教士(1328486072) 12:58:52
有人对命令行数码雨,感兴趣么?

根据传教士的提示,改了一下,避免了闪烁。

实现效果

Matrix

源代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
## Prepare the screen
$host.UI.RawUI.BackgroundColor = "Black"
$host.UI.RawUI.ForegroundColor = "Green"

$charSet = '0123456789'.ToCharArray()

$width = 75
$height = [Console]::WindowHeight
$maxStringLength = 7
$minStringLength = 2
$maxSpaceLength = 20
$minSpaceLength = 6

$lines = New-Object System.Collections.ArrayList
$symbols = @()

for ($i = 0; $i -lt $width; $i++) {
$symbols += ''
}

function AddLine([string]$line) {
$lines.insert(0, $line)
if ($lines.Count -eq $height) {
$lines.RemoveAt($lines.Count - 1)
}
}

function ShowFrame() {
Write-Host ($lines.ToArray() -join "`n")
}

function TryGenerateSymbol() {
for ($i = 0; $i -lt $width; $i++) {
$column = $symbols[$i]
if ($column -eq '') {
# initial state, generate spaces
$symbols[$i] = New-Object String ' ', (Get-Random -Minimum $minSpaceLength -Maximum $maxSpaceLength)
} elseif ($column -eq ' ') {
# last space
$randomCount = Get-Random -Minimum $minStringLength -Maximum $maxStringLength
$chars = Get-Random -InputObject $charSet -Count $randomCount
$symbols[$i] = $column + ($chars -join '')
} elseif ($column.Length -eq 1) {
# last char
$symbols[$i] = $column + (New-Object String ' ', (Get-Random -Minimum $minSpaceLength -Maximum $maxSpaceLength))
}
}
}

function UpdateFrame() {
TryGenerateSymbol

$line = @()
for ($i = 0; $i -lt $width; $i++) {
$column = $symbols[$i]
$line += $column[0]
$symbols[$i] = $column.Substring(1, $column.Length - 1)
}
$line = $line -join ''
AddLine $line
}

try
{
$host.UI.RawUI.WindowSize = New-Object System.Management.Automation.Host.Size $width + 1, $height + 1
}
catch {}

try
{
$host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size $width + 1, $height + 1
} catch {}

try
{
while($true)
{
if([Console]::KeyAvailable)
{
$key = [Console]::ReadKey()
if(($key.Key -eq 'Escape') -or
($key.Key -eq 'Q') -or
($key.Key -eq 'C'))
{
break
}
}

# Clear-Host

$host.UI.RawUI.CursorPosition = New-Object System.Management.Automation.Host.Coordinates 0,0

UpdateFrame
ShowFrame

$host.UI.RawUI.CursorPosition = New-Object System.Management.Automation.Host.Coordinates `
0,([Console]::WindowHeight - 1)
Write-Host -NoNewLine 'Q or ESC to Quit'

Start-Sleep -m 100
}
}
finally
{
## Clean up, display exit screen
Clear-Host
"`n"
" Happy Scripting from PowerShell..."
" by Victor.Woo!"
"`n`n`n"
}

您也可以在这里下载 Matrix.ps1

PowerShell 技能连载 - 简化 .NET 类型

适用于 PowerShell 所有版本

PowerShell 为多数常见的 .NET 类型定义了一个短名字。要查看已有多少个 .NET 类型定义了短名称,请使用以下代码:

PS> [System.Management.Automation.LanguagePrimitives]::ConvertTypeNameToPSTypeName("System.String")
[string]

PS> [System.Management.Automation.LanguagePrimitives]::ConvertTypeNameToPSTypeName("System.Int32")
[int]

PS> [System.Management.Automation.LanguagePrimitives]::ConvertTypeNameToPSTypeName("System.Management.ManagementObject")
[wmi]

PS> [System.Management.Automation.LanguagePrimitives]::ConvertTypeNameToPSTypeName("System.DirectoryServices.DirectoryEntry")
[adsi]

PS>

要查用另一种方法看真实的 .NET 名称,请使用以下方法:

PS> [string].FullName
System.String

PS> [int].FullName
System.Int32

PS> [wmi].FullName
System.Management.ManagementObject

PS> [adsi].FullName
System.DirectoryServices.DirectoryEntry

PS>

通过这个技巧,您还可以更好地理解 PowerShell 转换数据类型的机制:

PS> [System.Management.Automation.LanguagePrimitives]::ConvertTypeNameToPSTypeName("UInt8")
[Byte]

PS>

这表明了,当 PowerShell 遇到一个无符号 8 位整型数值,将自动把它转换为一个 Byte 数据。整个魔法是由 ConvertTypeNameToPSTypeName() 完成的。在内部,PowerShell 使用一个检索表来转换特定的数据类型:

$field = [System.Management.Automation.LanguagePrimitives].GetField('nameMap', 'NonPublic,Static')
$field.GetValue([System.Management.Automation.LanguagePrimitives])

该检索表看起来类似这样:

Key                                                            Value
---                                                            -----
SInt8                                                          SByte
UInt8                                                          Byte
SInt16                                                         Int16
UInt16                                                         UInt16
SInt32                                                         Int32
UInt32                                                         UInt32
SInt64                                                         Int64
UInt64                                                         UInt64
Real32                                                         Single
Real64                                                         double
Boolean                                                        bool
String                                                         string
DateTime                                                       DateTime
Reference                                                      CimInstance
Char16                                                         char
Instance                                                       CimInstance
BooleanArray                                                   bool[]
UInt8Array                                                     byte[]
SInt8Array                                                     Sbyte[]
UInt16Array                                                    uint16[]
SInt16Array                                                    int64[]
UInt32Array                                                    UInt32[]
SInt32Array                                                    Int32[]
UInt64Array                                                    UInt64[]
SInt64Array                                                    Int64[]
Real32Array                                                    Single[]
Real64Array                                                    double[]
Char16Array                                                    char[]
DateTimeArray                                                  DateTime[]
StringArray                                                    string[]
ReferenceArray                                                 CimInstance[]
InstanceArray                                                  CimInstance[]
Unknown                                                        UnknownType