VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

issue with Invoke-VMScriptPlus v3

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

Reply
0 Kudos
1 Solution

Accepted Solutions
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

I found a work around as working as expected.

Is that something, I need to change or is that good Smiley Happy

View solution in original post

Reply
0 Kudos
24 Replies
LucD
Leadership
Leadership
Jump to solution

Did you include the class definition as well?


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

hereby attached the script for your reference.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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)


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You seem to have the FQDN in $vmName

$vmName = 'mypup1.abc.loc'

Try with only the DisplayName

$vmName = 'mypup1'


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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?


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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.


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Sure LucD. I will wait for your next update. Smiley Happy

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

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

attached the error and script used

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

still the same error. attached the error details

One thing, I noticed that resolve-hostname if this uses IP then we get reverse ip but If we use by name then we get correct ip.

also attached in the screenshot

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

My bad, I forgot to adapt the Send-GuestFile function.
Try this attached updated version.


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

Now, resolution passed and got other error.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

And with this version?


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD, attached the screenshot of the error.

Reply
0 Kudos