Question : I need a Powershell script to read registry and services from a list of hosts in a text file.

I need a Powershell script to read registry and services from a list of hosts in a text file and then output the results to a .csv file. Specifically, for each host listed in list.txt, I need to determine the following things:

1) Query HKLM\software\mhhs and read the data of the string value "BusinessSegment"
2) Query the "ntrtscan" service to see if it's running, stopped, stopping, or whatever
3) Somehow query the uptime of the host and get the uptime in days.

The output should be saved to output.csv so that each row contains the hostname and the other data, all separated by commas. For example:

server1,Marketing,running,35
server2,Business,running,62

The Powershell script should be flexible enough so I can add in more registry and service checks as needed.

Thanks!

Answer : I need a Powershell script to read registry and services from a list of hosts in a text file.

Did notice one thing. Try this one

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
$file = ""
@(foreach($comp in Get-Content $file)
{
    Write-Host " Getting Info for Server [$Comp]" -fore GREEN
    $myobj = "" | Select-Object Name,BusinessSegment,ntrtscan,uptime
    $myobj.Name = $comp
    # Get RegKey
    $regKeyPath = "software\mhhs"
    $HKLM = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $comp)
    $subKey = $HKLM.OpenSubKey($regKeyPath,$false)
    $myobj.BusinessSegment = $subKey.GetValue('BusinessSegment')
    
    # Get Service State
    $myobj.ntrtscan = (Get-WmiObject Win32_Service -ComputerName $comp -filter "Name='ntrtscan'").State
    
    # Get Uptime
    $lastReboot = (Get-WmiObject Win32_OperatingSystem -ComputerName $comp).LastBootUpTime.Split(".")[0]
    $uptime = (Get-Date).Subtract([system.DateTime]::ParseExact($lastReboot,"yyyyMMddHHmmss",$null)).days
    $myobj.uptime = $uptime
    
    # Return Object
    $myobj
}) | export-Csv output.csv -noType
Open in New Window Select All
Random Solutions  
 
programming4us programming4us