我们正在尝试批量检索所有已安装的打印机。为此,我们使用-AsJob
CmdLet上的Get-Printer
开关。当所有ComputerNames
存在时,这种方法就可以正常工作,但是当不存在一个$ComputerName = @('Computer1', 'NonExisting', 'Computer2')
$GetPrinterJobs = Foreach ($C in $ComputerName) {
Get-Printer -ComputerName $C -AsJob
}
$GetPrinterJobs | Wait-Job -EA Ignore | Receive-Job
时,则会正确引发错误。
示例
NonExisting
此代码将为NonExisting
ComputerName引发一个错误。怎么可能发现错误实际上来自ComputerName $GetPrinterJobs = Foreach ($C in $ComputerName) {
$C
Get-Printer -ComputerName $C -AsJob
}
$null = Get-Job | Wait-Job -EA Ignore
$GetPrinterJobResults = for ( $i = 0; $i -lt $GetPrinterJobs.Count; $i += 2 ) {
$ReceiveParams = @{
ErrorVariable = 'JobError'
ErrorAction = 'SilentlyContinue'
}
$ReceivedJobResult = $GetPrinterJobs[$i + 1] | Receive-Job @ReceiveParams
[PSCustomObject]@{
ComputerName = $GetPrinterJobs[$i]
State = $GetPrinterJobs[$i + 1].State
Data = $ReceivedJobResult
Error = if ($JobError) {
$JobError
$JobError.ForEach( { $Error.Remove($_) })
}
}
}
$GetPrinterJobResults
?
Get-Printer:无法访问后台处理程序服务。确保 后台处理程序服务正在运行。
通过@JosefZ的以下帮助,已已解决,如下所示:
CREATE TABLE `destinations` (
`id` int(50) NOT NULL,
`name` varchar(225) NOT NULL,
`description` text NOT NULL,
`code` int(50) NOT NULL,
`menu_code` int(50) NOT NULL,
`menu_position` int(50),
`status` tinyint(3) NOT NULL,
`price` DOUBLE(50) NOT NULL);
答案 0 :(得分:1)
这是我的老派解决方法:
$ComputerName = @('Computer1', 'NonExisting', 'Computer2')
$GetPrinterJobs = Foreach ($C in $ComputerName) {
$C
Get-Printer -ComputerName $C -AsJob
}
$GetPrinterJobResults = $GetPrinterJobs |
Where-Object { $_.GetType().Name -ne 'String'} |
Wait-Job -EA Ignore | Receive-Job
for ( $i = 0; $i -lt $GetPrinterJobs.Count; $i+=2 ) {
@{
$GetPrinterJobs[$i] = $GetPrinterJobs[$i + 1].State
}
}
应该返回类似
的内容Name Value
---- -----
Computer1 Completed
NonExisting Failed
Computer2 Completed