VMware Cloud Community
esxi1979
Expert
Expert

Generic PowerShell question on cmd pipeline

Hi

I have a non Powercli question ie generic powershell question 

 

Any suggestion pls 

 

Get-S3Bucket

CreationDate BucketName
------------ ----------
01/04/2022 12:11:11 xxx
===============

Get-S3Bucket |Get-S3BucketTagging

Key Value
--- -----
onwer xxxx

===============

 

in 2nd one, how can i get the bucket name & creation date ?

 

thanks 

Reply
0 Kudos
13 Replies
LucD
Leadership
Leadership

Since these cmdlets do apparently not support common parameters, like PipelineVariable, you will have to use a ForEach construction.
Something like this

$buckets = Get-S3Bucket
foreach($bucket in $buckets){
    Get-S3BucketTagging |
    Select @{N='Bucket';E={$bucket.Name}},
        @{N='CreationDate';E={$bucket.CreationDate}},
        Key,Value
}

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

esxi1979
Expert
Expert

@LucD  Thanks for the reply .. it still did not liked something , i tried like below, any suggestions pls

 

PS /root> $buckets = Get-S3Bucket
PS /root> foreach($bucket in $buckets){Get-S3BucketTagging -BucketName $($bucket.Name) |Select @{N='Bucket';E={$bucket.Name}},@{N='CreationDate';E={$bucket.CreationDate}},Key,Value}
Get-S3BucketTagging: BucketName is a required property and must be set before making this call. (Parameter 'GetBucketTaggingRequest.BucketName')
PS /root>

 

 

Reply
0 Kudos
esxi1979
Expert
Expert

Also tried 

 

foreach($bucket in $buckets){Get-S3BucketTagging -BucketName (get-s3bucket $($bucket.Name)) |Select @{N='Bucket';E={$bucket.Name}},@{N='CreationDate';E={$bucket.CreationDate}},Key,Value}
Get-S3BucketTagging: Access Denied

 

 

And tried

 

foreach($bucket in $buckets){(get-s3bucket $($bucket.Name)) | Get-S3BucketTagging |Select @{N='Bucket';E={$bucket.Name}},@{N='CreationDate';E={$bucket.CreationDate}},Key,Value}

 

This gives all but bucket name which shows empty 

Reply
0 Kudos
LucD
Leadership
Leadership

Sorry, I have no access to these cmdlets, so I can really find out what is going on.
Perhaps try in the AWS PowerShell Scripting forum


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
esxi1979
Expert
Expert

Thanks @LucD  ,  i agree  AWS PowerShell Scripting should have been 1st choice, but powercli community is much  more active & is the best possible to get response on generic poweshell is what i observed , Kudos to folks like you , who really wants to help & the top experts.

 

here is formal docs

Get-S3BucketTagging

https://docs.aws.amazon.com/powershell/latest/reference/items/Get-S3Bucket.html

 

Not much i found even in normal google search For example  in  stackoverflow etc

Reply
0 Kudos
LucD
Leadership
Leadership

You could check what exactly both cmdlets return, perhaps we can work from there

 

Get-S3Bucket | Get-Member
Get-S3BucketTagging (Get-S3Bucket | Select -First 1) | Get-Member

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
esxi1979
Expert
Expert

Here is what i see...  2nd one gives "Get-S3BucketTagging: Access Denied" for some reason 

 

PS /root> Get-S3Bucket | Get-Member

TypeName: Amazon.S3.Model.S3Bucket

Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
BucketName Property string BucketName {get;set;}
CreationDate Property datetime CreationDate {get;set;}

 

 

###

PS /root> Get-S3BucketTagging (Get-S3Bucket | Select -First 1) | Get-Member
Get-S3BucketTagging: Access Denied
PS /root>

Reply
0 Kudos
LucD
Leadership
Leadership

"Access denied"?
Perhaps only for that first one.
Do you get the same on others?

Get-S3Bucket | Get-S3BucketTagging | Get-Member


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
esxi1979
Expert
Expert

Here is what i see 

 

PS /root> Get-S3Bucket | Get-S3BucketTagging | Get-Member

TypeName: Amazon.S3.Model.Tag

Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Key Property string Key {get;set;}
Value Property string Value {get;set;}

PS /root>

Reply
0 Kudos
LucD
Leadership
Leadership

Since you seem to get the "Access Denied" for some buckets, lets try with a try-catch construct.
That way we can avoid the "Access Denied" terminating exception.

But, I can't test this since I don't have access to such an environment.

$buckets = Get-S3Bucket
foreach($bucket in $buckets){
    try{
        Get-S3BucketTagging -BucketName $bucket.Name - |
        Select @{N='Bucket';E={$buket.Name}},
            @{N='CreationDate';E={$bucket.CreationDate}},
            Key,Value
    }
    catch{
        Write-Host "Error for bucket $($bucket.Name)"
    }
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
esxi1979
Expert
Expert

here is the output of all cmd

 

PS /root> Get-S3Bucket

CreationDate BucketName
------------ ----------
1/4/2022 12:31:17 PM xxx

PS /root>

 


PS /root>
PS /root> Get-S3Bucket|Get-S3BucketTagging

Key Value
--- -----
onwer xxx

PS /root>

 

 


PS /root>
PS /root> $buckets = Get-S3Bucket
PS /root> foreach($bucket in $buckets){
>> try{
>> Get-S3BucketTagging -BucketName $bucket.Name - |
>> Select @{N='Bucket';E={$buket.Name}},
>> @{N='CreationDate';E={$bucket.CreationDate}},
>> Key,Value
>> }
>> catch{
>> Write-Host "Error for bucket $($bucket.Name)"
>> }
>> }
Error for bucket
PS /root>
PS /root>

Reply
0 Kudos
LucD
Leadership
Leadership

I'm afraid I can't explain why that is happening.
These AWS cmdlets seem to behave rather strangely.
Without any access to an AWS environment, I'm at my wit's end.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
esxi1979
Expert
Expert

its ok thanks for checking @LucD 

Reply
0 Kudos