How to remove curly brackets from the PowerShell output - powershell

This is regarding AWS Certificate manager:
Get-ACMCertificatelist | Get-ACMCertificateDetail | Select -ExpandProperty renewalsummary | Where-object {$_.renewalStatus -ne "Success"}
Following is the output and I want to remove those curly brackets:
DomainValidationOptions RenewalStatus
----------------------- -------------
{Certificate1} PENDING_AUTO_RENEWAL
{certificate2} PENDING_AUTO_RENEWAL

The object in question is actually giving you an array for the DomainValidationOptions part, which can contain more than one value.
| Select #{n="DomainValidationOptions";e={($_.DomainValidationOptions -join ",")}},RenewalStatus
putting this on the end of your query will replace the DomainValidationOptions with a comma-separated string instead of an array, but keep the name, in cases where there's only one option this technically just converts it to a string.

Those are automatically added when converting a collection to a string in the formatting cmdlets. You can format the objects yourself to get rid of them.

You can specify calculated properties for each of the properties of DomainValidationOptions that you want to drill down into.
Step 1) Discover Properties
Pipe DomainValidationOptions to Get-Member -MemberType Property to see what properties you're going to be working with:
Get-ACMCertificateList |
Get-ACMCertificateDetail |
Select -ExpandProperty RenewalSummary |
Select -ExpandProperty DomainValidationOptions |
Get-Member -MemberType Property
TypeName: Amazon.CertificateManager.Model.DomainValidation
Name MemberType Definition
---- ---------- ----------
DomainName Property string DomainName {get;set;}
ValidationDomain Property string ValidationDomain {get;set;}
ValidationEmails Property System.Collections.Generic.List[string] ValidationEmails {get;set;}
ValidationStatus Property Amazon.CertificateManager.DomainStatus ValidationStatus {get;set;}
Step 2) Make Request
For the sake of example, lets say that we only want to retrieve DomainName and ValidationDomain. We would add two calculated properties for each of these properties, and then just do a regular select for RenewalStatus on the RenewalSummary object:
Get-ACMCertificateList |
Get-ACMCertificateDetail |
Select -ExpandProperty RenewalSummary |
Where-object {$_.RenewalStatus -ne "Success"} |
Select #{N='DomainName';E={$_.DomainValidationOptions.DomainName}}, `
#{N='ValidationDomain';E={$_.DomainValidationOptions.ValidationDomain}}, `
RenewalStatus
Example Output:
DomainName ValidationDomain RenewalStatus
---------- ---------------- -------------
*.subdomain.mydomain.com mydomain.com PENDING_AUTO_RENEWAL
mything.mydomain.com mydomain.com PENDING_AUTO_RENEWAL
You can perform a similar operation for ValidationEmails, I didn't include it in this example because that would have made too many properties to format cleanly on Stack Overflow. If you wanted to unroll the collection and convert it into a string, its calculated property would look something like this:
#{N='ValidationEmails';E={$_.DomainValidationOptions.ValidationEmails -join ','}}
Further Reading
AWS PowerShell Documentation - Get-ACMCertificateDetail
AWS SDK For .NET Documentation - Amazon.CertificateManager.Model.CertificateDetail
- There is an especially helpful note in here about RenewalSummary: "This field exists only when the certificate type is AMAZON_ISSUED."
Additional Note For Readers: I had to update to latest AWS PowerShell to be able to see RenewalSummary. If you can't see this property but expect that you should be able to, try updating your local AWS PowerShell version.

Related

How to call multiple properties in powershell

$loadbalancer = Get-AzureRmLoadBalancer | select Name,ResourceGroupName
$loadbalancer has two value ILB name and its resource group.
I want call both value in single PS command, Like when I call ILB name its respective resource group should be called.
The problem isn't entirely clear, can you please give an example of the function being used in the next step. I used "get-command" for the examples rather than get-azurermloadbalancer to avoid being package dependent.
To get the propety from $loadbalancer you use a dot and the name.
To get the current row from pipeline you can use $_
$loadbalancer = Get-Command get-* | Select-Object -Property CommandType,Name
#get a particular property
$loadbalancer.CommandType
#Use both properties in one function
$loadbalancer | Where-Object{($_.CommandType -eq "function") -and ($_.Name -match "Token")}

Using Where-Object with an object in PowerShell

I can't seem to get my brain around something and I am hoping someone can help
I have an array of objects called $SnapVMsAll that looks like this:
VMName Name SnapshotType CreationTime ParentSnapshotName
------ ---- ---------- ------------ ------------------
SHARED-server.host.com SHARED-server.host.com - (02/10/2017 - 13:02:44) Standard 02/10/2017 13:05:58
I need to display all the records in this array of objects that have string "Veeam" in the name column, but I think I am having problems isolating a specific attribute of the object to compare.
My attempts have been as follows:
echo $SnapVMsAll | Where-Object (Foreach-Object -MemberName name) -Like "Veeam Replica"
This returned the error:
Where-Object : Cannot validate argument on parameter 'Property'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
I have also tried
echo $SnapVMsAll | Where-Object $SnapVMsAll.name -Like "Veeam Replica"
But again I get the same error.
$SnapVMsAll does not exist inside the pipeline, it is the object you pass to the pipeline. Within a pipeline, objects can be accessed using the automatic variable $_.
Also note that -Like is usually used with wildcards to match a string. If the name is Veeam Replica you should use -eq, if the name contains Veeam Replica, you should change to -Like "*Veeam Replica*"
So changing your code to the following should work:
$SnapVMsAll | Where-Object { $_.name -Like "*Veeam Replica*" }

Does Get-WebApplication returns invalid objects?

Why do I don't get valid names, if I use Get-WebApplication in PowerShell like this:
Get-WebApplication | ForEach-Object { write-host $_.Name }
In the result set, the name properties are empty. Why?
Instead Get-WebApplication does return a structure like:
PS> Get-WebApplication
Name Application pool Protocols Physical Path
---- ---------------- --------- -------------
test default http C:\....
Assuming Get-WebApplication is https://technet.microsoft.com/en-us/library/ee790554.aspx.
Name column in the output from Get-WebApplication seems to behave a little like a calculated property. I was unable to find out exactly what the formula is behind it but by running
Get-WebApplication | select -ExpandProperty Path | select #{ n="Name";e={ $_.TrimStart('/') } }
you will get a similar looking result as to what you appear to be trying to extract.
This command selects and simplifies the Path property (essentially turns it into a string), then creates from it a calculated property that has the correct title and value.

Using Generalized Time in custom field

We are looking to add a couple of date fields to our Active Directory schema in 2008 R2 to help facilitate a bit of automation. In particular adding in a hireDate field that is populated automatically from an import.
This all works as expected in our test environment, however I am curious if there is a way to enhance the display of this custom attribute when it comes to dates.
The hireDate field is set as a Syntax of Generalized Time to match the native fields such as whenCreated.
However, when placing a date in to hireDate, it formats this much different than whenCreated for the display
> $user = Get-ADUser MyUser -Properties whenCreated,hireDate
> $user
-- snip --
hireDate : {19870301070000.0Z}
whenCreated : 8/28/2014 12:24:42 PM
If I inspect the MemberType of these properties, they are different too even though they are both Generalized Time
$user | Get-Member -MemberType Properties
TypeName: Microsoft.ActiveDirectory.Management.ADUser
Name MemberType Definition
---- ---------- ----------
hireDate Property Microsoft.ActiveDirectory.Management.ADPropertyValueCollection hireDate {get;set;}
whenCreated Property System.DateTime whenCreated {get;}
Is there a proper way to create a custom attribute as a DateTime or are we limited with the way to create these attributes?
hireDate is a collection. To get the actual value you need .hireDate[0] or .hireDate | Select-Object -First 1. And if PowerShell doesn't parse a Generalize-Time timestamp for you (hireDate isn't a standard attribute after all) you need to do it yourself.
$fmt = "yyyyMMddHHmmss.f'Z'"
$culture = [Globalization.CultureInfo]::InvariantCulture
Get-ADUser MyUser -Properties whenCreated,hireDate |
Select-Object -Property *, #{n='hireDate';e={
[DateTime]::ParseExact($_.hireDate[0], $fmt, $culture)
}} -Exclude hireDate

Referencing variables that are not strings

I want to reference properties selected from a cmdlet and then use them later on in my script as $mailboxarray.totalitemsize and $mailboxarray.totalitemsize
This is breaking later on in my script as I assume it doesn't like the type of object it is. How can I make anything I pull into the references a string? At the moment I am getting "No mapping exists from object type System.Management.Automation.PsObject to a known managed provider type". My code at the moment: the first command works fine but I can't use expandproperty when there are multiple entries like line two?
$recipienttype = Get-mailbox -identity $recordset.PrimarySmtpAddress | Select -expandproperty RecipientTypeDetails
$mailboxarray = Get-Mailbox -identity $recordset.PrimarySmtpAddress | Get-MailboxStatistics | Select-object totalitemsize, lastlogontime