VMware Cloud Community
JianyuZhu
Contributor
Contributor

powershell with CustomizationSpec scripttext

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!

0 Kudos
9 Replies
LucD
Leadership
Leadership

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


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

0 Kudos
JianyuZhu
Contributor
Contributor

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!

0 Kudos
JianyuZhu
Contributor
Contributor

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

0 Kudos
Godwin_Christop
Enthusiast
Enthusiast

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

0 Kudos
LucD
Leadership
Leadership

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


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

0 Kudos
JianyuZhu
Contributor
Contributor

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!

0 Kudos
JianyuZhu
Contributor
Contributor

Hi Luc!

I want to be able to do it via powershell.

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

0 Kudos
LucD
Leadership
Leadership

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)


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

JianyuZhu
Contributor
Contributor

This is all I need, thanks a lot!

0 Kudos