none
Simple PowerShell output formatting RRS feed

  • Question

  • Import-Module ActiveDirectory

    Get-ADComputer -SearchBase 'OU=myou2,OU=myou1,dc=mydomain,dc=local' -Filter '*' | `

    Select -Exp Name | %{echo $_ ; Get-WMIObject -Class Win32_BIOS -ComputerName $_| Format-List SerialNumber; `

    $monitor = gwmi WmiMonitorID -Namespace root\wmi -computername $_;($monitor.SerialNumberID -notmatch 0 | foreach {[char]$_}) -join ""};

    PowerShell neophyte here. I managed to hack this little line together from various scripts I found here and on other forums.

    It works fine, but the output is messy. It appears with a lot of unnecessary linebreaks and the SerialNumber label:

    COMPUTERNAME
    
    
    SerialNumber : COMPUTERSERIAL
    
    
    
    MONITORSERIAL

    I'd like the output to simply be:

    COMPUTERNAME
    COMPUTERSERIAL
    MONITORSERIAL

    How would I go about this?

    Thanks for reading.



    Friday, August 21, 2015 11:04 AM

Answers

  • Hello,

    You can obtain it by storing data to object instead of retrieveing raw data.

    The following script should produce your goals :

    Import-Module ActiveDirectory; 
    $Computers = Get-ADComputer -SearchBase 'OU=myou2,OU=myou1,dc=mydomain,dc=local' -Filter '*' | Select -Exp Name;
    
    
    $Inventory = $Computers | %{
        $MonitorSerialNumber = $null;
        $(gwmi WmiMonitorID -Namespace root\wmi -ComputerName $_).SerialNumberID | %{[string]$MonitorSerialNumber += $_.toString() }
        New-Object -TypeName psobject -Property @{
            'computerName'=$_;
            'ComputerSerialNumber'= $(Get-WMIObject -ComputerName $_ -Class Win32_BIOS).SerialNumber;
            'MonitorSerialNumber'=$MonitorSerialNumber
        }
    };
    
    $Inventory | fl
    
    1. We import ActiveDirectory PowerShell Module and store a list of computers to assess ($computers).
    2. Foreach computer we retrieve monitorSerialNumber and store it in $MonitorSerialNumber variable
    3. Foreach computer we create an object with 3 properties : ComputerName,ComputerSerialNumber and MonitorSerialNumber.
    4. Once object created, we add it to $inventory which is a liste of objecte
    5. Finally we display $inventory as a list to obtain thé following résult

    Regards,

    Régis

    Friday, August 21, 2015 11:35 AM

All replies

  • Hello,

    You can obtain it by storing data to object instead of retrieveing raw data.

    The following script should produce your goals :

    Import-Module ActiveDirectory; 
    $Computers = Get-ADComputer -SearchBase 'OU=myou2,OU=myou1,dc=mydomain,dc=local' -Filter '*' | Select -Exp Name;
    
    
    $Inventory = $Computers | %{
        $MonitorSerialNumber = $null;
        $(gwmi WmiMonitorID -Namespace root\wmi -ComputerName $_).SerialNumberID | %{[string]$MonitorSerialNumber += $_.toString() }
        New-Object -TypeName psobject -Property @{
            'computerName'=$_;
            'ComputerSerialNumber'= $(Get-WMIObject -ComputerName $_ -Class Win32_BIOS).SerialNumber;
            'MonitorSerialNumber'=$MonitorSerialNumber
        }
    };
    
    $Inventory | fl
    
    1. We import ActiveDirectory PowerShell Module and store a list of computers to assess ($computers).
    2. Foreach computer we retrieve monitorSerialNumber and store it in $MonitorSerialNumber variable
    3. Foreach computer we create an object with 3 properties : ComputerName,ComputerSerialNumber and MonitorSerialNumber.
    4. Once object created, we add it to $inventory which is a liste of objecte
    5. Finally we display $inventory as a list to obtain thé following résult

    Regards,

    Régis

    Friday, August 21, 2015 11:35 AM
  • Answered my own question:

    Select-Object -expand

    This cmd extracts the property without any formatting.

    This works:

    Import-Module ActiveDirectory
    
    Get-ADComputer -SearchBase 'OU=myou2,OU=myou1,dc=mydomain,dc=local' -Filter '*' | `
    
    Select -Exp Name | %{echo $_ ; Get-WMIObject -Class Win32_BIOS -ComputerName $_| Select-Object -expand SerialNumber; `
    
    $monitor = gwmi WmiMonitorID -Namespace root\wmi -computername $_;($monitor.SerialNumberID -notmatch 0 | foreach {[char]$_}) -join ""


    Friday, August 21, 2015 11:41 AM
  • Thanks Régis for your quick response. Your solution is ultimately much better and tidier than mine (and I'll use it as a basis for more scripts!)

    Friday, August 21, 2015 11:42 AM