enable it at the begining of your script and disable it at the end of the script, even if you put something in the <insert script> part of the code below, it will still run the options to reconfigure the server. This way you don't have to worry about leaving the door open.
if you run
exec sp_configure 'show advanced options', 1
go
reconfigure
go
exec sp_configure 'xp_cmdshell', 1
go
reconfigure
go
<insert Script>
exec sp_configure 'xp_cmdshell', 0
go
reconfigure
go
exec sp_configure 'show advanced options', 0
go
reconfigure