HI -
Because of ports not being open (wmi is not accessible), WinRemoting with Posh is the only way to pull the data needed. That said, the script works great when pulling the win32_operatingsystem. When I add TimeZone, Physical Memory, and Network Adapter, I get blank columsn except for __server, Operating system, Service Pack and LastBoot Time.
Any guidance is appreciated.
###########################################
#
# Title: Inventory.ps1............
#
#
###########################################
Clear-Host
# Assign variables
$InventoryCSVa = "D:code_testing_inventorya.csv"
$RemoteServerSessions = "D:\RemoteSrvSessions.txt"
$InventoryCSVb= "code_testing_inventoryb.csv"
# Retrieve most recent list from AD
$servers = Get-QADComputer -searchroot 'somedomain.com/ou/ou' |
select-object name,description,managedby | sort description | Export-Csv $InventoryCSVa -NoTypeInformation
# Import CSV file and extract just the server names
Import-Csv $InventoryCSVa | select Name | Export-Csv -Path $RemoteServerSessions -NoTypeInformation -Force -Encoding ascii
#File Cleanup for remote connections
( get-content $RemoteServerSessions ) | foreach {$_ -replace "`"",""} | Set-Content $RemoteServerSessions
$sessions = get-content $RemoteServerSessions | new-PSSession
$block = {
gwmi win32_operatingsystem | select __SERVER,@{name='LastBootTime';expression={$_.ConvertToDateTime($_.LastBootupTime)}} ,@{name='Operating System'; expression={$_.Caption}},` @{name='Service Pack';expression={$_.CSDVersion}}
gwmi win32_TimeZone | Select StandardName
gwmi win32_PhysicalMemory | Measure-Object -Property capacity -Sum | select @{N="Total Physical Ram"; E={[math]::round(($_.Sum / 1GB),2)}}
gwmi win32_NetworkAdapterConfiguration | where{$_.IPEnabled -eq "True"} | select IPAddress
}
[string]$body = Invoke-Command -ScriptBlock $block -Session $sessions | Select __Server,'Operating System','Service Pack','LastBootTime','StandardName','TotalPhysicalRam','IP Address' | sort __Server | Export-Csv $InventoryCSVb -NoTypeInformation
get-pssession | Remove-PSSession
Send-MailMessage -SmtpServer "x.x.x.x" -From "someone.com" -To "someone@.com" -Subject "Inventory" -Body $body -BodyAsHtml
Inventorying using Windows Remoting
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Re: Inventorying using Windows Remoting
You need to rethink and retest your powershell code. It does not work the way you think. YOU are returning many objects with different structures so they will all be limited by the values of the first object returns. You need to return a single structured object.
In other words - you cannot just glue arbitrary code together.
Create a method to run all of the code locally and you will see what I mean.
In other words - you cannot just glue arbitrary code together.
Create a method to run all of the code locally and you will see what I mean.
Re: Inventorying using Windows Remoting
Run the following and look at what is output.
PowerShell Code
Double-click the code block to select all.function Test-Code{ gwmi win32_operatingsystem | select __SERVER,@{name='LastBootTime';expression={$_.ConvertToDateTime($_.LastBootupTime)}} ,@{name='Operating System'; expression={$_.Caption}},` @{name='Service Pack';expression={$_.CSDVersion}} gwmi win32_TimeZone | Select StandardName gwmi win32_PhysicalMemory | Measure-Object -Property capacity -Sum | select @{N="Total Physical Ram"; E={[math]::round(($_.Sum / 1GB),2)}} gwmi win32_NetworkAdapterConfiguration | where{$_.IPEnabled -eq "True"} | select IPAddress } Test-Code | fl
Re: Inventorying using Windows Remoting
Now run it with your query:
PowerShell Code
Double-click the code block to select all.function Test-Code{ gwmi win32_operatingsystem | select __SERVER,@{name='LastBootTime';expression={$_.ConvertToDateTime($_.LastBootupTime)}} ,@{name='Operating System'; expression={$_.Caption}},` @{name='Service Pack';expression={$_.CSDVersion}} gwmi win32_TimeZone | Select StandardName gwmi win32_PhysicalMemory | Measure-Object -Property capacity -Sum | select @{N="Total Physical Ram"; E={[math]::round(($_.Sum / 1GB),2)}} gwmi win32_NetworkAdapterConfiguration | where{$_.IPEnabled -eq "True"} | select IPAddress } Test-Code | Select __Server,'Operating System','Service Pack','LastBootTime','StandardName','TotalPhysicalRam','IP Address' | sort __Server
Re: Inventorying using Windows Remoting
I get it, but I think my wording was wrong. I dont expect it to work by gluing. How do I get the below code to add to the columns in the spreadsheet? Do I need to define the server column on each line?
gwmi win32_TimeZone | Select StandardName
gwmi win32_PhysicalMemory | Measure-Object -Property capacity -Sum | select @{N="Total Physical Ram"; E={[math]::round(($_.Sum / 1GB),2)}}
gwmi win32_NetworkAdapterConfiguration | where{$_.IPEnabled -eq "True"} | select IPAddress
gwmi win32_TimeZone | Select StandardName
gwmi win32_PhysicalMemory | Measure-Object -Property capacity -Sum | select @{N="Total Physical Ram"; E={[math]::round(($_.Sum / 1GB),2)}}
gwmi win32_NetworkAdapterConfiguration | where{$_.IPEnabled -eq "True"} | select IPAddress
Re: Inventorying using Windows Remoting
Now try this way:
PowerShell Code
Double-click the code block to select all.function Test-Code{ $os=gwmi win32_operatingsystem $cp=gwmi win32_computersystem $adapter=gwmi win32_NetworkAdapterConfiguration -Filter 'IPEnabled=True' # gather the things we like $props=@{ Server=$os.CSName LastBootTIme=$os.ConvertToDateTime($os.LastBootupTime) OperatingSystem=$os.Caption ServicePack=$os.CSDVersion PhysicalMemory=$cp.TotalPhysicalMemory IPAddress=$adapter.IPAddress TimeZone=$tz.Caption } New-Object PsObject -Property $props } Test-Code | fl
Re: Inventorying using Windows Remoting
Now we take it for a remote spin. This is the proof of the methods. We will get back a single object per system and all objects will be identical.
PowerShell Code
Double-click the code block to select all.$sb={ function Test-Code{ $os=gwmi win32_operatingsystem $cp=gwmi win32_computersystem $tz=gwmi Win32_TimeZone $adapter=gwmi win32_NetworkAdapterConfiguration -Filter 'IPEnabled=True' # gather the things we like $props=@{ Server=$os.CSName LastBootTIme=$os.ConvertToDateTime($os.LastBootupTime) OperatingSystem=$os.Caption ServicePack=$os.CSDVersion PhysicalMemory=$cp.TotalPhysicalMemory IPAddress=$adapter.IPAddress TimeZone=$tz.Caption } New-Object PsObject -Property $props } Test-Code } invoke-command -computer omega -ScriptBlock $sb
Re: Inventorying using Windows Remoting
You are a fast powershell scripter. Someday...I hope to tbe that speedy.
Re: Inventorying using Windows Remoting
Here is a less conflicted way to do multiple servers.
PowerShell Code
Double-click the code block to select all.$results=Get-QADComputer -searchroot 'somedomain.com/ou/ou' | ForEach-Object{ invoke-command -computer $_.Name -ScriptBlock $sb } $results | Export-Csv file.csv -NoTypeInformation
Re: Inventorying using Windows Remoting
Alternate method which allows for more concurrency (sorry technical term for all-at-once)
PowerShell Code
Double-click the code block to select all.$servers=Get-QADComputer -searchroot 'somedomain.com/ou/ou' | %{$_.Name} $results=invoke-command -computer $servers -ScriptBlock $sb $results | Export-Csv file.csv -NoTypeInformation