Showing posts with label WMI. Show all posts
Showing posts with label WMI. Show all posts

Thursday, July 13, 2017

Get All Windows Services account that are configured to use a domain account in the trusted network

Hi
This small script will get all the services that are configured with a RunAs account
The service will be using several possible accounts like LocalSystem , Network Services.. and also a domain account.
The common thing is the service that will be using a domain account should have the UPN (FQDN) or SamAccountName (NetBIOS)

$Netbios=(Get-ADDomain).NetBIOSName #Get the Domain NetBIOS Name
$fqdn=(Get-ADDomain).dnsroot #Get the Domain FQDN Name

#The WMI Query that will be used
$WMIQuery="select * from Win32_Service where startname like '$Netbios%' or startname like '%$fqdn'"

#Getting computer list from AD, you can use the filter that fit your criteria, in my case, I have used a computer name as my filter criteria, you can use the search base.
Then I am executing the gwmi Get-WMIObject on the computer I got from the pipeline 
Get-ADComputer -Filter {name -like "*MyServers*"} | foreach {gwmi -ea SilentlyContinue -ComputerName $_.DNSHostName -Query$WMIQuery}  |ft -AutoSize SystemName,caption,startname 

The result should be something like this.

SystemName                       caption                                         startname        
----------                               -------                                             ---------        

HQ-SRV-N1       SQL Server Reporting Services                  Domain\report

Thursday, May 1, 2014

Deep in side DNS Server, DNS and WMI

All Microsoft Windows System Administrator know good information about DNS Server and how it work, and how to maintain it. but  do you really think that you know much.
There are a lot of hidden information in DNS Server that are not in GUI, these information can obtain using DNSCMD or WMI.
Today I will mention some of the hidden commands in Microsoft DNS (Namespace "MicrosoftDNS")
To make the query I will use PowerShell, So lets start:
Note: if you dont know much in powershell and want to use these scripts, then you can copy the script text and paste it to notepad, Save it as .ps1.
Open Powershell and then navigate to the script path and call it.
If you get an error regarding for execution policy, then use this command to first
Set-executionpolicy remotesigned

Get DNS Server Statistics
cls              
Param(            
[parameter(Mandatory=$True)]            
[string]$ServerName            
)            
Get-WmiObject -Class "MicrosoftDNS_Statistic" -Namespace "Root\MicrosoftDNS" -ComputerName $ServerName |Where{$_.value -gt 0} | FT DnsServerName,CollectionName,name,Value            
The Return result will show you a lot of details regarding for your DNS, like Timeout, Packets sent, LDAP information, Connection refused.

Get a list of all cached Domains in your DNS Server
In some case you may need to know which domains your DNS Server is caching. check this script

cls            
Param(            
[parameter(Mandatory=$True)]            
[string]$ServerName,            
[parameter(Mandatory=$True)]            
$DomainToExclude            
)            
Get-WmiObject -Class "MicrosoftDNS_AType" -Namespace "Root\MicrosoftDNS" -ComputerName $ServerName | Where{$_.domainname -notlike "*$DomainToExclude"} | select domainname,IPAddress,OwnerName}


The result will be a list of all the cached domains and its IP, This list may take some time to load if your DNS is loaded.

Read DS Polling Interval

When DNS Service Stored in AD, DNS Read AD information every 180 sec, To get these information.
Param(            
[parameter(Mandatory=$true)]            
$ServerName            
)
Get-WmiObject -Class "MicrosoftDNS_Server" -Namespace "Root\MicrosoftDNS" -ComputerName $ServerName | FT -AutoSize @{N="Polling Interval/ Sec";E={$_.DsPollingInterval}}


Change DS Polling Interval
If you want to change this value, you can use the following script

param(            
[parameter(Mandatory=$true)]            
$ServerName,            
[parameter(Mandatory=$true)]            
[int]$NewInterval            
)
$DSInterval=Get-WmiObject  -Namespace "Root\MicrosoftDNS" -Class "MicrosoftDNS_Server" -ComputerName $ServerName            
write-host "Old Value was "$DSInterval.DsPollingInterval            
$DSInterval.DsPollingInterval=$NewInterval            
$DSInterval.Put() |Out-Null            
write-host "New Value was "$DSInterval.DsPollingInterval            
}

Change DNS Error Log Level

If you want to change how much of events are written in Windows Eventlog, then you can take the control from here        
#0 None.            
#1 Log only errors.            
#2 Log only warnings and errors.            
#4 Log all events.            
            
Param(            
[parameter(Mandatory=$true)]            
$ServerName,            
[parameter(Mandatory=$true)]            
[int]$LoggingLevel            
)            
            
$x=Get-WmiObject -Namespace "root\microsoftdns" -Class "MicrosoftDNS_Server"            
$x.EventLogLevel=$LoggingLevel            
$x.Put() |Out-Null            
Write-Host "The Current Logging level is set to "$x.EventLogLevel            

I hope you like this..if so please share it or comment or like or what ever you want .
Thanks for reading

Wednesday, February 5, 2014

Install IP Printer using Powershell

Good day,
You may want to install an IP Printer to several computers and install it locally using TCP/IP Port, in this case Windows GPO will not work as the printer that is pushed for the users is the shared printer and is not installed directly to the client computer.
This is the way to do it.
Usually if you notice that steps you made in the GUI TCP/IP Printer installation wizard, you first create the port and then select the printer that will use this port, and this is what we will do.
First we need to create the IP Port, to do this. we will need to use WMIClass called Win32_TcpIpPrinterPort


Function PortInstall {            
param ($PortName,$PrinterIP,$servername)            
            
$PPrinter=([WMIClass]"\\.\ROOT\cimv2:Win32_TcpIpPrinterPort").CreateInstance()            
$PPrinter.name           = $PortName            
$PPrinter.Protocol       = 1            
$PPrinter.HostAddress    = $PrinterIP            
$PPrinter.PortNumber     = 9100            
$PPrinter.Put()            
            
}            

PortInstall -PortName "1stFloorPrinter" -PrinterIP "192.168.1.1"

This Function will allow you to create IP Port named as 1stFloorPrinter with IP 192.168.1.1

Now the port is created and we need to Add the printer and we will use the WMIClass Win32_Printer.
Let remmeber what we steps we do on the normal IP Printer installation.

- Select the printer Driver
- Assigning the printer to the port


Function Printerinstall {            
param ($caption,$PortName,$DriverName,$IsDefault=$false)             
            
$iprinter = ([WMIClass]"\\.\Root\cimv2:Win32_Printer").CreateInstance()            
$iprinter.Caption     =$caption            
$iprinter.DriverName  =$DriverName            
$iprinter.PortName    =$PortName            
$iprinter.DeviceID    =$caption            
$iprinter.Default     = $IsDefault            
$iprinter.Put()            


}            
Printerinstall -caption "First Floor" -PortName "1stFloorPrinter" -IsDefault $true -DriverName "HP LaserJet P3011/P3015 PCL6"

The Caption is the Name of the printer that the user will see and use
The PortName is the Port Name you create on the first set "1stFloorPrinter"
If you want to make the printer default then you can set the IsDefault to $True, other wise it will not be default.
and the DriverName is the Driver name as it appear in the INF File or in the Add New Printer Hardware list .

I hope you like this and find it useful.
Feel free to leave your comment and share this page.