Get List of Printer Drivers from list of Computers PowerShell - powershell

I've tried a variety of iterations of this and gotten a range of errors. I'm trying to get a a list of installed drivers off from a list of computers. None of the ways I've tried in PowerShell have piped the information into a csv. Here's the current iteration of the script.
#Load Active Directory
Import-Module activedirectory
#Load list of computers
$results = #()
$Computer = Get-Content -path 'C:\ScriptResources\computers.txt'
#Check each computer in the list
foreach($ComputerName in $Computer)
{
$results += Get-ADComputer -Filter " Name -Like '*$ComputerName*' " | Get-PrinterDriver; Start-Sleep -milliseconds 500
}
#Export to CSV file
$results | export-csv 'C:\ScriptResults\InstalledPrinters.csv'
I've also used it with just the Get-Printer command and got the following error.
Get-Printer : No MSFT_Printer objects found with property 'Name' equal to 'Redacted'. Verify the value of the
property and retry.
Depending what I've fed the $Computer file I'll get different errors. I've also gotten the RPC server is unavailable and Error Spooler Service Not Running. I have domain wide privileges and I checked the print spooler service and it is running.
The reason I think this is odd is that I have .bat tool that I use that gets printer info from a singular host and I don't run into any issues. The reason I'm trying to put this in PowerShell is because 1) I want to do the whole domain and 2) PowerShell formats its outputs in a more useable fashion.
wmic /node:%ComputerIP% path win32_printer get deviceid, drivername, portname
Additionally, I've also tried the following in the $results function of the script
$results += Get-WmiObject -class Win32_printer -ComputerName name, systemName, shareName
This didn't give errors. What it did instead is that for each computer in the list of computers it checked the computer I was running the script from for its printers and output on each line which printers were installed on my computer.
I'm at a loss and any help would be appreciated. Thanks!

Just so this is closed out. Vivek's answer ended up working.
$results += Get-WmiObject -class Win32_printer -ComputerName $Computer | Select name, systemName, shareName
The RPC issue I was getting was that the list of computers were all turned off for some reason (remote site + different time zone + doing the testing during second shift). Normally, everything remains on though. So that was just an anomaly.
Thanks for the help!

Related

PowerShell script to compile a csv file of all machines and last logged on users

I am in the process of writing a PowerShell script that should check every machine on my domain and export the last logged on user to a csv file. I seem to be getting multiple errors which I can not figure out why this is.
Get-WinEvent : There are no more endpoints available from the endpoint mapper
Get-WinEvent : The RPC server is unavailable So far I have wrote this:
$computers=get-adcomputer -filter {operatingsystem -like '*server*'}|select -exp Name
$data=ForEach($computer in $computers)
{
#Who-loggedinLast -computer $computer -maxresults 2
Get-WinEvent -Computer $computer -FilterHashtable #{Logname='Security';ID=4672} -MaxEvents 1|
select #{N='User';E={$_.Properties[1].Value}}
}
$data |export-csv c:\path.csv -notype ```
first, i think that because you running on WinEvent on each computer in computers, perhaps the first computer it checks isnt online, hence it cant reach the first one and fails.
second, like you got in comment, maybe the RPC rule of inbound is disabled.
i did almost a similar script not too long ago, but to check specific user on all DC machines, and there i did a checkup with test-connection first, to see if the machines are on, and then loop for process of each online machine - maybe you can do something with that too

Powershell Get-ciminstance error in foreach Loop

New to Powershell, trying to use it to update our AD inventory with Serial Numbers from all computers in the environment. I have exported a .csv with all the machine names, and am trying to run through that in a loop from my machine
The script runs fine without the loop-- if I run it and swap the $name variable with a string it works, and if I run it and define the variable and don't loop it work.When in the loop I get this error:
WS-Management could not connect to the specified destination: (#{Name="PC1"}:5985).
This is a different error than if I type a hostname that doesn't exist or one that isn't in DNS. It's getting the proper info from the .csv like "PC1". tried using start-sleep -second 2 to give it some time to connect to each machine but I don't think that's helping.
$admachines = import-csv .\Computerinventory.csv
foreach ($hostname in $admachines){
$sn = Get-CimInstance -Class win32_bios -ComputerName $hostname |select-object -expandproperty serialnumber
Set-ADComputer -identity $hostname -replace #{serialNumber = $sn}
}

Powershell script to get NIC speed from remote hosts

I'm trying to create PowerShell script to get the NIC speed of remote hosts, and output the end result to a txt file.
So far, this is what I've got:
Hostname
Get-NetAdapter | SELECT LinkSpeed
Out-File C:\CheckNIC-Speed\checknic.txt
That's a script for the local computer, but it also won't output the results to the txt, instead it will just create an empty file.
Now, I also need to run it on multiple remote hosts and get the output to the same file.
Thanks.
("Server1","Server2","Server3") | Foreach {
Invoke-Command -ComputerName $_ -ScriptBlock {
[PSCustomObject]#{
LinkSpeed = (Get-NetAdapter).LinkSpeed -join " & "
HostName = $env:COMPUTERNAME
}
}
} | Out-File "C:\File.txt"
This should retrieve info for any of the servers listed within the brackets, you can also import from a list.
look up Invoke-Command Help if you need to do this kind of thing.
You can use wmi-object for the same:
get-wmiobject win32_networkadapter -ComputerName Server01,Server02,Server03 | select name,speed, pscomputername

Get-WMIObject include computer name

I'm trying out a script to go grab installed software on servers remotely. Problem is I want it to output certain attribs including the computer name but I can't seem to figure out how to get the name inserted.
Here is what I have so far...
$servers = Get-QADComputer -SearchRoot "OU=servers,OU=mydomain:-),DC=COM" | Select Name
...which works fine of course. Then...
$servers | % {Get-WMIObject -Class Win32Reg_AddREmovePrograms} | select Displayname,Version,InstallDate,PSComputerName
... which provides the full list of software installed on all servers in that OU but the PSComputerName becomes MY COMPUTER (the computer I run the query from - not the computername of the system being queried). The goal is to have the servername the software is installed on on each line item of software. I've asked professor Google and don't seem to see anything helpful (or anything that I understand anyway).
Hope this makes sense. semi-amateur PS script writer so hopefully this is easy for you guys. Thanks in advance for your help
Your command:
Get-WMIObject -Class Win32Reg_AddREmovePrograms
Does not specify computer to query, so it just query computer command being executed on. Thus PSComputerName display MY COMPUTER, as MY COMPUTER is computer being queried. You have to specify -ComputerName parameter to Get-WMIObject cmdlet to query specific computer. And -ComputerName parameter accept array of computer names, so you can put array of computer names to it instead of using ForEach-Object cmdlet and query one computer at time.
Since the object returned from the WMI call doesn't contain the computer you made the request on, you need to include it yourself from include your ForEach-Object (%) block. You could use Add-Member to add it yourself, then do your Select-Object outside like you're doing now:
$servers | % {
Get-WMIObject -Class Win32Reg_AddREmovePrograms -ComputerName $_ |
Add-Member -MemberType NoteProperty -Name ComputerName -Value $_ -PassThru
} | select Displayname,Version,InstallDate,ComputerName
Another way is to move the Select-Object to inside the block and do it within there, by creating a new property on the fly with a hashtable:
$servers | % {
Get-WMIObject -Class Win32Reg_AddREmovePrograms -computername $_ |
Select-Object Displayname,Version,InstallDate,#{Name='ComputerName';Expression={$_}}
}

Finding the connectivity status of a printer by share name

I'm currently writing a test-page printing script, and am trying to work out if there is a way of "pinging" a printer when its only known details are share name (eg \SERVERNAME\PRINTER01).
Standard ping messages and test-connection tests don't perform this job as far as I can see. Any ideas of how to make this functionality in Powershell?
Assuming you are using a network printer and simply sharing it, try this:
$sharedPrinter = Get-CIMInstance CIM_Printer -ComputerName SERVERNAME |`
Where-Object{$_.ShareName -match 'PRINTER01'} |`
Select-Object -ExpandProperty PortName
Test-Connection $sharedPrinter