Inventorying using Windows Remoting

Ask your PowerShell-related questions, including questions on cmdlet development!
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.
This topic is 10 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
juneb1022
Posts: 34
Last visit: Fri Sep 30, 2016 8:08 am

Inventorying using Windows Remoting

Post by juneb1022 »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Inventorying using Windows Remoting

Post by jvierra »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Inventorying using Windows Remoting

Post by jvierra »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Inventorying using Windows Remoting

Post by jvierra »

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
User avatar
juneb1022
Posts: 34
Last visit: Fri Sep 30, 2016 8:08 am

Re: Inventorying using Windows Remoting

Post by juneb1022 »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Inventorying using Windows Remoting

Post by jvierra »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Inventorying using Windows Remoting

Post by jvierra »

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
User avatar
juneb1022
Posts: 34
Last visit: Fri Sep 30, 2016 8:08 am

Re: Inventorying using Windows Remoting

Post by juneb1022 »

You are a fast powershell scripter. Someday...I hope to tbe that speedy.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Inventorying using Windows Remoting

Post by jvierra »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Inventorying using Windows Remoting

Post by jvierra »

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
This topic is 10 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked