Forums on Intune, SCCM, and Windows 11

Welcome to the forums. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your topics and posts, as well as connect with other members through your own private inbox!

PENDING Writing variable to registry

Paul Conway

New Member
Messages
2
Reaction score
0
Points
1
I have a task variable set in the TSGUI at the beginning of the build process for the build technicians name
I can see this populates as expected
1726140192833.png
I am then trying to add this to the devices registry as part of the build for auditing purposes
the script I am trying to use is:

# Create an object to access the task sequence environment
$tsenv = New-Object -ComObject Microsoft.SMS.TSEnvironment
# Set variables to indicate value and key to set
$RegistryPath = "HKEY_LOCAL_MACHINE\SYSTEM"
$Name = "ImagingTechnician"
$Value = $tsenv.Value("_OSDTechnician")
# Create the key if it does not exist
If (-NOT (Test-Path $RegistryPath)) {
New-Item -Path $RegistryPath -Force | Out-Null
}
# Now set the value
New-ItemProperty -Path $RegistryPath -Name $Name -Value $Value -PropertyType REG_SZ -Force

however this is failing, I ve tried using the variable as %OSDTechnician and OSDTechnician
the first of these also fails, the second added the value as OSDTechnician rather than the the value

How do I refer to the variable to get the correct value?
 
I have a task variable set in the TSGUI at the beginning of the build process for the build technicians name
I can see this populates as expected
View attachment 6510
I am then trying to add this to the devices registry as part of the build for auditing purposes
the script I am trying to use is:

# Create an object to access the task sequence environment
$tsenv = New-Object -ComObject Microsoft.SMS.TSEnvironment
# Set variables to indicate value and key to set
$RegistryPath = "HKEY_LOCAL_MACHINE\SYSTEM"
$Name = "ImagingTechnician"
$Value = $tsenv.Value("_OSDTechnician")
# Create the key if it does not exist
If (-NOT (Test-Path $RegistryPath)) {
New-Item -Path $RegistryPath -Force | Out-Null
}
# Now set the value
New-ItemProperty -Path $RegistryPath -Name $Name -Value $Value -PropertyType REG_SZ -Force

however this is failing, I ve tried using the variable as %OSDTechnician and OSDTechnician
the first of these also fails, the second added the value as OSDTechnician rather than the the value

How do I refer to the variable to get the correct value?
Change the $RegistryPath from "HKEY_LOCAL_MACHINE" to "HKLM:" and the key from REG_SZ to String. By testing this in Powershell manually, I was able to get this to work. With the original values, Powershell was having issues.



# Create an object to access the task sequence environment
$tsenv = New-Object -ComObject Microsoft.SMS.TSEnvironment
# Set variables to indicate value and key to set
$RegistryPath = "HKLM:\SYSTEM"
$Name = "ImagingTechnician"
$Value = $tsenv.Value("_OSDTechnician")
# Create the key if it does not exist
If (-NOT (Test-Path $RegistryPath)) {
New-Item -Path $RegistryPath -Force | Out-Null
}
# Now set the value
New-ItemProperty -Path $RegistryPath -Name $Name -Value $Value -PropertyType String -Force
 

Forum statistics

Threads
7,045
Messages
27,537
Members
17,739
Latest member
Ericthomas

Trending content

Back
Top