Capture Powershell output and set TS Variable

hi Guys.

I have read many forums reg. this but I cannot get it right . I have following PS script :

********************************************

$NetworkStatus = $NetworkStatusValue
$nameSrv = 'netadapter'
$service = Get-NetAdapter

If ($service.status -eq "Up") {
$NetworkStatusValue = "true"
}
else {
$NetworkStatusValue = "false"
}

********************************************

Very easy script that will set $networkStatus to either True or false :) .

What I read is that I can invoke powershell from command line using this :

$TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment
$TSEnv.Value($ComputerNetworkStatus) = $NetworkStatus

What I am trying to do, is to set either True or False variable for TSvariable called ComputerNetworkStatus.

I checked this post : http://www.potentengineer.com/using-powershell-to-set-osd-task-sequence-variables/ , but all of this are regarding very easy and one-lined codes. I don't know how to put it to command line - doesn't seem to be working. Also splitting this into 2 TS - one with PS.1 call and another with command isn't working either.

Could you please help me with this matter ?

Many many thanks

August 20th, 2015 9:56am

You would need to do this

$tsenv.Value("ComputerNetworkStatus") = $NetworkStatusValue

the value in bold is the name of the task sequence variable you want. $tsenv.Value("ComputerNetworkStatus")

and you want it to equal true or false that you set in the script above. So you need to call variable from the script.

SO the entire script would look like this

********************************************

$NetworkStatus = $NetworkStatusValue
$nameSrv = 'netadapter'
$service = Get-NetAdapter

If ($service.status -eq "Up") { 
$NetworkStatusValue = "true" 
}
else { 
$NetworkStatusValue = "false" 
}

#Set ts vairable
$TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment
$tsenv.Value("ComputerNetworkStatus") = $NetworkStatusValue
********************************************

You can find more information here :http://powershelldistrict.com/how-to-read-and-write-sccm-task-sequence-variables-with-powershell/

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 9:59am

Thank you Frederik for such fast replay.

So the outcome should look like this :

powershell.exe -executionpolicy bypass -command "& {$TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment; $tsenv.Value("ComputerNetworkStatus") = $NetworkStatusValue;

but my question is, where should I put the rest ? Adding it there doesn't work . Should I put another TS after that that will simply call this PS ?

thank you






August 20th, 2015 10:27am

Thank you Frederik for such fast replay.

So the outcome should look like this :

powershell.exe -executionpolicy bypass -command "& {$TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment; $tsenv.Value("ComputerNetworkStatus") = $NetworkStatusValue;

but my question is, where should I put the rest ? Adding it there doesn't work . Should I put another TS after that that will simply call this PS ?

thank you






You said you have a Powershell script that get you true or false. You need to add the code to that powershell and it should all run in the same task sequence. The Task sequence variable live only in the current task sequence.

So you just change the code of the original PS like i said in my reply and now the TS where that PS ran will have that variable configure.

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 10:31am

Thank you Frederick

from some reason, I haven't seen whole response, just a glimpse so I wrote that 2nd post. So I modified the script, hovewer I still got same error

***********************************

Failed to run the last action: Run PowerShell Script. Execution of task sequence failed.
Incorrect function. (Error: 00000001; Source: Windows) TSManager 8/20/2015 4:58:05 PM 1100 (0x044C)

***********************************

But I guess it has something to do with this - its from link you sent :

So basically it is very easy to access the OSD variables using the very friendly Microsoft.SMS.TSEnvironment com object. But, there is a small drawback of using this comboject when using it on 64x bits machines : It simply doesnt work.

When you need to access OSD variables in x64 bits mode from a PowerShell script, I recommend you try to avoid to read the varaibles directly from within the script (thus avoiding to use the Microsoft.SMS.TSEnvironment com object

***********************************

So I will try to dig deeper, how to get this work.

I know that simple WMI query can get this done, but there are some occasions , that we need to use powershell.

Thank you very much for your effort

August 20th, 2015 11:15am

Am using this windows 7 x64 and can do this with no issue. I build entire tattoo and even use the UDI wizard to set and pass the info to a PS that read and change the value.

Just make a step that run a command line and do this. After that test it.

powershell.exe -executionpolicy bypass -command {$tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment; $tsenv.Value("ComputerNetworkStatus") = "true"}

The error you are getting is with the script and not the X?4 if you would be having architecture issue you would be getting a error that look like new-object :  Retrieving the COM class factory for component with CLSID ....

So you might need to look at the smsts.log to see the exact issue. Also you should have some sort of log building in the script.

just so you know the line ************ need to have the comment prefix to is # so it would be #***********

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 11:27am

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

Other recent topics Other recent topics