For a few months now I have been trying to work out running a simple PowerShell script to set some task sequence variables in my build and capture task sequence. This is easily doable with a package/application and a wrapper but I wanted a one liner that could be ran from a run command line task. Easy to implement, and even easier to use, because you don’t have to worry about source updates or any files whatsoever.
I finally got it working this weekend! Long story short it ended up being two things:
- When calling a PowerShell command using powershell.exe and the -command parameter you must specify “& { your command here }”
- When setting a task sequence variable via PowerShell, you must use single-quotes, not double quotes.
Previously I was using the following command, and it did not work.
1 |
powershell.exe -executionpolicy bypass -command {$tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment; $tsenv.Value("ImageVersion") = get-date -uformat %m%d%Y} |
I caught another example someone had posted on a forum reply on technet and they had made the two changes I noted above. The command now looks like this:
1 |
powershell.exe -executionpolicy bypass -command "& {$tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment; $tsenv.Value('ImageVersion') = get-date -uformat %m%d%Y}" |
This is exactly what I was looking for. I am using the %ImageVersion% variable to do two things.
- Brand the image version to the registry
- Brand the image version to the captured .wim
I found it pretty amazing that the task sequence variable worked on the Capture task. This brings me one step closer to a fully automated image build.
[…] or PowerShell can do a lot of the work for you if the information you need is on the machine you are deploying. […]
Worked like a champ.
Awesome!