PowerCLI

Expand all | Collapse all

PowerCLI: Add VMs on vCenter to available resource pool

  • 1.  PowerCLI: Add VMs on vCenter to available resource pool

    Posted 29 days ago

    Dear All,

    I used PowerCLI to add VMs into Resource Pool on vCenter. However has error bllow. Please support me, thanks!

    My code: 

    $credential = Get-Credential          
    Connect-VIServer -Server 10.122.113.20 -Credential $credential -Force -WarningAction SilentlyContinue
     
    # Đọc thông tin từ file CSV
    $csvData = Import-Csv -Path 'E:\adam\Scripts\res.csv' -UseCulture
     
    foreach ($row in $csvData) {
        $cluster = Get-Cluster -Name $row.Cluster
        $defaultResourcePool = Get-ResourcePool -Name "Resources" -Location $cluster
        $vm = Get-VM -Name $row.VMName | Where-Object { $_.ResourcePool -eq $defaultResourcePool }
        $destinationResourcePool = Get-ResourcePool -Name $row.ResourcePool -Location $cluster
        if ($vm) {
            Move-VM -VM $vm -Destination $destinationResourcePool
        } else {
            Write-Host "VM $($row.VMName) don't Resource Pool default."
        }
    }
     
    Disconnect-VIServer -Confirm:$false
    Error:
    Move-VM : 2/4/2025 9:01:03 AM Move-VM Failed to get Lookup Service information for this vCenter server. Details: The content type text/html;charset=utf-8 of the response message does not match the 
    content type of the binding (text/xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 1024 bytes of the response were: 
    '<!DOCTYPE html>
    <html class="login-pf">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="robots" content="noindex, nofollow">
                <meta name="viewport" content="width=device-width,initial-scale=1"/>
        <title>Sign in to VNPAY</title>
        <link rel="icon" href="/auth/resources/cbasi/login/keycloak/img/favicon.ico" />
                <link href="/auth/resources/cbasi/common/keycloak/web_modules/@patternfly/react-core/dist/styles/base.css" rel="stylesheet" />
                <link href="/auth/resources/cbasi/common/keycloak/web_modules/@patternfly/react-core/dist/styles/app.css" rel="stylesheet" />
                <link href="/auth/resources/cbasi/common/keycloak/node_modules/patternfly/dist/css/patternfly.min.css" rel="stylesheet" />
                <link href="/auth/resources/cbasi/common/keycloak/node_modules/patternfly/dist/css/patternfly-additions.min.css" rel="stylesheet" />
                <link href="/auth/resources/cbasi/common/keycloak/l'.'
    At line:15 char:9
    +         Move-VM -VM $vm -Destination $destinationResourcePool
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:)
     
     [Move-VM], VimException
        + FullyQualifiedErrorId : ViCore_ConnectivityServiceImpl_GetLookupServiceClient_NoClient,VMware.VimAutomation.ViCore.Cmdlets.Commands.MoveVM


  • 2.  RE: PowerCLI: Add VMs on vCenter to available resource pool

    Posted 29 days ago

    Could it be that you are using a language setting on your station, or in the CSV, that the VCSA doesn't understand?
    Can you check with Get-Culture?



    ------------------------------


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


    ------------------------------



  • 3.  RE: PowerCLI: Add VMs on vCenter to available resource pool

    Posted 29 days ago
    Edited by Cao Quang Chien 29 days ago

    Hi LucD, Thanks your answer, 

    I run  Get-Culture, output here: please suppore me to fix this.

    LCID             Name             DisplayName                                                                                                                                                              
    ----             ----             -----------                                                                                                                                                              
    1033             en-US            English (United States)




  • 4.  RE: PowerCLI: Add VMs on vCenter to available resource pool

    Posted 29 days ago

    That seems to be ok.
    Does the format of the CSV you are using correspond?
    Are there any characters in there that do not display correctly?



    ------------------------------


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


    ------------------------------



  • 5.  RE: PowerCLI: Add VMs on vCenter to available resource pool

    Posted 26 days ago

    Hi LucD,

    the CSV file as I attach, no special characters, format .csv

    I try fix this error, but not OK. 




  • 6.  RE: PowerCLI: Add VMs on vCenter to available resource pool

    Posted 26 days ago

    How did you configure your PowerCLI?
    Check with

    Get-PowerCLIConfiguration


    Try setting the certificate action to ignore, if not already so

    Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Scope Session,User,AllUsers -Confirm:$false


    A long shot, can you change the Connect-VIServer line to this?
    That might give a clue as to what is happening.
    Also try with -Protocol HTTP, if https doesn't work.

    Connect-VIServer -Server 10.122.113.20 -Credential $credential -Protocol https -Verbose


    ------------------------------


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


    ------------------------------



  • 7.  RE: PowerCLI: Add VMs on vCenter to available resource pool

    Posted 25 days ago

    Hi LucD, 

    Thank so much for support me. 

    I used your guide, but the same error. 

    Move-VM : 2/18/2025 9:11:18 AM Move-VM Failed to get Lookup Service information for this vCenter server. Details: The content type text/html;charset=utf-8 of the response message does not match the 
    content type of the binding (text/xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 1024 bytes of the response were: 
    '<!DOCTYPE html>
    <html class="login-pf">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="robots" content="noindex, nofollow">
                <meta name="viewport" content="width=device-width,initial-scale=1"/>
        <title>Sign in to VNPAY</title>
        <link rel="icon" href="/auth/resources/cbasi/login/keycloak/img/favicon.ico" />
                <link href="/auth/resources/cbasi/common/keycloak/web_modules/@patternfly/react-core/dist/styles/base.css" rel="stylesheet" />
                <link href="/auth/resources/cbasi/common/keycloak/web_modules/@patternfly/react-core/dist/styles/app.css" rel="stylesheet" />
                <link href="/auth/resources/cbasi/common/keycloak/node_modules/patternfly/dist/css/patternfly.min.css" rel="stylesheet" />
                <link href="/auth/resources/cbasi/common/keycloak/node_modules/patternfly/dist/css/patternfly-additions.min.css" rel="stylesheet" />
                <link href="/auth/resources/cbasi/common/keycloak/l'.'
    At line:15 char:9
    +         Move-VM -VM $vm -Destination $destinationResourcePool
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [Move-VM], VimException
        + FullyQualifiedErrorId : ViCore_ConnectivityServiceImpl_GetLookupServiceClient_NoClient,VMware.VimAutomation.ViCore.Cmdlets.Commands.MoveVM



  • 8.  RE: PowerCLI: Add VMs on vCenter to available resource pool

    Posted 25 days ago

    Hi LucD, 

    Thank so much for support me. 

    I used your guide, but the same error. 

    Move-VM : 2/18/2025 9:11:18 AM Move-VM Failed to get Lookup Service information for this vCenter server. Details: The content type text/html;charset=utf-8 of the response message does not match the 
    content type of the binding (text/xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 1024 bytes of the response were: 
    '<!DOCTYPE html>
    <html class="login-pf">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="robots" content="noindex, nofollow">
                <meta name="viewport" content="width=device-width,initial-scale=1"/>
        <title>Sign in to VNPAY</title>
        <link rel="icon" href="/auth/resources/cbasi/login/keycloak/img/favicon.ico" />
                <link href="/auth/resources/cbasi/common/keycloak/web_modules/@patternfly/react-core/dist/styles/base.css" rel="stylesheet" />
                <link href="/auth/resources/cbasi/common/keycloak/web_modules/@patternfly/react-core/dist/styles/app.css" rel="stylesheet" />
                <link href="/auth/resources/cbasi/common/keycloak/node_modules/patternfly/dist/css/patternfly.min.css" rel="stylesheet" />
                <link href="/auth/resources/cbasi/common/keycloak/node_modules/patternfly/dist/css/patternfly-additions.min.css" rel="stylesheet" />
                <link href="/auth/resources/cbasi/common/keycloak/l'.'
    At line:15 char:9
    +         Move-VM -VM $vm -Destination $destinationResourcePool
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [Move-VM], VimException
        + FullyQualifiedErrorId : ViCore_ConnectivityServiceImpl_GetLookupServiceClient_NoClient,VMware.VimAutomation.ViCore.Cmdlets.Commands.MoveVM



  • 9.  RE: PowerCLI: Add VMs on vCenter to available resource pool

    Posted 25 days ago

    Did the Connect-VIServer return any messages?



    ------------------------------


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


    ------------------------------



  • 10.  RE: PowerCLI: Add VMs on vCenter to available resource pool

    Posted 25 days ago

    Hi LucD,

    No. Only above error. 

    I also use other script as create VMs, migrate VMs to other Host, Datastore, everything is ok. but with migrate VMs to Resource Pool has this error :(




  • 11.  RE: PowerCLI: Add VMs on vCenter to available resource pool

    Posted 25 days ago

    The only possible option that I can still imagine is that the cluster name or the resourcepool name in the CSV is for a resource that doesn't exist (or returns more than 1 result).
    Then the Inventory Service might return an error (in HTML) while the API method behind the PowerCLI cmdlet is expecting a result in XML format.

    Also check if you have multiple connections open (with $global:defaultVIServers)



    ------------------------------


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


    ------------------------------



  • 12.  RE: PowerCLI: Add VMs on vCenter to available resource pool

    Posted 24 days ago

    Hi LucD,

    My vCenter has some clusters, but i only want to get the VMs in one cluster, as you see my script and csv file.

    Can you use my script to run on your environment? Please let me know the results.




  • 13.  RE: PowerCLI: Add VMs on vCenter to available resource pool

    Posted 24 days ago

    Your code seems to work for me



    ------------------------------


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


    ------------------------------



  • 14.  RE: PowerCLI: Add VMs on vCenter to available resource pool

    Posted 23 days ago

    Hi

    The code "$vm = Get-VM -Name $row.VMName | Where-Object { $_.ResourcePool -eq $defaultResourcePool }" looks suspicious to me as you are trying to compare a string ($_.ResourcePool) with an array ($defaultResourcePool) which would always return negative. $vm would be empty and move-vm would fail to find the vm.

    Try { $_.ResourcePool -eq $defaultResourcePool.Name } instead.




  • 15.  RE: PowerCLI: Add VMs on vCenter to available resource pool

    Posted 22 days ago

    Hi Leafy911,

    Thank so much for support me.

    I changed $_.ResourcePool -eq $defaultResourcePool to $_.ResourcePool -eq $defaultResourcePool.Name, No Error, but responsed "VM chiencq does not exist in the resource pool default", while i checked this VM are in default Resource Pool named "Resource".