Powershell foreach + 获取

Powershell foreach + 获取

我是 powershell 的新手,需要你的帮助解决这个问题。

我需要Get-PrinterConfigurationGet-Printer大家共同努力。

我找到了这个代码:

$Printers = Get-Printer *
Foreach ($Printer in $Printers){Get-PrintConfiguration -PrinterName $Printer.name}

但我仍然需要一些信息Get-Printer,但我不知道如何使它们协同工作。

例如我需要:

Name,,,,来自 和 ,来自。DriverNamePortName​​CommentLocationGet-PrinterColorDuplexingModeGet-PrintConfiguration

你们能帮助我吗?

答案1

这...

$Printers = Get-Printer *
Foreach ($Printer in $Printers){Get-PrintConfiguration -PrinterName $Printer.name}

...可以简化为这样...

Get-Printer | 
Foreach {Get-PrintConfiguration -PrinterName $PSItem.name}

在开始之前使用帮助文件了解哪些是可用的/哪些是不可用的

# Get specifics for a module, cmdlet, or function
(Get-Command -Name Get-Printer).Parameters
(Get-Command -Name Get-Printer).Parameters.Keys
# Results
<#
Name
ComputerName
Full
CimSession
ThrottleLimit
AsJob
Verbose
Debug
ErrorAction
WarningAction
InformationAction
ErrorVariable
WarningVariable
InformationVariable
OutVariable
OutBuffer
PipelineVariable
#>
Get-help -Name Get-Printer -Examples
Get-help -Name Get-Printer -Full
Get-help -Name Get-Printer -Online


(Get-Command -Name Get-PrintConfiguration).Parameters
(Get-Command -Name Get-PrintConfiguration).Parameters.Keys
# Results
<#
ComputerName
PrinterName
PrinterObject
CimSession
ThrottleLimit
AsJob
Verbose
Debug
ErrorAction
WarningAction
InformationAction
ErrorVariable
WarningVariable
InformationVariable
OutVariable
OutBuffer
PipelineVariable
#>
Get-help -Name Get-PrintConfiguration -Examples
Get-help -Name Get-PrintConfiguration -Full
Get-help -Name Get-PrintConfiguration -Online


# Get all data about one printer
Get-Printer | 
Select-Object -First 1 | 
Select-Object -Property '*'
# Results
<#
RenderingMode                : 
PrinterStatus                : Normal
Type                         : Local
DeviceType                   : Print
Caption                      : 
Description                  : 
ElementName                  : 
InstanceID                   : 
CommunicationStatus          : 
DetailedStatus               : 
HealthState                  : 
InstallDate                  : 
Name                         : OneNote for Windows 10
OperatingStatus              : 
OperationalStatus            : 
PrimaryStatus                : 
Status                       : 
StatusDescriptions           : 
BranchOfficeOfflineLogSizeMB : 
Comment                      : 
ComputerName                 : 
Datatype                     : RAW
DefaultJobPriority           : 0
DisableBranchOfficeLogging   : 
DriverName                   : Microsoft Software Printer Driver
JobCount                     : 0
KeepPrintedJobs              : False
Location                     : 
PermissionSDDL               : 
PortName                     : Microsoft.Office.OneNote_1600...
PrintProcessor               : winprint
Priority                     : 1
Published                    : False
SeparatorPageFile            : 
Shared                       : False
ShareName                    : 
StartTime                    : 0
UntilTime                    : 0
WorkflowPolicy               : 
PSComputerName               : 
CimClass                     : ROOT/StandardCimv2:MSFT_Printer
CimInstanceProperties        : {Caption...
CimSystemProperties          : Microsoft.Management.Infrastructure.CimSystemProperties
#>


Get-Printer | 
Select-Object -First 1 | 
Foreach {
    Get-PrintConfiguration -PrinterName $PSItem.name | 
    Select-Object -Property '*'
}
# Results
<#
DuplexingMode         : OneSided
PaperSize             : Letter
Collate               : True
Color                 : True
ComputerName          : 
PrintCapabilitiesXML  : <?xml version="1.0"?>
                        <...
                    
PrinterName           : OneNote for Windows 10
PrintTicketXML        : <?xml version="1.0"?>
                        ...
                    
PSComputerName        : 
CimClass              : ROOT/StandardCimv2:MSFT_PrinterConfiguration
CimInstanceProperties : {Collate, Color, ComputerName, DuplexingMode, PaperSize, PrintCapabilitiesXML, PrinterName, PrintTicketXML}
CimSystemProperties   : Microsoft.Management.Infrastructure.CimSystemProperties
#>

您需要选择所需的内容,然后使用哈希表或自定义对象将它们组合在一起。现在这不是第一天的事情,但同样,关于组合 cmdlet 输出的文章/博客/视频、样本/示例非常丰富。

PowerShell 组合 cmdlet 输出

https://duckduckgo.com/?q=PowerShell+combine+cmdlet+output&t=h_&ia=web

例子:

Get-Printer | 
Select-Object -First 1 | 
Select-Object -Property Name, DriverName, PortName, Comment, Location, 
@{
    Name       = 'Color'
    Expression = {(Get-PrintConfiguration -PrinterName $PSItem.Name).Color}
},
@{
    Name       = 'DuplexingMode '
    Expression = {(Get-PrintConfiguration -PrinterName $PSItem.Name).DuplexingMode }
}
# Results
<#
Name           : OneNote for Windows 10
DriverName     : Microsoft Software Printer Driver
PortName       : Microsoft.Office.OneNote_16001.13801.20202.0_x64__8wekyb3d8bbwe_microsoft.onenoteim_S-1-5-21-3258886415-1034932420-179337933-1002
Comment        : 
Location       : 
Color          : True
DuplexingMode  : OneSided
#>

要获取所有打印机,请删除此行...

Select-Object -First 1 | 

...您的用例不需要 ForLoop。

如果想压缩行数,上面也可以这样写。

Get-Printer | Select-Object -Property Name, DriverName, PortName, Comment, Location, 
@{Name = 'Color';Expression = {(Get-PrintConfiguration -PrinterName $PSItem.Name).Color}},
@{Name = 'DuplexingMode';Expression = {(Get-PrintConfiguration -PrinterName $PSItem.Name).DuplexingMode}}
# Results
<#
Name          : OneNote for Windows 10
DriverName    : Microsoft Software Printer Driver
PortName      : Microsoft.Office.OneNote_16001.13801.20202.0_x64__8wekyb3d8bbwe_microsoft.onenoteim_S-1-5-21-3258886415-1034932420-179337933-1002
Comment       : 
Location      : 
Color         : True
DuplexingMode : OneSided

...

Name          : Microsoft XPS Document Writer
DriverName    : Microsoft XPS Document Writer v4
PortName      : PORTPROMPT:
Comment       : 
Location      : 
Color         : True
DuplexingMode : OneSided

Name          : Microsoft Print to PDF
DriverName    : Microsoft Print To PDF
PortName      : PORTPROMPT:
Comment       : 
Location      : 
Color         : True
DuplexingMode : OneSided

Name          : Fax
DriverName    : Microsoft Shared Fax Driver
PortName      : SHRFAX:
Comment       : 
Location      : 
Color         : False
DuplexingMode : OneSided

...
#>

然而,我发现这更难阅读。但这只是我个人的看法。使用自然换行符(如 '|'、';'、::'、比较运算符等)可以使内容更易于阅读。

答案2

以下脚本演示了如何从多个类中获取属性并将它们全部统一到一个新对象中。

我让你学习和理解所使用的命令并根据您的需要调整脚本。

$Printers = Get-Printer *
Foreach ($Printer in $Printers){
$x = new-object -typename psobject 
$x | Add-Member -Type NoteProperty -Name PrinterName -Value $Printer.name
$x | Add-Member -Type NoteProperty -Name DriverName -Value $Printer.DriverName
$x | Add-Member -Type NoteProperty -Name PortName -Value $Printer.PortName
$x | Add-Member -Type NoteProperty -Name Comment -Value $Printer.Comment
$x | Add-Member -Type NoteProperty -Name Location -Value $Printer.Location
$config = Get-PrintConfiguration -PrinterName $Printer.name
$x | Add-Member -Type NoteProperty -Name Color -Value $config.Color
$x | Add-Member -Type NoteProperty -Name DuplexingMode -Value $config.DuplexingMode
Write-Output $x
}

相关内容