none
pipe cmdlet output into a variable and a csv file RRS feed

  • Question

  • Currently I am doing this in my PowerShell script:

        $ServiceTagsPath=$filePath + '\DellAPIWarrantyLIST.csv'
        
        write-host 'get all computer names from Active Directory...for Windows 7...'
        Get-ADComputer -properties * -filter {(operatingsystem -like "*Windows 7*")} |
            Where-Object {$_.name -like "*-*"} |
            Where-Object {$_.name -NotLike "V7-*"} |
            Where-Object {$_.name -NotLike "*-NONE"} |
            Where-Object {$_.name -NotLike "*-ONCALL"} |
            Where-Object {$_.name -NotLike "*-BLACKBAUD"} |
            Where-Object {$_.name -NotLike "SC-WIN7-1"} |
            Where-Object {$_.name -NotLike "UT-SWCLIENT-01"} |
            Select-Object -property Name , LastLogonDate | export-csv $ServiceTagsPath -NoTypeInformation -Force
        
        $computers= Get-ADComputer -properties * -filter {(operatingsystem -like "*Windows 7*")} |
            Where-Object {$_.name -like "*-*"} |
            Where-Object {$_.name -NotLike "V7-*"} |
            Where-Object {$_.name -NotLike "*-NONE"} |
            Where-Object {$_.name -NotLike "*-ONCALL"} |
            Where-Object {$_.name -NotLike "*-BLACKBAUD"} |
            Where-Object {$_.name -NotLike "SC-WIN7-1"} |
            Where-Object {$_.name -NotLike "UT-SWCLIENT-01"} |
            Select-Object -Expand Name                           
        
        Write-Host $computers.Length + ' computers found in Active Directory...'



    The first one gives me a csv file with 2 columns, and about 1500 records, the second one gives me an array variable which I use in web-service calls to an API...

    But would it be possible to do both in one step?
    Tuesday, April 17, 2018 3:01 PM

Answers

  • See my answer on stackoverflow.

    https://stackoverflow.com/questions/49881519/

    It is much faster to specify what you want in the query rather than using Where-Object. Example:


    $outputFilename = Join-Path $filePath "DellAPIWarrantyLIST.csv"
    Get-ADComputer -LDAPFilter "(&(operatingSystem=*Windows 7*)(name=*-*)(!name=*-none)(!name=*-oncall)(!name=*-blackbaud)(!name=sc-win7-1)(!name=ut-swclient-01))" -Property LastLogonDate |
      Select-Object Name,LastLogonDate |
      Export-Csv $outputFilename -NoTypeInformation
    $outputCount = (Import-Csv $outputFilename | Measure-Object).Count
    Write-Host ("Found "{0} computer(s)" -f $outputCount)
    if ( $outputCount -eq 0 ) {
      Remove-Item $outputFilename
    }
    


    -- Bill Stewart [Bill_Stewart]

    Tuesday, April 17, 2018 3:45 PM
    Moderator

All replies

  • You can use PowerShell to get computers easily and use the results in many mysterious ways.  Why do it twicw when you can do it like this:

    $ServiceTagsPath = Join-Path $filePath 'DellAPIWarrantyLIST.csv'
        
    write-host 'get all computer names from Active Directory...for Windows 7...'
    $computers = Get-ADComputer -properties LastLogonDate -filter {operatingsystem -like '*Windows 7*'} |
        Where-Object {$_.name -match '-' -and $_.Name -notmatch 'V7-|-NONE|-ONCALL|-BLACKBAUD|SC-WIN7-1|UT-SWCLIENT-01' }
        
    $computers | Select-Object Name , LastLogonDate | Export-Csv $ServiceTagsPath -NoTypeInformation
    $computers | Select-Object -Expand Name 
    


    \_(ツ)_/


    • Edited by jrv Tuesday, April 17, 2018 3:32 PM
    Tuesday, April 17, 2018 3:30 PM
  • See my answer on stackoverflow.

    https://stackoverflow.com/questions/49881519/

    It is much faster to specify what you want in the query rather than using Where-Object. Example:


    $outputFilename = Join-Path $filePath "DellAPIWarrantyLIST.csv"
    Get-ADComputer -LDAPFilter "(&(operatingSystem=*Windows 7*)(name=*-*)(!name=*-none)(!name=*-oncall)(!name=*-blackbaud)(!name=sc-win7-1)(!name=ut-swclient-01))" -Property LastLogonDate |
      Select-Object Name,LastLogonDate |
      Export-Csv $outputFilename -NoTypeInformation
    $outputCount = (Import-Csv $outputFilename | Measure-Object).Count
    Write-Host ("Found "{0} computer(s)" -f $outputCount)
    if ( $outputCount -eq 0 ) {
      Remove-Item $outputFilename
    }
    


    -- Bill Stewart [Bill_Stewart]

    Tuesday, April 17, 2018 3:45 PM
    Moderator