Show output in two columns Powershell - 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

Related

Select-object -property parameter not displaying multiple column

Currently when I try
get-process | Select-Object -property NAME,Id,CPU,PM
Only the Name column is displayed in vscode in ubuntu
Name
----
(sd-pam)
accounts-daemon
However if i switch the position of the value 'name' it would work but it seems to only display till where the 'name property is?
PS /home/limyk14> get-process | Select-Object -property Id,CPU,name,PM
Id CPU Name
-- --- ----
1506 0 (sd-pam)
PS /home/limyk14> get-process | Select-Object -property Id,CPU,PM,name
Id CPU PM Name
-- --- -- ----
1506 0 880640 (sd-pam)
797 2.77 49152 accounts-daemon
Thanks to #iRon and #mclayton
It is actually due to the length of a few processes that had really long names
vs code itself was one of them,
Format-table var1,2,3,4 -autosize didn't help neither did wrapping.
However the suggestion of using a hash table and declaring the width works.
format-table #{name = 'name';expression = 'name'; width = 7}, cpu, id, pm

Get-ADComputer from Multiple Computers from a CSVFile

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.

Editing Annotations In VSphere Using PowerShell

I need to be able to edit the Annotations of VMs from powershell(ps) as will be editing many at once Image. I found "AvailibleField" after getting VM view in ps Image but not sure how to edit these. Can anyone help? Thanks :)
I think what you're looking for is the Get/Set-Annotation cmdlets: http://www.vmware.com/support/developer/PowerCLI/PowerCLI651/html/Set-Annotation.html
Example:
PS C:\Users\kruddy> Get-VM file02 | select CustomFields
CustomFields
------------
{[CompanyName, ]}
PS C:\Users\kruddy> Get-VM file02 | Get-Annotation
AnnotatedEntity Name Value
--------------- ---- -----
file02 CompanyName
PS C:\Users\kruddy> Set-Annotation -Entity (Get-VM file02) -CustomAttribute CompanyName -Value TempCorp -Confirm:$false
AnnotatedEntity Name Value
--------------- ---- -----
file02 CompanyName TempCorp
PS C:\Users\kruddy> Get-VM file02 | Get-Annotation
AnnotatedEntity Name Value
--------------- ---- -----
file02 CompanyName TempCorp
PS C:\Users\kruddy> Get-VM file02 | select CustomFields
CustomFields
------------
{[CompanyName, TempCorp]}
PS C:\Users\kruddy>

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

PS: Filter selected rows with only max values as output?

I have a variable results ($result) of several rows of data or object like this:
PS> $result | ft -auto;
name value
---- -----
a 1
a 2
b 30
b 20
....
what I need to get all the rows of name and max(value) like this filtered output:
PS> $result | ? |ft -auto
name value
---- -----
a 2
b 30
....
Not sure what command or filters available (as ? in above) so that I can get each name and only the max value for the name out?
$result | group name | select name,#{n='value';e={ ($_.group | measure value -max).maximum}}
This should do the trick:
PS> $result | Foreach {$ht=#{}} `
{if ($_.Value -gt $ht[$_.name].Value) {$ht[$_.Name]=$_}} `
{$ht.Values}
This is essentially using the Begin/Process/End scriptblock parameters of the Foreach-Object cmdlet to stash input objects with a max value based on a key into a hashtable.
Note: watch out for extra spaces after the line continuation character (`) - there shouldn't be any.