Export ADObject data to CSV
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
add a comment |
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
add a comment |
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
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
powershell
asked Jan 2 at 15:43
rad_rad_
445
445
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You should use Export-CSV instead of Out-File.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
You should use Export-CSV instead of Out-File.
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
add a comment |
You should use Export-CSV instead of Out-File.
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
add a comment |
You should use Export-CSV instead of Out-File.
You should use Export-CSV instead of Out-File.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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