Creating environment variable from script during OSD

Hi,

I would like testing server name in a script to see in which domaine the computer is:

$tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
$servers = "server1","server2","server3"
Foreach($s in $servers) {

 if (Test-Connection -Cn $s -BufferSize 16 -Count 1 -ea 0 -quiet){
 
  Echo $s
  $tsenv.value("SCCMServer")=$s
 
 }

Then I get impossible calling a method from a null expression.

$s is existing as server1 so the SCCMServer variable should get server1 as value...

Where am I wrong and how solving it?

August 31st, 2015 3:07pm

You have some issue in the script. You are missing 1 curly bracket and also Echo is not a powershell thing

$tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
$servers = "server1","server2","server3"
Foreach($s in $servers) {

 if (Test-Connection -Cn $s -BufferSize 16 -Count 1 -ea 0 -quiet)
    {
         write-host $s
         $tsenv.value("SCCMServer")=$s 
    }
 }

Also the error you are getting impossible calling a method from a null expression this is from the test-connection command


Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 3:15pm

Hi,

#$tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
$servers = "server1","server2","server3"Foreach($s in $servers) {
 
 if (Test-Connection -Cn $s -BufferSize 16 -Count 1 -ea 0 -quiet)
     {
          write-host $s
        #  $tsenv.value("SCCMServer")=$s 
     }
  }
This is not generating any error as I rem anything about tsenv. But I need creating environment variable in my ts and then I get the error as soon as I remove #.

  • Edited by FRacine 12 hours 18 minutes ago
August 31st, 2015 3:24pm

Microsoft.SMS.TSEnvironment only exists when a task sequence is running, and you need to be in the Admin or System context to set TS variables.


Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 3:47pm

Hi,

So there is no way to test such script outside a TS?

Thanks,

August 31st, 2015 3:51pm

Hi,

No you need to run it with the task seqeunce engine is running, run a powershell skript with a long sleep and then press F8 when you have Command shell enabled in the boot image.

Regards,
Jrgen

Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 4:04pm

You are trying to access the environment that gets created only when a task sequence is running. This environment is deleted when the task sequence ends.

If you just want to test in the full OS, you can run a task sequence that will just 'hang' (e.g, command-line step to run Notepad). You can then test your script while the task sequence is still running.

August 31st, 2015 4:19pm

Hi,

#$tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
$servers = "server1","server2","server3"Foreach($s in $servers) {
 
 if (Test-Connection -Cn $s -BufferSize 16 -Count 1 -ea 0 -quiet)
     {
          write-host $s
        #  $tsenv.value("SCCMServer")=$s 
     }
  }
This is not generating any error as I rem anything about tsenv. But I need creating environment variable in my ts and then I get the error as soon as I remove #.

  • Edited by FRacine Monday, August 31, 2015 7:32 PM
Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 7:23pm

Microsoft.SMS.TSEnvironment only exists when a task sequence is running, and you need to be in the Admin or System context to set TS variables.


August 31st, 2015 7:47pm

Hi,

I just start a TS and ran my script. The variable was not created. So I ran again the TS and press F8 then ran manually the ps1.

$tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
$servers = "server1","server2","server3"
Foreach($s in $servers) {
 
 if (Test-Connection -Cn $s -BufferSize 16 -Count 1 -ea 0 -quiet)
     {
          write-host $s
          $tsenv.value("SCCMServer")=$s 
		  
     }
  }
From the TS, I am calling my script with this:

From the smsts.log, I read no error:

But the environment variable: SCCMServer is not appearing.

I ran set and did not saw that variable.

Free Windows Admin Tool Kit Click here and download it now
September 1st, 2015 10:25am

When you ran the script manually did you see a value being echo to the power shell ?

If you did not see anything means that none of the server where reachable and would make sense to have the error being empty.

Also to run the script you need to be inside the task sequence. The easy thing is to do like Jorgen propose make a script with a pause of like 1 hour. 
September 1st, 2015 10:45am

Hi,

I agree with you. Yes a servername is echoed but the variable is not create. Do I need a reboot to make it appearing?


  • Edited by FRacine 16 hours 38 minutes ago
Free Windows Admin Tool Kit Click here and download it now
September 1st, 2015 10:49am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics