Get-VM |
Select Name, Operatingsystem, VMHost, PowerState,
#{N="Datastore"; E={$_ |
Get-Datastore}} |
Out-gridview
I ran this command. It will return and output a grid view with all rows filled in.
However, the field "OperatingSystem" returns a blank column, nothing there.
Untested, but I think you can do this:
Get-VM | Select-Object Name,
#{Name = 'Operatingsystem'; Expression = {$_.Guest.OsFullName}},
VMHost, PowerState,
#{Name = 'Datastore'; Expression = {$_ | Get-Datastore}} |
Out-GridView
You're looking for the "Guest" property I think. FYI you can also do this to view everything if you're not sure of the exact name
Get-VM |
Select-Object -Property * |
Out-gridview
Related
I tried something like this but it never gives the values, where my ifile.csv contains two columns displayname, grpmail
Import-Csv "E:\ifile.csv" | foreach {get-distributionGroupMember -Identity $_.displayname | select-Object #{Name= GroupName; Expression = {$_.grpmail}}, Recipienttype, Primarysmtpaddress} | Export-csv -no typeinformation "E:\Ofile.csv"
Can anyone advise me what I am doing wrong
I am expecting ofile.csv with 3 columns as GroupName, Recipienttype, Primarysmtpaddress
i get values for Recipienttype, Primarysmtpaddress columns but GroupName column is always empty.
It's because your $_ in the pipeline has changed to the result of your get-distributiongroupmember and is no longer your CSV file input.
Try this instead:
Import-Csv "E:\ifile.csv" | foreach {
$gname = $_.grpmail
Get-distributionGroupMember -Identity $_.displayname | Select #{Name= GroupName; Expression = {$gname}}, Recipienttype, Primarysmtpaddress} | Export-csv -no typeinformation "E:\Ofile.csv"
I've just split lines 2 and 3 to make it easier to read - you can put a semi-colon after $gname = $_.grpmail instead if you like.
As a general note, I like to assign specific variable names to pipeline objects to see what I'm actually working with, especially if they're being transformed on the way. Also, I like to use multiple lines to better see what's happening
foreach ($g in (Import-Csv "E:\ifile.csv")) {
Get-distributionGroupMember -Identity $g.displayname |
Select #{Name= GroupName; Expression = {$g.grpmail}},Recipienttype,Primarysmtpaddress
} | Export-csv -notypeinformation "E:\Ofile.csv"
EDIT
As per T-Me's suggestion, try this:
Import-Csv "E:\ifile.csv" | ForEach-Object {
$grpmail = $_.grpmail
get-distributionGroupMember -Identity $_.displayname | Select-Object #{ Name = 'GroupName'; `
Expression = "$grpmail" }, `
Recipienttype, Primarysmtpaddress
} | Export-csv -no typeinformation "E:\Ofile.csv"
Is there a way to use a calculated property in the same select statement for another calculated property. Or do you have to have another separate select for this?
I'm using PowerCLI from VMware to try to produce some memory stats (total, provisioned, % provisioned) for hosts.
get-vmhost | sort Parent |
select Parent, Name, #{Name="MemoryTotalGB";E={[math]::Round($_.MemoryTotalGB)}}, #{Name="MemoryProvisionedGB";Expression={$_ | get-vm | measure -sum MemoryGB | select -ExpandProperty Sum}} |
select Parent, Name, MemoryTotalGB, MemoryProvisionedGB, #{Name="MemoryProvisionedPercentage";E={[math]::Round($_.MemoryProvisionedGB / $_.MemoryTotalGB * 100)}} | ft
In the first select statement, I calculate the sum of VM memory running on the host as MemoryProvisionedGB. In the 2nd I do the % provisioned using this sum.
Is it possible to somehow reference the calculated property MemoryProvisionedGB in the first select statement to produce the % calculation?
Why don't you have the collection calculated just once (Although, BenH's ForEach-Object suggestion is more elegant)?
Get-VMHost |
Sort-Object -Property 'Parent' |
Select-Object -Property #(
'Parent'
'Name'
#{ N = 'MemoryTotalGB'
E = {[Math]::Round($PSItem.MemoryTotalGB)}
}
#{ N = 'MemoryProvisionedGB'
E = {($PSItem | Get-VM | Measure-Object -Sum 'MemoryGB').Sum}
}
#{ N = 'MemoryProvisionedPercentage'
E = {[Math]::Round(($PSItem | Get-VM | Measure-Object -Sum 'MemoryGB').Sum /
[Math]::Round($PSItem.MemoryTotalGB) * 100)
}
}
) | Format-Table
Rather than using calculated properties, you could use a ForEach-Object loop and create a [pscustomobject]. This would allow you to create several variables to reuse.
Get-VMHost |
ForEach-Object {
$MemoryTotalGB = [math]::Round($_.MemoryTotalGB)
$MemoryProvisionedGB = ($_ | Get-VM | Measure-Object -Sum MemoryGB).Sum
[PSCustomObject]#{
'Parent' = $_.Parent
'MemoryTotalGB' = $MemoryTotalGB
'MemoryProvisionedGB' = $MemoryProvisionedGB
'MemoryProvisionedPercentage' = [math]::Round($MemoryProvisionedGB / $MemoryTotalGB * 100)
}
} |
Sort-Object Parent |
Format-Table
I am currently trying to do an Out-GridView to get a simple overview about our group policy objects. To do so, I am using the Get-GPO cmdlet, like so:
Get-GPO -all |
Select-Object -Property DisplayName, Description |
Sort-Object -Property DisplayName |
Out-GridView
In our company we use the first line of the description field to store the name of the admin who created the policy, and all following lines hold a short description.
I would want to be able to grab the first line of the the Description field with the column header Responsability and all other lines of the field in a separate column. So assuming my current code would give me a table like this:
DisplayName | Description
-------------------------
GPO1 | Username
| stuff
| stuff
I would want it to look like this:
DisplayName | Responsability | Description
------------------------------------------
GPO1 | Username | stuff
| | stuff
How can I achieve this?
As #Matt suggested, you can use a calculated property.
Then since Description is a string, rather than an array of strings, you will need to split the line at the line breaks. This can be done by using -split and since it's information from a GPO we can assume Windows line endings `r`n (Otherwise you could use [environment]::newline)
The first property, use array element [0] will be the first line. For the second property, we'll need to save the array in a variable. Then we can use the length of that variable to get first element through the last.
Get-GPO -all |
Select-Object -Property DisplayName, #{
Name = "Responsibility"
Expression = {($_.Description -split "`r`n")[0]}
}, #{
Name = "Description"
Expression = {
$linearray = ($_.Description -split "`r`n")
$linearray[1..($linearray.length - 1)] | Out-String
}
} |
Sort-Object -Property DisplayName |
Out-GridView
Alternatively, you could create a new object rather than using the calculated property.
Get-GPO -all |
ForEach-Object {
$linearray = ($_.Description -split "`r`n")
[pscustomobject]#{
"DisplayName" = $_.DisplayName
"Responsibility"= $linearray[0]
"Description" = $linearray[1..($linearray.length - 1)] | Out-String
}
} |
Sort-Object -Property DisplayName |
Out-GridView
The first thing to understand is what Get-GPO is returning: an array of objects, each of which has a set of properties.
What is displayed in your table is a series of rows (one per object), with the columns being the values of the properties for that object.
Therefore if you want a new column, you need a new property.
There are two ways you can do this: create a calculated property with Select-Object or add a property to the objects via Add-Member.
Calculated
You may provide a hashtable as a property to Select-Object, and the hashtable must have two keys:
Name (the name of the property)
Expression (a scriptblock that will be executed to determine the value, where $_ refers to the object itself)
Get-GPO -all |
Select-Object -Property DisplayName, Description, #{
Name = 'Responsibility'
Expression = {
($_.Description -split '\r?\n')[0] # First line
}
} |
Sort-Object -Property DisplayName |
Out-GridView
New Member
You can use a ScriptProperty that will execute a scriptblock each time the property is called on the object. Use $this to refer to the object in this context.
Get-GPO -all |
Add-Member -MemberType ScriptProperty -Name Responsibility -Value {
($this.Description -split '\r?\n')[0] # First line
} -Force -PassThru |
Select-Object -Property DisplayName, Responsibility, Description |
Sort-Object -Property DisplayName |
Out-GridView
I would probably use something like this:
Get-GPO -All | ForEach-Object {
$info = $_.Description
$pos = $info.IndexOf([Environment]::NewLine)
if ( $pos -gt 0 ) {
$responsibility = $info.Substring(0,$pos)
$description = $info.Substring($pos + [Environment]::NewLine.Length)
}
else {
$responsibility = ""
$description = $info
}
[PSCustomObject] #{
"DisplayName" = $_.DisplayName
"Responsibility" = $responsibility
"Description" = $description
}
}
This way you can preserve formatting.
What's the powershell command to get the all Content_DB size in SharePoint server(MB)?
Maybe?
Get-SPDatabase | Sort-Object disksizerequired -desc | Format-Table Name, #{Label ="Size in MB"; Expression = {$_.disksizerequired/1024/1024}}
If you need Export the report using below commands
Get-SPDatabase | Sort-Object disksizerequired -desc | Format-Table Name, #{Label ="Size in MB"; Expression = {$_.disksizerequired/1024/1024}} > c:\Content_DataBaseSize.txt
I am outputing an array of objects in HTML using this:
$arrinfo | Where-Object {$_.Status -eq "Delivered"} | ConvertTo-HTML - PreContent "<h2><font color=green>Delivered:</font></h2>" -Property Name, Outputfile, StartTime,EndTime,TotalSeconds -fragment |Out-String
My question is, can I format things without creating a new object? Specifically, I am looking to format the dates (StartTime,EndTime) in a different format.
I guess I could create another array of objects with the format needed, but am wondering if there is a better way.
You can use calculated properties:
$arrinfo |
Where-Object {$_.Status -eq "Delivered"} |
Select-Object Name,Outputfile,#{n='StartTime';e={$_.StartTime.ToString('ddMMyyyy')}},#{n='EndTime';e={$_.EndTime.ToString('ddMMyyyy')}},TotalSeconds |
ConvertTo-HTML -PreContent "<h2><font color=green>Delivered:</font></h2>" -Fragment |
Out-String
June Blender explains calculated properties in Name that Property.
Get-ChildItem | Select-Object #{Name = "Attributes"; Expression = {$_.Mode}},
#{Name = "Updated_UTC"; Expression = {$_.LastWriteTime.ToUniversalTime()}}, Name