PowerShell Question

I am trying to get basic computer information form powershell and output to a log file. I can't get it to output properly.

I am trying to get

Computer Name
Domain
Free Space
IP Address
gateway


I want these to all be outputted to a log file in a useable list but I can't seem to get it working. Mind you i am very new tp powershell.

Any help is appreciated.
LVL 3
Josef Al-ChacarSystems AdministratorAsked:
Who is Participating?
 
Dorababu MSenior Software EngineerCommented:
Try the below one
[cmdletbinding()]
param (
 [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
    [string[]]$ComputerName = $env:computername
)
 
begin {}
process {
 foreach ($Computer in $ComputerName) {
  if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
   try {
    $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer -EA Stop | ? {$_.IPEnabled}
   } catch {
        Write-Warning "Error occurred while querying $computer."
        Continue
   }
   foreach ($Network in $Networks) {
    $IPAddress  = $Network.IpAddress[0]
    $SubnetMask  = $Network.IPSubnet[0]
    $DefaultGateway = $Network.DefaultIPGateway
    $DNSServers  = $Network.DNSServerSearchOrder
    $WINS1 = $Network.WINSPrimaryServer
    $WINS2 = $Network.WINSSecondaryServer   
    $WINS = @($WINS1,$WINS2)         
    $IsDHCPEnabled = $false
    If($network.DHCPEnabled) {
     $IsDHCPEnabled = $true
    }
    $MACAddress  = $Network.MACAddress
    $OutputObj  = New-Object -Type PSObject
    $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
    $OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask
    $OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value ($DefaultGateway -join ",")      
    $OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
    $OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value ($DNSServers -join ",")     
    $OutputObj | Add-Member -MemberType NoteProperty -Name WINSServers -Value ($WINS -join ",")        
    $OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
    $OutputObj
   }
  }
 }
}
 
end {}

Open in new window

0
 
Justin EvansCommented:
Hi,  

Try this URL https://docs.microsoft.com/en-us/sysinternals/downloads/pstools

it includes a executable called Psinfo which when run will give you the information you are after.

hope this helps

Justin
0
 
Josef Al-ChacarSystems AdministratorAuthor Commented:
Thanks but it needs to be done in powershell. To put psinfo on every server (2300 of them) would need some serious approval.
0
How do you know if your security is working?

Protecting your business doesn’t have to mean sifting through endless alerts and notifications. With WatchGuard Total Security Suite, you can feel confident that your business is secure, meaning you can get back to the things that have been sitting on your to-do list.

 
Josef Al-ChacarSystems AdministratorAuthor Commented:
AWESOME!! How would I export that into a log file?
0
 
Dorababu MSenior Software EngineerCommented:
You can try this
$OutputObj | Out-File -filepath C:\process.log

Open in new window

0
 
Josef Al-ChacarSystems AdministratorAuthor Commented:
That worked perfectly. Last question I swear lol. I want to write that log file to the event log. Either that or just send the results to the event log.

I see the Write-Eventlog cmdlet but I can't seem to get to to pull from the file.
0
 
Dorababu MSenior Software EngineerCommented:
You have to use
New-EventLog

Open in new window

if you are creating a new
New-EventLog –LogName Application –Source “My Script”

Open in new window

0
 
Josef Al-ChacarSystems AdministratorAuthor Commented:
Right it looks like that sets which logs to send it to and the source. I don't see how to add the results to the event log though.
0
 
Josef Al-ChacarSystems AdministratorAuthor Commented:
Nevermind I figured it out. Thank you, you have been a huge help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.