Export ADObject data to CSV












0















Basically, I want to create a pscustomobject to correct the headers when ADObject data is exported. I'm not sure if it's possible to pipe a foreach into the code I have, or which way to go exactly. So far The current code I have does export to the csv, but not in the format I'd like it to be.
I have noticed the foreach I currently have only sees the elements in the array, but does not pull the actual data within the element.



Have already tried creating a pscustomobject but it's not outputting the actually data. It just creates the headers.



$ou_Array = $pOU, $lou, $fOU, $vOU, $cOU, $aOU, $auOu
$date = (Get-Date).AddDays(-14)

$get_Data =
(Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[0]),
(Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[1]),
(Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[2]),
(Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[3]),
(Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[4]),
(Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[5]),
(Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[6])

$Results = foreach ($item in $get_Data) {
$DN = $get_Data.Context.PostContext | Where-Object {$_ -match
"DistinguishedName"}
$Name = $get_Data.Context.PostContext | Where-Object {$_ -match 'Name'}
$OC = $get_Data.Context.PostContext |
Where-Object {$_ -match 'ObjectClass'}

[pscustomobject]@{

'DN' = $DN
'Computer Name' = $Name
'Object Class' = $OC
}
}
$Results | Out-File -FilePath "C:Users$env:UserNameDesktopresults1.csv"


Expected results would be corrected headers with the data. Actual results is corrected headers, but no data.










share|improve this question



























    0















    Basically, I want to create a pscustomobject to correct the headers when ADObject data is exported. I'm not sure if it's possible to pipe a foreach into the code I have, or which way to go exactly. So far The current code I have does export to the csv, but not in the format I'd like it to be.
    I have noticed the foreach I currently have only sees the elements in the array, but does not pull the actual data within the element.



    Have already tried creating a pscustomobject but it's not outputting the actually data. It just creates the headers.



    $ou_Array = $pOU, $lou, $fOU, $vOU, $cOU, $aOU, $auOu
    $date = (Get-Date).AddDays(-14)

    $get_Data =
    (Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[0]),
    (Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[1]),
    (Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[2]),
    (Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[3]),
    (Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[4]),
    (Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[5]),
    (Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[6])

    $Results = foreach ($item in $get_Data) {
    $DN = $get_Data.Context.PostContext | Where-Object {$_ -match
    "DistinguishedName"}
    $Name = $get_Data.Context.PostContext | Where-Object {$_ -match 'Name'}
    $OC = $get_Data.Context.PostContext |
    Where-Object {$_ -match 'ObjectClass'}

    [pscustomobject]@{

    'DN' = $DN
    'Computer Name' = $Name
    'Object Class' = $OC
    }
    }
    $Results | Out-File -FilePath "C:Users$env:UserNameDesktopresults1.csv"


    Expected results would be corrected headers with the data. Actual results is corrected headers, but no data.










    share|improve this question

























      0












      0








      0








      Basically, I want to create a pscustomobject to correct the headers when ADObject data is exported. I'm not sure if it's possible to pipe a foreach into the code I have, or which way to go exactly. So far The current code I have does export to the csv, but not in the format I'd like it to be.
      I have noticed the foreach I currently have only sees the elements in the array, but does not pull the actual data within the element.



      Have already tried creating a pscustomobject but it's not outputting the actually data. It just creates the headers.



      $ou_Array = $pOU, $lou, $fOU, $vOU, $cOU, $aOU, $auOu
      $date = (Get-Date).AddDays(-14)

      $get_Data =
      (Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[0]),
      (Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[1]),
      (Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[2]),
      (Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[3]),
      (Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[4]),
      (Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[5]),
      (Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[6])

      $Results = foreach ($item in $get_Data) {
      $DN = $get_Data.Context.PostContext | Where-Object {$_ -match
      "DistinguishedName"}
      $Name = $get_Data.Context.PostContext | Where-Object {$_ -match 'Name'}
      $OC = $get_Data.Context.PostContext |
      Where-Object {$_ -match 'ObjectClass'}

      [pscustomobject]@{

      'DN' = $DN
      'Computer Name' = $Name
      'Object Class' = $OC
      }
      }
      $Results | Out-File -FilePath "C:Users$env:UserNameDesktopresults1.csv"


      Expected results would be corrected headers with the data. Actual results is corrected headers, but no data.










      share|improve this question














      Basically, I want to create a pscustomobject to correct the headers when ADObject data is exported. I'm not sure if it's possible to pipe a foreach into the code I have, or which way to go exactly. So far The current code I have does export to the csv, but not in the format I'd like it to be.
      I have noticed the foreach I currently have only sees the elements in the array, but does not pull the actual data within the element.



      Have already tried creating a pscustomobject but it's not outputting the actually data. It just creates the headers.



      $ou_Array = $pOU, $lou, $fOU, $vOU, $cOU, $aOU, $auOu
      $date = (Get-Date).AddDays(-14)

      $get_Data =
      (Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[0]),
      (Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[1]),
      (Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[2]),
      (Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[3]),
      (Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[4]),
      (Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[5]),
      (Get-ADObject -Filter "created -lt '$date'" -SearchBase $ou_Array[6])

      $Results = foreach ($item in $get_Data) {
      $DN = $get_Data.Context.PostContext | Where-Object {$_ -match
      "DistinguishedName"}
      $Name = $get_Data.Context.PostContext | Where-Object {$_ -match 'Name'}
      $OC = $get_Data.Context.PostContext |
      Where-Object {$_ -match 'ObjectClass'}

      [pscustomobject]@{

      'DN' = $DN
      'Computer Name' = $Name
      'Object Class' = $OC
      }
      }
      $Results | Out-File -FilePath "C:Users$env:UserNameDesktopresults1.csv"


      Expected results would be corrected headers with the data. Actual results is corrected headers, but no data.







      powershell






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 15:43









      rad_rad_

      445




      445
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You should use Export-CSV instead of Out-File.






          share|improve this answer
























          • Well, that worked great. Saved a ton of unnecessary code.. Thanks!

            – rad_
            Jan 2 at 16:15











          • Often it just takes a second set of eyes. :-)

            – Mike Shepard
            Jan 2 at 16:22











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009181%2fexport-adobject-data-to-csv%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You should use Export-CSV instead of Out-File.






          share|improve this answer
























          • Well, that worked great. Saved a ton of unnecessary code.. Thanks!

            – rad_
            Jan 2 at 16:15











          • Often it just takes a second set of eyes. :-)

            – Mike Shepard
            Jan 2 at 16:22
















          0














          You should use Export-CSV instead of Out-File.






          share|improve this answer
























          • Well, that worked great. Saved a ton of unnecessary code.. Thanks!

            – rad_
            Jan 2 at 16:15











          • Often it just takes a second set of eyes. :-)

            – Mike Shepard
            Jan 2 at 16:22














          0












          0








          0







          You should use Export-CSV instead of Out-File.






          share|improve this answer













          You should use Export-CSV instead of Out-File.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 16:09









          Mike ShepardMike Shepard

          12.9k64156




          12.9k64156













          • Well, that worked great. Saved a ton of unnecessary code.. Thanks!

            – rad_
            Jan 2 at 16:15











          • Often it just takes a second set of eyes. :-)

            – Mike Shepard
            Jan 2 at 16:22



















          • Well, that worked great. Saved a ton of unnecessary code.. Thanks!

            – rad_
            Jan 2 at 16:15











          • Often it just takes a second set of eyes. :-)

            – Mike Shepard
            Jan 2 at 16:22

















          Well, that worked great. Saved a ton of unnecessary code.. Thanks!

          – rad_
          Jan 2 at 16:15





          Well, that worked great. Saved a ton of unnecessary code.. Thanks!

          – rad_
          Jan 2 at 16:15













          Often it just takes a second set of eyes. :-)

          – Mike Shepard
          Jan 2 at 16:22





          Often it just takes a second set of eyes. :-)

          – Mike Shepard
          Jan 2 at 16:22




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009181%2fexport-adobject-data-to-csv%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Mossoró

          Error while reading .h5 file using the rhdf5 package in R

          Pushsharp Apns notification error: 'InvalidToken'