# Scripting for OSD

### Connecting to the Task Sequence Environment

To leverage the variables in the task sequence, or create task sequence variables with PowerShell, you'll need to add a simple line into your script:

```powershell
Import-Module DeployR.Utility
```

Once you've done that, you can start pulling in variables you've set in the Task Sequence, or ones that are auto generated.  You can access the variables by using ${TSEnv:VariableName}.  Here is an example of pulling three Task Sequence variables into standard PowerShell variables to be used later in the script.

```powershell
[String]$MakeAlias = ${TSEnv:MakeAlias}
[String]$ModelAlias = ${TSEnv:ModelAlias}
[int]$OSImageBuild = ${TSEnv:OSImageBuild}

if ($MakeAlias -ne "Dell" -and $MakeAlias -ne "Lenovo" -and $MakeAlias -ne "HP" -and $MakeAlias -ne "Panasonic Corporation" -and $MakeAlias -ne "Microsoft") {
    Write-Host "MakeAlias must be Microsoft, Dell, Lenovo, Panasonic or HP. Exiting script."
    Exit 0
}
```

To create Task Sequence variables that can then be used by other steps in your task sequence, once you've connected into your TS environment by using the Import-Module DeployR.Utility command, you will simply set the variable:

```
$tsenv:LineOfBusiness = "RetailAgents"
$tsenv:HyperVHost = (Get-ItemPropertyValue -Path 'HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters' -Name "HostName" -ErrorAction SilentlyContinue)
```

<figure><img src="https://744643921-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJO9NLelA0RS8JB4i9oaQ%2Fuploads%2FwYOksgOvBamEkD90C5f7%2Fimage.png?alt=media&#x26;token=7c6153e6-eb25-4ce3-b989-875e439bf6ae" alt=""><figcaption></figcaption></figure>

For this example, I've added a step that runs those two lines, we'll take a look at the logs and variables:

<figure><img src="https://744643921-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJO9NLelA0RS8JB4i9oaQ%2Fuploads%2FFhXzTIhrxU8k3KJvORCk%2Fimage.png?alt=media&#x26;token=7e58ab13-6495-4c6a-b83e-a1ed0352a0f8" alt=""><figcaption></figcaption></figure>
