VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Unable to get custom object info

Hi,

I am not able to get the server and member as it is generating blank

There is no error after the script is run but the output shows blank for Server and Members

Please help

Script

connect-viserver 10.10.10.10

Get-VM | Where {$_.PowerState -eq 'PoweredOn' -and $_.Guest.OSFullName -match 'Windows'} | Select @{N="Folder"; E={$_.Folder.Name}}, @{N="VM"; E={$_.Name}}, @{N="IP_Address";E={@($_.guest.IPAddress[0])}}, @{N="OS"; E={@($_.guest.OSFullName)}} | Export-Csv -Path .\WinOS_Info.csv -NoTypeInformation -UseCulture

disconnect-viserver -server * -force -confirm:$false

$date = Get-Date -format dd-MMM-yyyy

$reportlocation = ".\WinOS_Info_$date.xlsx"

Import-Csv -Path .\WinOS_Info.csv -UseCulture -PipelineVariable row |

ForEach-Object -Process {

   try

   {

   $group =[ADSI]"WinNT://$server.VM/Administrators"

   $members = $group.Members() | foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null) }

   $result += New-Object PsObject -Property @{

   Server = $($server.VM)

   Members = $members -join ";"

  }

   }

   catch

   {

   $result = 'False'

   }

   $row | Add-Member -MemberType NoteProperty -Name 'Server' -Value $($server.VM) -PassThru | Add-Member -MemberType NoteProperty -Name 'Members' -Value $members -PassThru

} | Export-Excel -Path $reportlocation

Output

pastedImage_9.png

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

That should say $group =[ADSI]"WinNT://$($row.VM)/Administrators"

​This is substitution in a string, then you need this notation when referencing a property of an object.


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

View solution in original post

Reply
0 Kudos
16 Replies
LucD
Leadership
Leadership
Jump to solution

Where is the $server variable coming from and what is supposed to be in there?


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

$server variable is nothing but the VM Name, I just wanted to get the same VM name in the sheet again, so that to ensure i am getting the members information from correct server.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

But you're not assigning a value to that variable anywhere in the script.


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

I guess, I have assigned, is this correct ?

Server = $($server.VM)

Members = $members -join ";"

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There you are creating the new object, but where do you assign what is in $server?


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

I want to get $server and $member details in the output file

$row | Add-Member -MemberType NoteProperty -Name 'Server' -Value $($server.VM) -PassThru | Add-Member -MemberType NoteProperty -Name 'Members' -Value $members -PassThru

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There is no problem assigning a value, my question is just, where do you assign something to the variable $server.
If you don't, then this will not work, since $server by default contains $null.

You need to have a $server = ... somewhere in your script, before you actually use the variable


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

I have a .csv file which has below details and $server.VM has get input from that file.

contents of the file as below

"Folder","VM","IP_Address","OS"

"Dev","app09","192.27.3.76","Microsoft Windows Server 2012 (64-bit)"

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I understand that, but on the Import-Csv you defined the pipelinevariable as $row.
While the $server variable is nowhere assigned a value in your script.
Change $server to $row.


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

As suggested, I have changed $server to $row, still server and members shows blank

$reportlocation = ".\Local_Admin_Accounts_date.xlsx"

Import-Csv -Path .\WinOS_Info.csv -UseCulture -PipelineVariable row |

ForEach-Object -Process {

   try

   {

   $group =[ADSI]"WinNT://$row/Administrators"

   $members = $group.Members() | foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null) }

   $result += New-Object PsObject -Property @{

   Server = $row

   Members = $members -join ";"

  }

   }

   catch

   {

   $result = 'False'

   }

   $row | Add-Member -MemberType NoteProperty -Name 'Server' -Value $row -PassThru | Add-Member -MemberType NoteProperty -Name 'Members' -Value $members -PassThru

} | Export-Excel -Path $reportlocation -AutoFilter -AutoSize -BoldTopRow -FreezeTopRow -WorksheetName Summary

pastedImage_9.png

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Now you changed $($server.VM) to $row.
That should be $($row.VM)


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

After I made the changes, I am getting error

$reportlocation = ".\Local_Admin_Accounts_$date.xlsx"

Import-Csv -Path .\WinOS_Info.csv -UseCulture -PipelineVariable row |

ForEach-Object -Process {

      $group =[ADSI]"WinNT://$($row.VM)/Administrators"

   $members = $group.Members() | foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null) }

   $result += New-Object PsObject -Property @{

   Server = $($row.VM)

   Members = $members -join ";"

  }

   }

$row | Add-Member -MemberType NoteProperty -Name 'Server' -Value $($row.VM) -PassThru | Add-Member -MemberType NoteProperty -Name 'Members' -Value $members -PassThru | Export-Excel -Path $reportlocation

Error :

Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.

At D:\myGet_Local_Administators_3.ps1:22 char:15

+    Server = $($row.VM)

+               ~~~~~~~

    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException

    + FullyQualifiedErrorId : MethodNotFound

Add-Member : Cannot bind argument to parameter 'InputObject' because it is null.

At D:\my\Get_Local_Administators_3.ps1:26 char:8

+ $row | Add-Member -MemberType NoteProperty -Name 'Server' -Value $($r ...

+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Add-Member], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

What happens when you change that to Server = $row.VM


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

Now I am able to get the server name after changing as below but member field still shows blank.

Import-Csv -Path .\WinOS_Info.csv -UseCulture -PipelineVariable row |

ForEach-Object -Process {

   try

   {

   $group =[ADSI]"WinNT://$row.VM/Administrators"

   $members = $group.Members() | foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null) }

   $result += New-Object PsObject -Property @{

   Server = $($row.VM)

   Members = $members -join ";"

  }

   }

   catch

   {

   $result = 'False'

   }

   $row | Add-Member -MemberType NoteProperty -Name 'Server' -Value $($row.VM) -PassThru | Add-Member -MemberType NoteProperty -Name 'Members' -Value $($members -join ";") -PassThru

} | Export-Excel -Path $reportlocation

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That should say $group =[ADSI]"WinNT://$($row.VM)/Administrators"

​This is substitution in a string, then you need this notation when referencing a property of an object.


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Thank you very much LucD.

that worked perfectly Smiley Happy

Reply
0 Kudos