1 2 Previous Next 16 Replies Latest reply on Jan 16, 2020 4:52 AM by ganapa2000

    Unable to get custom object info

    ganapa2000 Hot Shot

      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

        • 1. Re: Unable to get custom object info
          LucD Guru
          Community WarriorsUser ModeratorsvExpert

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

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: Unable to get custom object info
            ganapa2000 Hot Shot

            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.

            • 3. Re: Unable to get custom object info
              LucD Guru
              vExpertCommunity WarriorsUser Moderators

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

              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 4. Re: Unable to get custom object info
                ganapa2000 Hot Shot

                I guess, I have assigned, is this correct ?

                 

                Server = $($server.VM)

                Members = $members -join ";"

                • 5. Re: Unable to get custom object info
                  LucD Guru
                  User ModeratorsvExpertCommunity Warriors

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

                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                  • 6. Re: Unable to get custom object info
                    ganapa2000 Hot Shot

                    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

                    • 7. Re: Unable to get custom object info
                      LucD Guru
                      vExpertCommunity WarriorsUser Moderators

                      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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                      • 8. Re: Unable to get custom object info
                        ganapa2000 Hot Shot

                        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)"

                        • 9. Re: Unable to get custom object info
                          LucD Guru
                          User ModeratorsvExpertCommunity Warriors

                          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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                          • 10. Re: Unable to get custom object info
                            ganapa2000 Hot Shot

                            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

                             

                            • 11. Re: Unable to get custom object info
                              LucD Guru
                              vExpertCommunity WarriorsUser Moderators

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

                              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                              • 12. Re: Unable to get custom object info
                                ganapa2000 Hot Shot

                                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

                                • 13. Re: Unable to get custom object info
                                  LucD Guru
                                  vExpertCommunity WarriorsUser Moderators

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

                                   

                                   

                                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                                  • 14. Re: Unable to get custom object info
                                    ganapa2000 Hot Shot

                                    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

                                    1 2 Previous Next