PowerCLI

 View Only
  • 1.  powershell with CustomizationSpec scripttext

    Posted Mar 04, 2022 03:53 AM

    Hi Everyone,

    I need to use powershell to do New-OSCustomizationSpec with LINUX OS type script.

    The operation on the web page is normal.

    But I can't find POWERSHEll related API or command.

    Can this be done with powershell or Powercli?

    Thank you very much!



  • 2.  RE: powershell with CustomizationSpec scripttext

    Posted Mar 04, 2022 06:07 AM

    Yes, on the New-OSCustomizationSpec cmdlet the OStype parameter can be set to Linux.



  • 3.  RE: powershell with CustomizationSpec scripttext

    Posted Mar 07, 2022 01:27 AM

    Hi Luc.

    But I don't want to set the OS-type parameter.

    I wish to set customization script in powershell script,like this.微信图片_20220307092634.jpg

    Hope to give advice,

    Thank you!



  • 4.  RE: powershell with CustomizationSpec scripttext

    Posted Mar 07, 2022 01:38 AM

    I have got the content of this script by powershell command.

    I wished to modify it but failed.

    Can you offer some advice?

    Thank you very much!

    微信图片_20220307093526.jpg



  • 5.  RE: powershell with CustomizationSpec scripttext

    Posted Mar 07, 2022 05:50 AM

    can you tell me what exact modification you want to make?



  • 6.  RE: powershell with CustomizationSpec scripttext

    Posted Mar 08, 2022 02:17 AM

    Nice to meet you!

    Yes,I want to change the password of the operating system in this way.

    Windows virtual machines can set different passwords through custom specifications.

    But LINUX does not seem to have this parameter.

    微信图片_20220308094859.jpg微信图片_20220308094906.jpg

    So I found this way with script.

    The script has been written and the test passed, the content is as follows:

    微信图片_20220308095702.jpg

    Tested by creating a virtual machine from the template, it succeeded, the password for the OS was changed, and the "zjy.out" file was also generated in the root directory.

    Up to now, the operation is carried out on the website.

    I want to be able to pass in this script when creating a new virtual machine customization specification through powershell.

    I can't find the relevant parameters to pass this script when generating the virtual machine customization spec.Like this

    微信图片_20220308100857.jpg

    I know that there is a way to change the password of the virtual machine through Invoke-VMScript, but I don't know the original password of the virtual machine.

    So I hope to modify it through powershell, not through the web page.

    thank you for your reply!



  • 7.  RE: powershell with CustomizationSpec scripttext

    Posted Mar 07, 2022 07:36 AM

    How did you make that change?
    First, a call to GetCustomizationSpec, followed by a call to OverwriteCustomizationSpec?
    In the CustomizationSpecItem you have to update Spec.Identity.ScriptText



  • 8.  RE: powershell with CustomizationSpec scripttext

    Posted Mar 08, 2022 02:21 AM

    Hi Luc!

    I want to be able to do it via powershell.

    Could you please help write a sample? thank you very much!



  • 9.  RE: powershell with CustomizationSpec scripttext

    Posted Mar 08, 2022 08:16 AM

    The New-OSCustomizatonSpec cmdlet does not have the option to add a script.

    But you can combine the New-OSCustomizationSpec cmdlet with a call to the API method to add the script.
    Something like this for example.

    $script = @'
    #!/bin/sh
    if [ x$1 == x"precustomization" ]; then
    echo 'root:zjy@123' | chpasswd
    elif [ x$1 == x"postcustomization" ]; then
    echo test >> zjy.out
    fi
    '@
    $osCustName = 'LinuxCust'
    
    # Cleanup
    Get-OSCustomizationSpec -Name $osCustName -ErrorAction SilentlyContinue | Remove-OSCustomizationSpec -Confirm:$false -ErrorAction SilentlyContinue
    
    # Create new Spec
    New-OSCustomizationSpec -Name $osCustName -OSType 'Linux' -Domain 'domain.com' -NamingScheme 'vm' | Out-Null
    
    $custMgr = Get-View CustomizationSpecManager
    
    # Add script to Spec
    $p = $custMgr.GetCustomizationSpec($osCustName)
    $p.Spec.Identity.ScriptText = $script
    $custMgr.OverwriteCustomizationSpec($p)
    


  • 10.  RE: powershell with CustomizationSpec scripttext

    Posted Mar 08, 2022 09:04 AM

    This is all I need, thanks a lot!