1 2 Previous Next 24 Replies Latest reply on Nov 20, 2019 11:22 PM by LucD

    issue with Invoke-VMScriptPlus v3

    ganapa2000 Hot Shot

      Hi,

       

      I am getting the below error when used the Invoke-VMScriptPlus v3 function

       

      $vmName = 'mypup1.abc.loc'

      $credVM = Get-VICredentialStoreItem -Host $vmName

      $cred = New-Object System.Management.Automation.PSCredential ($credVM.User, (ConvertTo-SecureString $credVM.Password -AsPlainText -Force))

       

      $sInvoke = @{

          VM = $vmName

          GuestCredential = $cred

          ScriptText = 'sudo ls -l /opt'

          ScriptType = 'bash'

          Sudo = $true

      }

      Invoke-VMScriptPlus @sInvoke

       

      error :

      Invoke-VMScriptPlus : Cannot process argument transformation on parameter 'VM'. Index was out of range. Must be non-negative and less than the size of the collection.

      Parameter name: index

      At D:\Downloads\Invoke-VMScriptPlus.ps1:621 char:21

      + Invoke-VMScriptPlus @sInvoke

      +                     ~~~~~~~~

          + CategoryInfo          : InvalidData: (:) [Invoke-VMScriptPlus], ParameterBindingArgumentTransformationException

          + FullyQualifiedErrorId : ParameterArgumentTransformationError,Invoke-VMScriptPlus

        • 1. Re: issue with Invoke-VMScriptPlus v3
          LucD Guru
          User ModeratorsCommunity WarriorsvExpert

          Did you include the class definition as well?

          • 2. Re: issue with Invoke-VMScriptPlus v3
            ganapa2000 Hot Shot

            LucD,

             

            hereby attached the script for your reference.

            • 3. Re: issue with Invoke-VMScriptPlus v3
              LucD Guru
              vExpertCommunity WarriorsUser Moderators

              The value you have in $VMname is that the DisplayName of the VM or the FQDN of the guest OS inside the VM?

              It needs to be the DisplayName, or a VirtualMachine object (what you get from Get-VM)

              • 4. Re: issue with Invoke-VMScriptPlus v3
                ganapa2000 Hot Shot

                LucD,

                 

                Yes, it the name what I get, which is used in the script

                • 5. Re: issue with Invoke-VMScriptPlus v3
                  LucD Guru
                  User ModeratorsvExpertCommunity Warriors

                  You seem to have the FQDN in $vmName

                  $vmName = 'mypup1.abc.loc'

                  Try with only the DisplayName

                   

                  $vmName = 'mypup1'

                   

                  • 6. Re: issue with Invoke-VMScriptPlus v3
                    ganapa2000 Hot Shot

                    LucD,

                     

                    If I use only as the my name mypup1 then I am getting the below error

                     

                    Invoke-VMScriptPlus : Unable to determine the guest OS type on VM mypup1

                    At D:\Downloads\Invoke-VMScriptPlus.ps1:616 char:1

                    + Invoke-VMScriptPlus @sInvoke

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

                        + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException

                        + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-VMScriptPlus

                     

                    Invoke-VMScriptPlus : Try using the GuestOSType parameter.

                    At D:\Downloads\Invoke-VMScriptPlus.ps1:616 char:1

                    + Invoke-VMScriptPlus @sInvoke

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

                        + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException

                        + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-VMScriptPlus

                     

                    Resolve-DnsName : 9.3.27.172.in-addr.arpa : DNS name does not exist

                    At D:\Downloads\Invoke-VMScriptPlus.ps1:211 char:25

                    +             $hostName = Resolve-DnsName -Name $ip | Select-Object -Ex ...

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

                        + CategoryInfo          : ResourceUnavailable: (9.3.27.172.in-addr.arpa:String) [Resolve-DnsName], Win32Exception

                        + FullyQualifiedErrorId : DNS_ERROR_RCODE_NAME_ERROR,Microsoft.DnsClient.Commands.ResolveDnsName

                     

                    Invoke-WebRequest : Cannot bind parameter 'Uri'. Cannot convert value "https://:443/guestFile?id=6&token=52563041-6b65-4013-e236-651a638bf23e6" to type "System.Uri". Error: "Invalid URI: The

                    hostname could not be parsed."

                    At D:\Downloads\Invoke-VMScriptPlus.ps1:214 char:50

                    +             $copyResult = Invoke-WebRequest -Uri $filePath -Method Pu ...

                    +                                                  ~~~~~~~~~

                        + CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException

                        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

                     

                    ScripText copy failed!Status

                    At D:\Downloads\Invoke-VMScriptPlus.ps1:217 char:17

                    + ...             Throw "ScripText copy failed!`rStatus $($copyResult.Statu ...

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

                        + CategoryInfo          : OperationStopped: (ScripText copy failed!Status  :String) [], RuntimeException

                        + FullyQualifiedErrorId : ScripText copy failed!Status

                    • 7. Re: issue with Invoke-VMScriptPlus v3
                      LucD Guru
                      User ModeratorsvExpertCommunity Warriors

                      What guest OS do you have running in that VM?
                      Normally, provided the VMware Tools are running, the function can determine the guest OS type.
                      Try adding the GuestOSType parameter. There are 2 options: Windows or Linux.


                      The DNS error seems to indicate that your ESXi node does not use a FQDN that is in your DNS.
                      Is the name of the ESXi node perhaps it's IP address?
                      Or perhaps you can't query DNS from where you run the script?

                      • 8. Re: issue with Invoke-VMScriptPlus v3
                        ganapa2000 Hot Shot

                        After adding the GuestOS type and I tried using different VM, now I am getting the below error

                         

                        I use IP address for adding my esxi into vcenter. But I am able to query DNS from my laptop where I am running the script

                         

                        $vmName = 'jivvdjen01'

                        $credVM = Get-VICredentialStoreItem -Host $vmName

                        $cred = New-Object System.Management.Automation.PSCredential ($credVM.User, (ConvertTo-SecureString $credVM.Password -AsPlainText -Force))

                         

                        $sInvoke = @{

                            VM = $vmName

                            GuestOS = 'Linux'

                            GuestCredential = $cred

                            ScriptText = 'sudo ls -l /opt'

                            ScriptType = 'bash'

                            Sudo = $true

                        }

                        Invoke-VMScriptPlus @sInvoke

                         

                         

                        Error:

                        Resolve-DnsName : 9.3.27.172.in-addr.arpa : DNS name does not exist

                        At D:\Downloads\Invoke-VMScriptPlus.ps1:211 char:25

                        +             $hostName = Resolve-DnsName -Name $ip | Select-Object -Ex ...

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

                            + CategoryInfo          : ResourceUnavailable: (9.3.27.172.in-addr.arpa:String) [Resolve-DnsName], Win32Exception

                            + FullyQualifiedErrorId : DNS_ERROR_RCODE_NAME_ERROR,Microsoft.DnsClient.Commands.ResolveDnsName

                         

                        Invoke-WebRequest : Cannot bind parameter 'Uri'. Cannot convert value

                        "https://:443/guestFile?id=12&token=52308dfb-ccda-8daa-2fad-3ee3aeb8db6b12" to type "System.Uri". Error: "Invalid URI:

                        The hostname could not be parsed."

                        At D:\Downloads\Invoke-VMScriptPlus.ps1:214 char:50

                        +             $copyResult = Invoke-WebRequest -Uri $filePath -Method Pu ...

                        +                                                  ~~~~~~~~~

                            + CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException

                            + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

                         

                        ScripText copy failed!Status

                        At D:\Downloads\Invoke-VMScriptPlus.ps1:217 char:17

                        + ...             Throw "ScripText copy failed!`rStatus $($copyResult.Statu ...

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

                            + CategoryInfo          : OperationStopped: (ScripText copy failed!Status  :String) [], RuntimeException

                            + FullyQualifiedErrorId : ScripText copy failed!Status

                        • 9. Re: issue with Invoke-VMScriptPlus v3
                          LucD Guru
                          vExpertCommunity WarriorsUser Moderators

                          The problem here is that the URI the ESXi node returns is using an IP address.

                          Most of the time the certificates being used, do not contain an entry for the IP address, only the FQDN.
                          To avoid errors due to that, the function changes the IP address in the URI with the FQDN.


                          You probably do not have a DNS entry where the hostname is this IP address.

                          Hence the DNS resolution error.

                           

                          I could add a switch that cancels that conversion IP to FQDN, just for the cases like yours.

                          • 10. Re: issue with Invoke-VMScriptPlus v3
                            ganapa2000 Hot Shot

                            Sure LucD. I will wait for your next update.

                            • 11. Re: issue with Invoke-VMScriptPlus v3
                              LucD Guru
                              Community WarriorsUser ModeratorsvExpert

                              Try the attached new version of the function.

                              This version introduces a new switch, name NoIPforCert.

                              When this switch is $true, the function will not translate the IP address of the ESXi node in the URI to a FQDN.
                              As a I explained earlier, this might cause errors when using certificates that do not have a SAN entry for the IP address of the ESXi node where the VM is running.

                              The default is that the IP address is translated to the FQDN.

                               

                              In your case, you should call the function along these lines.
                              Note that in the verbose output, you will notice that the GET uses a URI with an IP address in there.

                               

                              $vmName = 'ubuntuVM'


                              $credVM = Get-VICredentialStoreItem -Host $vmName

                              $cred = New-Object System.Management.Automation.PSCredential ($credVM.User, (ConvertTo-SecureString $credVM.Password -AsPlainText -Force))


                              $sInvoke = @{

                                  VM = $vmName

                                  ScriptType = 'bash'

                                  ScriptText = 'whoami'

                                  GuestCredential = $cred

                                  Verbose = $true

                                  NoIPinCert = $true

                              }

                              Invoke-VMScriptPlus @sInvoke

                              • 12. Re: issue with Invoke-VMScriptPlus v3
                                LucD Guru
                                User ModeratorsCommunity WarriorsvExpert

                                The blog post, and the attached code in there has been updated with the NoIPinCert switch as well.

                                • 13. Re: issue with Invoke-VMScriptPlus v3
                                  ganapa2000 Hot Shot

                                  LucD,

                                   

                                  I have used your script, still I am getting the error

                                   

                                  attached the error and script used

                                  • 14. Re: issue with Invoke-VMScriptPlus v3
                                    LucD Guru
                                    User ModeratorsvExpertCommunity Warriors

                                    And what do you get when you leave out the NoIPinCert switch?
                                    Keep the Verbose switch in.

                                    1 2 Previous Next