I am attempting to work on a script to call all printers including network printers for a computer on a domain. I first found how to do this for one computer:
$printerList = Get-CimInstance -ClassName Win32_Printer -ErrorAction Stop -ComputerName $env:COMPUTERNAME | Select-Object Name
Without including the environmental variable part '$env:' I was only able to get a list of local printers not any network/shared printers.
My specific issue came up when trying to have the -ComputerName parameter be another variable that was an expandable string. This is important since I will need the script to be able to take a user input and have the command run without them having to manually type it out. I would run something like this:
$a = '$env:' "$ComputerName"
$printerList = Get-CimInstance -ClassName Win32_Printer -ErrorAction Stop -ComputerName $a | Select-Object Name
But then I get the error:
Get-CimInstance : The WS-Management service cannot process the request because port $ComputerName:port/wsman is invalid.
... Printers3 = Get-CimInstance -ClassName Win32_Printer -ErrorAction Sto ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : NotSpecified: (:) [Get-CimInstance], CimException
FullyQualifiedErrorId : Microsoft.Management.Infrastructure.CimException,Microsoft.Management.Infrastructure.CimCmdlets.GetCimInstanceCommand
Not sure what this means but it is strange that it works manually but not when trying to expand the string.
CodePudding user response:
Was able to successfully query the network printers using the invoke-command function to remote query the registry.
$scriptblock={
Get-ChildItem Registry::\HKEY_Users | Select-Object -ExpandProperty
PSChildName | ForEach-Object { Get-ChildItem Registry::\HKEY_Users\$_\Printers\Connections -Recurse -ea ignore | Select-Object -ExpandProperty Name}
}
$printers = invoke-command -ScriptBlock $scriptblock -computername $computername