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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Redefine Your Security with AI & Machine Learning

The implications of AI and machine learning in cyber security are massive and constantly growing, creating both efficiencies and new challenges across the board. Check out our on-demand webinar to learn more about how AI can help your organization!

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.