I want to write a PowerShell script to get all the print servers in a network.
I have used the following LDAP query, but it returns only servers with network printers attached to it. But not other print servers that have a remote printer attached to it.
Here's the code I used to get the print servers (But getting only the servers with n/w printers)
Import-Module ActiveDirectory
[array]$testarray = Get-ADObject -LDAPFilter "(&(&(&(uncName=*)(objectCategory=printQueue))))" -properties *|Sort-Object -Unique -Property servername |select servername
$testarray
You could try something like Get-WMIObject win32_printer | select name,local wrapped in a foreach-object loop like this:
$servers | ForEach-Object {
Get-WMIObject Win32_Printer -computername $_ | Where-Object {$_.local -like 'False'} | select Name,local,SystemName | format-table -a
}
The $servers can be what scoped to whatever servers you need to check.
Use Get-WMIObject Win32_Printer | select * to see what properties you want to report back on and include them in the | select Name,local,SystemName section of the script
You could use a combination of Get-ADComputer and Get-Printer to list all shared printer queues on computers joined to your domain:
Get-ADComputer -Filter * | % {
$computer = $_.Name
Get-Printer -Computer $computer | ? { $_.Shared } | select -Expand Name
}
This doesn't cover computers that aren't domain members, though.
Related
I am trying to get write a script where I can get all of the machine within my domains. here is what I found so far however I need to add additional information and still unable to get the correct information to get pull out. If someone can help me this will be great.
Get-ADComputer -Filter 'operatingsystem -like "*Windows server*" -and enabled -eq "true"' -Properties Name,Operatingsystem, OperatingSystemVersion, OperatingSystemServicePack,IPv4Address | Sort-Object -Property Operatingsystem | Select-Object -Property Name,Operatingsystem, OperatingSystemVersion, OperatingSystemServicePack, IPv4Address| ft -Wrap –Auto
I still need to be able to grab the MAC Address from all machines as well domains the machine belong to. and to make it worst I need to figure out how to export all of the data to CSV.
You will need to loop over the computers and get the MAC address individually inside the loop:
# Get-ADComputer returns these properties by default:
# DistinguishedName, GroupCategory, GroupScope, Name, ObjectClass, ObjectGUID, SamAccountName, SID
$props = 'OperatingSystem', 'OperatingSystemVersion', 'OperatingSystemServicePack', 'IPv4Address'
$result = Get-ADComputer -Filter "operatingsystem -like '*Windows server*' -and enabled -eq 'true'" -Properties $props |
Sort-Object OperatingSystem | ForEach-Object {
$mac = if ((Test-Connection -ComputerName $_.Name -Count 1 -Quiet)) {
# these alternative methods could return an array of MAC addresses
# get the MAC address using the IPv4Address of the computer
(Get-CimInstance -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled='True'" -ComputerName $_.IPv4Address).MACAddress
# or use
# Invoke-Command -ComputerName $_.IPv4Address -ScriptBlock { (Get-NetAdapter | Where-Object {$_.Status -eq 'Up'}).MacAddress }
# or
# (& getmac.exe /s $_.IPv4Address /FO csv | ConvertFrom-Csv | Where-Object { $_.'Transport Name' -notmatch 'disconnected' }).'Physical Address'
}
else { $mac = 'Off-Line' }
# return the properties you want as object
$_ | Select-Object Name, OperatingSystem, OperatingSystemVersion, OperatingSystemServicePack, IPv4Address,
#{Name = 'MacAddress'; Expression = {#($mac)[0]}}
}
# output on screen
$result | Format-Table -AutoSize -Wrap
# or output to CSV file
$result | Export-Csv -Path 'X:\Wherever\ADComputers.csv' -NoTypeInformation -UseCulture
Active directory computer object doesn't contain the MAC address attribute , so you will not be able to get the info needed using active directory object only; but instead you can use the "IPv4Address" attribute of the AD computer object and query the DHCP server to find the machines MAC address and place the output data as "custompsobject" then export the result as C.V sheet.
Also if you have System center configuration manager "SCCM" you can query its database to generate a report with all needed data (Name,Operatingsystem, OperatingSystemVersion, OperatingSystemServicePack,IPv4Address and MAC address)
#Listing machine from which we will Query
$Machines = Get-ADComputer -Filter * -SearchBase 'OU=Laptops,OU=Win10Modern,OU=LN,OU=Workstations,DC=cooley,DC=com' | Select-Object Name
#Getting the Network Adapter version for Wi-Fi Adapter
ForEach ($Machine in $Machines) {
Get-NetAdapter | Select-Object Name,InterfaceDescription,DriverVersion,DriverDate,DriverProvider
}
Currently, your code loops over objects in variable $Machines, where each object has a single property called Name.
In order to get just the name values, either use Select-Object -ExpandProperty Name or get the array of names like this:
# get an array of computernames
$Machines = (Get-ADComputer -Filter * -SearchBase 'OU=Laptops,OU=Win10Modern,OU=LN,OU=Workstations,DC=cooley,DC=com').Name
Next loop over these computernames and have each computer run the Get-NetAdapter cmdlet:
# capture the output(s) in variable $result
$result = foreach ($Machine in $Machines) {
if (Test-Connection -ComputerName $Machine -Count 1 -Quiet) {
Invoke-Command -ComputerName $Machine -ScriptBlock {
Get-NetAdapter | Select-Object SystemName,Name,InterfaceDescription,
DriverVersion,DriverDate,DriverProvider,Status,AdminStatus
}
}
else {
Write-Warning "Computer '$Machine' does not respond"
}
}
# output on screen
$result
# or to GridView
$result | Out-GridView -Title 'NetAdapterInfo'
# or to CSV file
$result | Export-Csv -Path 'X:\NetAdapterInfo.csv' -NoTypeInformation
AdminStatus is a setting (enabled --> 'up'; disabled --> 'down')
Status is operational status (connected --> 'up'; disconnected --> 'down')
I don't think you can use Get-NetAdapter to connect to remote computers.
You can however use Get-WmiObject Win32_NetworkAdapter -ComputerName .
Like this:
ForEach ($Machine in $Machines) {
Get-WmiObject -Class Win32_NetworkAdapter -Filter "NetConnectionStatus = 2" -ComputerName $Machine
}
You need to become familiar with the properties of the Win32_NetworkAdapter class. You can see all of the properties by running this command:
Get-WmiObject -Class Win32_NetworkAdapter -ComputerName "Localhost" | fl * -Force
or you can use this command to see all of the properties (and methods) available to you.
Get-WmiObject -Class Win32_NetworkAdapter -ComputerName "Localhost" | Get-Member
Most computers will have more than 1 network card (some are hidden) and you have to filter the irrelevant ones out.
I'm setting up a RADIUS server and all works fine except for laptops with a certain Wireless NIC.
I managed to solve the problem, but I wish to have a list of all my AD Domain joined computers with the faulty/outdated NIC (all of our laptops are in several different OU's).
I was thinking something of combining the GET-ADcomputer -filter * (to list all computer objects) and combine this with the get-wmiobject win32_networkadapter | select netconnectionid, name | where name -eq "Intel(R) Dual Band Wireless-AC 8265"
The PS cmdlets work separately, but I want to match the get-wmiobject for each match triggered by the get-adcomputerCan you help me out ? :)
This is how you can approach your script. I do agree with Abraham's comment, if you can, use Get-CimInstance:
Get-CimInstance win32_networkadapter | Where-Object Name -EQ $match
$computers = Get-ADComputer -Filter *
$match = 'Intel(R) Dual Band Wireless-AC 8265'
$result = foreach($computer in $computers)
{
$wmi = Get-WmiObject win32_networkadapter | Where-Object Name -EQ $match
if(-not $wmi){ continue }
[pscustomobject]#{
ComputerName = $computer.Name
NetconnectionID = $wmi.NetConnectionID
Name = $wmi.Name
}
}
$result | Export-Csv 'path/to/csv.csv' -NoTypeInformation
Edit
I haven't tested this nor I have a way of testing it right now, but if this works should be a lot faster, using query language:
$computers = (Get-ADComputer -Filter *).sAMAccountName
$match = 'Intel(R) Dual Band Wireless-AC 8265'
$filter = #{
ComputerName = $computers
Query = "SELECT NetConnectionID, Name FROM win32_networkadapter WHERE Name = '$match'"
}
Get-WmiObject #filter
I am missing something fundamental in PowerShell.
I have a script that generates two collections, computer names with version details of a specific application and a separate user name list that is taken from the computer names list because the user names are in the computer names, for example a computer name is:
XXXXXX02jbloggs
The owner of this computer is jbloggs and jbloggs is a valid AD object which has a full name of joe blogs.
The ultimate objective of the script is to produce a report with computer names, owner SamAccountName, full name and application details, which the script will specifically check for.
For example,
what version(s) of Adobe Reader exist on this range of machines
So far I have:
$ErrorActionPreference = "SilentlyContinue"
$Computers = Get-ADComputer -Server BlahBlah.com -Filter {name -like "XXXXXX02*"} |
Select-Object -ExpandProperty Name
$Users = $Computers -Replace '\D*\d*(\w*)', '$1'
$Results = foreach ($Computer in $Computers) {
Get-CimInstance -ComputerName $Computer -ClassName Win32_Product |
Where-Object{$_.Name -like "*Adobe Reader*"} |
Select-Object PSComputerName, Name, Version, InstallDate
}
$FullNames = ForEach ($user in $Users) {
Get-ADUser -Server BlahBlah.com -Identity $User -Properties * |
Select-Object -ExpandProperty Name
}
$Results gets me a list of computer names, Adobe Reader xxx, the version and install date.
$FullNames gets me a list of the full names based on their user IDs
I do not know how to construct the script so it produces Full Name, User Name, Computer Name, Application Name and Install Date.
This is why I say I am missing something fundamental in PowerShell, I have been looking at custom objects, nested loops and other ideas but to no avail. Really looking for some advice on this type of problem as I several similar examples I need to accomplish.
Any advice would be greatly appreciated.
Get the single current user inside the foreach($computer in $Computers) instead of creating two separate foreach.
Add a calculated property to the select to include FullName in
$Result
$ErrorActionPreference = "SilentlyContinue"
$Computers = Get-ADComputer -Server BlahBlah.com -Filter {name -like "XXXXXX02*"} |
Select-Object -ExpandProperty Name
$Results = foreach ($Computer in $Computers) {
$User = $Computer -Replace '\D*\d*(\w*)', '$1'
$FullName = (Get-ADUser -Server BlahBlah.com -Identity $User -Properties *).Name
Get-CimInstance -ComputerName $Computer -ClassName Win32_Product |
Where-Object{$_.Name -like "*Adobe Reader*"} |
Select-Object PSComputerName, Name, Version, InstallDate,#{n='FullName';e=#{$FullName}}
}
I've been trying to develop a powershell script to query a particular OU in AD and then get the services on each of those services including name, status, startmode, startname, description. I've gotten really close but not getting the results I expect. Can someone help?
The problem is if I run the command for one server specifically, I get the results I want. If I run for all servers in an OU it does not find any unique startnames (only the local accounts). I want a single CSV with the server name first
Script for single computer (getting results we want):
$ServerName = “server01”
Get-WmiObject win32_service -ComputerName $servername | Select #{N="ServerName";E={$ServerName}}, Name, Status, StartMode, StartName, description | Export-Csv "server_Services.csv" -NoTypeInformation
Script for querying OU (only returning services with local accounts):
$ServerOU = 'OU=Servers,DC=some,DC=domain'
$ServerList = Get-QADComputer -SearchRoot $ServerOU -SearchScope Subtree
$ServerList | ForEach {
$ServerNameTemp = $_.Name
Get-WMIObject Win32_Service |
Select #{N="ServerName";E={$ServerNameTemp}}, StartName, Name, StartMode, Status, description
} | Export-Csv -NoTypeInformation $ExportFile
This should do the trick i think but th following would wanna make a file for each server in the OU, it would keep overwriting the same file.
I also Changed Get-QADComputer into Get-ADComputer no specific need for quest powershell for this one :)
$ServerOU = 'OU=Servers,DC=some,DC=domain'
$ServerList = Get-ADComputer -SearchBase $ServerOU -Filter *
ForEach ($Server in $Serverlist)
{
Invoke-Command -ComputerName $Server.Name -ScriptBlock {
Get-WMIObject Win32_Service |
Select #{N="ServerName";E={[System.Net.Dns]::GetHostName()}}, StartName, Name, StartMode, Status, description}
} | Export-Csv -NoTypeInformation $ExportFile
This way it would send ti all to a variable first and then to a file.
$ServerOU = 'OU=Servers,DC=some,DC=domain'
$Services = #()
$ServerList = Get-ADComputer -SearchBase $ServerOU -Filter *
ForEach ($Server in $Serverlist)
{
$Services += Invoke-Command -ComputerName $Server.Name -ScriptBlock {
Get-WMIObject Win32_Service |
Select-Object #{N="ServerName";E={[System.Net.Dns]::GetHostName()}}, StartName, Name, StartMode, Status, description }
}
$Services | Export-Csv -NoTypeInformation $Exportfile
PS Remoting is automatically enabled from win 2012. If you are using any Win servers prior to that, you need to enable to WinRM services, to enable powershell remoting. This is very important, if you are trying to use "Invoke-Command" or any of those "*-Session" cmdlets against those servers.