Get-ADComputer from Multiple Computers from a CSVFile - powershell

I have a csv file full of computer information formatted:
Name OS Site Code AD_Status Region Tech
computerone Windows 10 Enterprise **** Exists Chicago T T
computertwo Windows 10 Enterprise **** Exists Chicago T T
computerthree Windows 10 Enterprise **** Exists Chicago T T
I'm running a Powershell script that grabs the computer name from the csv file and checks its 'modifyTimeStamp' field.
$csvfile = Import-CSV -Path 'C:\Users\****\testexcel.csv'-Delimiter ","
$numofcompsincsv = $csvfile.psobject.properties.value[0] - 1
for ($i = 0; $i -le $numofcompsincsv; $i++) {
Get-ADComputer -identity $csvfile[$i].psobject.properties.value[0] -Properties * | FT Name, modifyTimeStamp
}
The problem with this is that it prints the computer information one by one, for example:
Name modifyTimeStamp
---- ---------------
computerone 7/19/2019 11:06:22 AM
Name modifyTimeStamp
---- ---------------
computertwo 7/24/2019 6:02:14 AM
Name modifyTimeStamp
---- ---------------
computerthree 7/24/2019 2:02:14 AM
How can I modify this so that it prints all in one like:
Name modifyTimeStamp
---- ---------------
computerone 7/19/2019 11:06:22 AM
computertwo 7/24/2019 6:02:14 AM
computerthree 7/24/2019 2:02:14 AM

Don't do this with a for loop; use the pipe instead:
Import-CSV -Path 'C:\Users\****\testexcel.csv' | Select -expand Name | Get-ADComputer -prop modifyTimeStamp | Select Name,ModifyTimeStamp
ETA: I apparently didn't create the CSV (hand-coded) I used for testing quite properly, and had originally piped the CSV to | Select Name | instead of expanding the property (which worked in my test). When I re-did it using Excel, I verified that | Select -expand Name | was required, per the comment by #js2010.

Related

Show output in two columns Powershell

I have 2 execution commands and we need to have it as a otput file (csv). The results should be shown in two columns next to each other. I am not sure how to use "format-table" or hashtables?
It gets attributes from Vmware
get-vm | get-annotation -customattribute "GrupaAktualizacji" | select value
get-vm | select name, #{N="DnsName"; E={$_.ExtensionData.Guest.Hostname}}
Below the example output
Name DnsName
---- -------
examplename example.com
and
PS C:\Windows\system32> get-vm | get-annotation -customattribute "GrupaAktualizacji" | select value
Value
-----
GWT
GW3
GW2
GW3
GW3
GW2

Does PSCustomObject have an order in which its displayed to the console? [duplicate]

This question already has an answer here:
PowerShell output is crossing between functions
(1 answer)
Closed 1 year ago.
Good morning peeps,
Im a big fan of making a selection from values that are returned from listed items. So, I was expirementing with making a selection out of the values returned from C:\Users, which is input into a [PSCustomObject] type, but noticed something off. I can list the selection into the [PSCustomObject] just fine like so:
[array]$Userlist = Get-ChildItem C:\users | Sort-Object -Property LastWriteTime -Descending
for($i=0; $i -lt $UserList.BaseName.count; $i++){
[PSCustomObject]#{
'Profile Name' = "$($i): $($UserList.BaseName[$i])"
' Full Path ' = $UserList.FullName[$i]
'Modified Time' = $UserList.LastWriteTime[$i]
}
}
#Output:
Profile Name Full Path Modified Time
------------ ------------- -------------
0: Abraham C:\users\Abraham 4/11/2021 10:26:58 PM
1: Public C:\users\Public 3/28/2021 8:51:28 AM
..but, when I try to make a selection simply by adding a Read-Host at the end of the script, I get that prompt first:
[array]$Userlist = Get-ChildItem C:\users | Sort-Object -Property LastWriteTime -Descending
for($i=0; $i -lt $UserList.BaseName.count; $i++){
[PSCustomObject]#{
'Profile Name' = "$($i): $($UserList.BaseName[$i])"
' Full Path ' = $UserList.FullName[$i]
'Modified Time' = $UserList.LastWriteTime[$i]
}
}
$ii = Read-Host -Prompt "Enter The Users Number to Delete"
$i = $ii -split " "
""
foreach($profile in $Userlist.baseName[$i]){
""
"Selection: $profile"
}
#output
Enter The Users Number to Delete: 1 <------ Here its asking first before displaying.
Profile Name Full Path Modified Time
------------ ------------- -------------
0: Abraham C:\users\Abraham 4/11/2021 10:26:58 PM
1: Public C:\users\Public 3/28/2021 8:51:28 AM
Selection: Public
Am I missing something? Why is my Read-Host being prompted before my top object is displayed? Id like to see the selection before i choose lol
Is there an order in which it's displayed?
A similar example of how the formatting system can surprise users.
Have a simple CSV
#'
OneProperty
test
'# | ConvertFrom-Csv
OneProperty
-----------
test
And another
#'
OneProperty,TwoProperty
test1,test2
'# | ConvertFrom-Csv
OneProperty TwoProperty
----------- -----------
test1 test2
Everything is perfect, as expected.
But running these together
#'
OneProperty
test
'# | ConvertFrom-Csv
#'
OneProperty,TwoProperty
test1,test2
'# | ConvertFrom-Csv
OneProperty
-----------
test
test1
Where is the second property? Well, the first object to hit the formatting system is what determines the properties. Powershell won't check each item because that could be a big performance hit. However, if you force the output to the formatter with Out-Host, Out-Default, or any of the Format-* cmdlets..
#'
OneProperty
test
'# | ConvertFrom-Csv | Out-Default
#'
OneProperty,TwoProperty
test1,test2
'# | ConvertFrom-Csv | Out-Default
OneProperty
-----------
test
OneProperty TwoProperty
----------- -----------
test1 test2
We can see both are shown completely. Simply put, the Read-Host cmdlet just beats your pipeline output to the formatting system.
You can also surround commands/sections of code with sub-expression and pipe it versus assigning to a variable.
$(for($i=0; $i -lt $UserList.BaseName.count; $i++){
[PSCustomObject]#{
'Profile Name' = "$($i): $($UserList.BaseName[$i])"
' Full Path ' = $UserList.FullName[$i]
'Modified Time' = $UserList.LastWriteTime[$i]
}
}) | Out-Default

powershell iterate all users and display recent lnk files

Iterate all windows $users and display the recent .lnk files from a specific path!
I have tried importing this module - https://gist.github.com/picheljitsu/cc2ed99cbae7caad3abb0928cd8a286b
Get-RecentFiles and I want to iterate with $users after getting the users with get-localuser
$user = (Get-LocalUser | Select-Object Name) |
ForEach-Object { Get-RecentFiles $user }
should display recent files of all users recent directory..
Directory: C:\Users\admin\AppData\Roaming\Microsoft\Windows\Recent
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 6/30/2019 6:59 PM AutomaticDestinations
d----- 7/1/2019 3:21 PM CustomDestinations
Directory: C:\Users\user2\AppData\Roaming\Microsoft\Windows\Recent
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 6/30/2019 6:59 PM AutomaticDestinations
d----- 7/1/2019 3:21 PM CustomDestinations
The result of Get-LocalUser | Select-Object Name is an array of users. When you pass this array to the pipeline, it will "unwrap" its items and pass them one at a time, and this item will be declared as $_ variable.
Passing Arrays to Pipeline
If a function returns more than one value, PowerShell wraps them in an array. However, if you pass the results to another function inside a pipeline, the pipeline automatically "unwraps" the array and processes one array element at a time.
ExpandProperty parameter is used to convert the object property Name to string to be used in the Get-RecentFiles function.
Modify your code and try this:
Get-LocalUser | Select-Object -ExpandProperty Name | Foreach-Object {Get-RecentFiles $_}
Update
The above code will get some errors for the disabled users (e.g: administrator, guest). To solve this, you have to only get the enabled users as follows:
Get-LocalUser | Where-Object Enabled | Select-Object -ExpandProperty Name | Foreach-Object {Get-RecentFiles $_}

Outputting sorted sections of sorted data to a variable?

I have the following PowerShell code:
[xml]$xml = Get-Content uccxResourceList_forReference.xml
$xml.resources.resource |
Select-Object firstname, lastname, extension,
#{Name="Team"; Expression={($_.team.name)}} |
Sort Team |
Format-Table
which produces a table like this:
firstName lastName extension Team
--------- -------- --------- ----------------
Homer Simpson 1000 SafetyInspectors
Frank Grimes 1001 SafetyInspectors
Lenford Leonard 1002 SafetyInspectors
Carlton Carlson 1003 SafetyInspectors
Montgomery Burns 2000 Executives
Waylon Smithers 2001 Executives
What I would like to do is output each team into its own file. So not just a simple | Out-File teamlist.txt at the end, but I would like to output a text file containing all of the "SafetyInspectors" and another with all of the "Executives".
I know I could get this done with a subsequent foreach loop but I feel it could also be done in the pipeline and I just don't know how to do it.
I'd prefer to output to a csv file (which easily is imported again) , so:
[xml]$xml = Get-Content uccxResourceList_forReference.xml
$xml.resources.resource |
Select-Object firstname, lastname, extension,
#{Name="Team"; Expression={($_.team.name)}} |
Group-Object Team | ForEach-Object {
$_.Group | Export-Csv ("{0}.csv" -f $_.Name) -NoTypeInformation
}
Should return something like this:
> gc .\Executives.csv
"firstName","lastName","extension","Team"
"Montgomery","Burns","2000","Executives"
"Waylon","Smithers","2001","Executives"
> gc .\SafetyInspectors.csv
"firstName","lastName","extension","Team"
"Homer","Simpson","1000","SafetyInspectors"
"Frank","Grimes","1001","SafetyInspectors"
"Lenford","Leonard","1002","SafetyInspectors"
"Carlton","Carlson","1003","SafetyInspectors"

Echo objects in Powershell with two different AD objects in them results them being put in the same table

I have two objects from an AD query in two variables, when I echo them they always end up in the same table. Very anoying.
For example
Echo "Users:"
$InactiveUsers | select name
Echo "Computers:"
$InactiveComputers | select name, lastlogondate
This should give me this result
Users:
name
----
John
Computers:
name lastlogondate
---- -------------
JohnComputer 1.1.2011
But instead I get it like this
Users:
name
----
John
Computers:
JohnComputer
What am I doing wrong?
Your problem is your script is outputting more than one type of object (three in fact). If you truely want to throw out the objects and just want tables, do this.
Write-Host "Users:"
$InactiveUsers | Select name | Out-Host
Write-Host "Computers:"
$InactiveComputers | Select name, lastlogondate | Out-Host