Powershell wont output infomation

Welcome Forums General PowerShell Q&A Powershell wont output infomation

This topic contains 8 replies, has 3 voices, and was last updated by

 
Keymaster
3 months, 3 weeks ago.

  • Author
    Posts
  • #104954

    Participant
    Points: 0
    Rank: Member

    $ErrorActionPreference = 'SilentlyContinue'

    $ComputerName =Get-ADComputer -Filter {(Name -like "*")} -SearchBase "OU=AsiaPacific,OU=Sales,OU=UserAccounts,DC=FABRIKAM,DC=COM" | Select-Object -ExpandProperty Name

    $results = @{}

    ForEach ($computer in $ComputerName) {

    $Results += Get-NetAdapter -CimSession $ComputerName | Select-Object PsComputerName, InterfaceAlias, Status, MacAddress

    }

    $results | Export-csv -path C\users\bret.hooker\desktop\macaddress.csv -Append

    Please note the base and filter are just examples and not the actual code due to work place confidentiality. Code currently will pull from AD all computer name, then will run the ForEach command to get the NetAdapter Information. I am unable to get it to output to the CSV file however. Any advice would be great.

  • #104962

    Keymaster
    Points: 1,625
    Helping HandTeam Member
    Rank: Community Hero

    Yeah, so, there's an important pattern in PowerShell that you're not following, which is making things harder on you. See https://devops-collective-inc.gitbook.io/the-big-book-of-powershell-gotchas/accumulating-output-in-a-function. You're also not using -CimSession correctly.

    Function Get-NetAdapterInfo {
     [CmdletBinding()]
     Param(
      [Parameter(Mandatory=$True)]
      [string]$SearchBase
     )
     
     Get-ADComputer -Filter * -SearchBase $SearchBase |
     Select -Expand Name |
     ForEach-Object {
      $session = New-CimSession -ComputerName $_
      Get-NetAdapter -CimSession $session | 
      Select-Object PsComputerName, InterfaceAlias, Status, MacAddress
     }
    
    }
    
    Get-NetAdapterInfo -searchbase "ou=whatever,dc=domain,dc=com" | Export-CSV Whatever.csv
    

    The idea is to NOT accumulate output into an array. That's contrary to how PowerShell's pipeline model works. So, I've written a function that simply lets the output into the pipeline one object at a time. I can then run that function and pipe its output to Export-CSV or whatever I like.

    I suspect you're not getting anything as-is because you're not using -CimSession correctly. It doesn't accept a computer name; it accepts a CIM Session.

    • #104963

      Participant
      Points: 0
      Rank: Member

      in your code, what items do i need to change as i do not currently understand what you have written. I attempted to replace[String] in the $searchBase and now it just declares missing function perimeter list

    • #105004

      Participant
      Points: 165
      Helping Hand
      Rank: Participant

      From what I recall, doesn't New-CimSession take an array of computernames as input? It should be possible to create session(s) for all machines at once, then feed that to Get-NetAdapter to do everything at the same time, rather than introducing unnecessary overhead with a loop.

    • #105007

      Keymaster
      Points: 1,625
      Helping HandTeam Member
      Rank: Community Hero

      Yeah, you totally could.

    • #105011

      Keymaster
      Points: 1,625
      Helping HandTeam Member
      Rank: Community Hero

      So...

       $SearchBase = "OU=AsiaPacific,OU=Sales,OU=UserAccounts,DC=FABRIKAM,DC=COM"
      
       $sessions = New-CimSession -ComputerName (
       Get-ADComputer -Filter * -SearchBase $SearchBase |
       Select -Expand Name)
      
       Get-NetAdapter -CimSession $sessions | 
       Select-Object PsComputerName, InterfaceAlias, Status, MacAddress |
       Export-CSV whatever.csv
      

      Certainly valid. It'd be easier to add error-handling to it the way I originally had it, since you could trap any failed sessions individually and log them, but this also works if nothing goes wrong with the session creation.

  • #104969

    Keymaster
    Points: 1,625
    Helping HandTeam Member
    Rank: Community Hero

    You don't need to change anything. That's the delight of parameters.

    Get-NetAdapterInfom -SearchBase "OU=AsiaPacific,OU=Sales,OU=UserAccounts,DC=FABRIKAM,DC=COM" | Export-CSV Whatever.csv

    • #104974

      Participant
      Points: 0
      Rank: Member

      when i run the powershell it requests values for searchbase

    • #105010

      Keymaster
      Points: 1,625
      Helping HandTeam Member
      Rank: Community Hero

      If it is prompting you for a value, then give it the value it is prompting you for. That's why it's prompting you.

The topic ‘Powershell wont output infomation’ is closed to new replies.