0

I'm new to scripting so please excuse me if my script is messy. This script pretty much does what I want it to do but for 2 fields it doesn't return the values.

If I run the commands without Invoke I get all the values I want but when I run this with the Invoke command on remote computers the OsHotFixes and CsProcessors return weird values of "Microsoft.PowerShell.Commands.HotFix" for each hotfix and "Microsoft.PowerShell.Commands.Processor" for the CsProcessors value. All other properties gave me the values I am looking for. I'm not sure why those 2 aren't returning correct values. If someone could point me in the right direction that would be awesome.

$c = Get-Content "myfilepath"
$e = "myfilepath"
$ScriptBlock = {
$ComputerInfo = Get-ComputerInfo -Property WindowsVersion, OsBuildNumber, OsHotFixes, CsModel, BiosSMBIOSBIOSVersion, WindowsProductName, CsProcessor, OsInstallDate, OsArchitecture, CsProcessors
$GPU = Get-WmiObject win32_VideoController | Select-Object "Name", "DeviceID", "DriverVersion"
$RAM = Get-CimInstance -ClassName CIM_PhysicalMemory | Select-Object "Manufacturer", "PartNumber", @{'Name'='Capacity (GB)'; 'Expression'={[math]::Truncate($_.capacity / 1GB)}}, "Speed"
$Storage = Get-WmiObject Win32_LogicalDisk | Where caption -eq "C:" | Foreach-object {write " $($_.caption) $('{0:N2}' -f ($_.Size/1gb)) GB total, $('{0:N2}' -f ($_.FreeSpace/1gb)) GB Free"}
$MyArray = @($ComputerInfo, $GPU, $RAM, $Storage)
$Properties =
    @(
        'WindowsVersion'
        'OsBuildNumber'
        'OsHotFixes'
        'CsModel'
        'BiosSMBIOSBIOSVersion'
        'WindowsProductName'
        'OsInstallDate'
        'OsArchitecture'
        'CsProcessors'
        'Name'
        'DeviceID'
        'DriverVersion'
        'Manufacturer'
        'PartNumber'
        'Capacity'
        'Speed'
        'Disk'

    )

$MyArray | ForEach-Object {
        :Inner ForEach( $Property in $Properties )
        {
            If($_.$Property)
            {
                [PSCustomObject][Ordered]@{
                    hostname                = $env:COMPUTERNAME
                    WindowsVersion          = $_.WindowsVersion
                    Build                   = $_.OsBuildNumber
                    Patches                 = $_.OsHotFixes
                    Motherboard             = $_.CsModel
                    BiosVersion             = $_.BiosSMBIOSBIOSVersion
                    WindowsProductName      = $_.WindowsProductName 
                    OsInstallDate           = $_.OsInstallDate
                    OsArchitecture          = $_.OsArchitecture
                    Processor               = $_.CsProcessors
                    GPUName                 = $_.Name
                    DeviceID                = $_.DeviceID
                    DriverVersion           = $_.DriverVersion
                    RamManufacturer         = $_.Manufacturer
                    PartNumber              = $_.PartNumber
                    Capacity                = $_.Capacity
                    Speed                   = $_.Speed
                    Disk                    = $Storage
                }
            Break Inner
            }
        }
    }
}
Invoke-Command -ComputerName $c -ScriptBlock $ScriptBlock | Sort hostname | Export-Csv -append $e -NoTypeInformation

I've tried running just the lines from 4 - 8 locally and then Outputting the Array. This will show all correct values. However when this script runs with the PSCustomObject and Invoke command I don't get CsProcessors or OsHotFixes values.

  • Take a look at the help files regarding function and variable scopes, when using PSRemoting. Specifically, focus on ```$using```, or ```-ArgumentList``` options. – postanote Nov 26 '22 at 01:35

0 Answers0