Skip to content

Commit 83c4b7f

Browse files
change sms detection
1 parent f9d39b1 commit 83c4b7f

File tree

3 files changed

+131
-111
lines changed

3 files changed

+131
-111
lines changed

AppOptimizeAndConfig.ps1

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@
4545
4646
#Add script to task sequence
4747
48-
.LINKS
48+
.LINK
4949
https://github.com/TheVDIGuys/W10_1803_VDI_Optimize
5050
https://github.com/cluberti/VDI/blob/master/ConfigAsVDI.ps1
5151
52-
.LOGS
52+
.LOG
5353
1.1.1 - May 15, 2019 - Added Get-ScriptPpath function to support VScode and ISE; fixed Set-UserSettings
5454
1.1.0 - May 10, 2019 - added appx removal Feature on Demand removal, reorganized controls in categories
5555
1.0.4 - May 09, 2019 - added Office detection
@@ -95,41 +95,49 @@ Function Get-ScriptPath {
9595
}
9696
}
9797

98-
Function Import-SMSTSENV{
99-
## Get the name of this function
100-
[string]${CmdletName} = $PSCmdlet.MyInvocation.MyCommand.Name
98+
Function Get-SMSTSENV{
99+
param([switch]$LogPath,[switch]$NoWarning)
101100

102-
try{
103-
# Create an object to access the task sequence environment
104-
$Script:tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
105-
#test if variables exist
106-
$tsenv.GetVariables() #| % { Write-Output "$ScriptName - $_ = $($tsenv.Value($_))" }
107-
}
108-
catch{
109-
If(${CmdletName}){$prefix = "${CmdletName} ::" }Else{$prefix = "" }
110-
Write-Warning ("{0}Task Sequence environment not detected. Running in stand-alone mode." -f $prefix)
111-
112-
#set variable to null
113-
$Script:tsenv = $null
101+
Begin{
102+
## Get the name of this function
103+
[string]${CmdletName} = $PSCmdlet.MyInvocation.MyCommand.Name
114104
}
115-
Finally{
116-
#set global Logpath
117-
if ($tsenv){
118-
#grab the progress UI
119-
$Script:TSProgressUi = New-Object -ComObject Microsoft.SMS.TSProgressUI
120-
121-
# Query the environment to get an existing variable
122-
# Set a variable for the task sequence log path
123-
#$Global:Logpath = $tsenv.Value("LogPath")
124-
$Global:Logpath = $tsenv.Value("_SMSTSLogPath")
125-
126-
# Or, convert all of the variables currently in the environment to PowerShell variables
127-
$tsenv.GetVariables() | % { Set-Variable -Name "$_" -Value "$($tsenv.Value($_))" }
105+
Process{
106+
try{
107+
# Create an object to access the task sequence environment
108+
$Script:tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
109+
#test if variables exist
110+
$tsenv.GetVariables() #| % { Write-Output "$ScriptName - $_ = $($tsenv.Value($_))" }
128111
}
129-
Else{
130-
$Global:Logpath = $env:TEMP
112+
catch{
113+
If(${CmdletName}){$prefix = "${CmdletName} ::" }Else{$prefix = "" }
114+
If(!$NoWarning){Write-Warning ("{0}Task Sequence environment not detected. Running in stand-alone mode." -f $prefix)}
115+
116+
#set variable to null
117+
$Script:tsenv = $null
118+
}
119+
Finally{
120+
#set global Logpath
121+
if ($tsenv){
122+
#grab the progress UI
123+
$Script:TSProgressUi = New-Object -ComObject Microsoft.SMS.TSProgressUI
124+
125+
# Query the environment to get an existing variable
126+
# Set a variable for the task sequence log path
127+
#$UseLogPath = $tsenv.Value("LogPath")
128+
$UseLogPath = $tsenv.Value("_SMSTSLogPath")
129+
130+
# Convert all of the variables currently in the environment to PowerShell variables
131+
$tsenv.GetVariables() | % { Set-Variable -Name "$_" -Value "$($tsenv.Value($_))" }
132+
}
133+
Else{
134+
$UseLogPath = $env:Temp
135+
}
131136
}
132137
}
138+
End{
139+
If($LogPath){return $UseLogPath}
140+
}
133141
}
134142

135143
Function Format-ElapsedTime($ts) {
@@ -897,12 +905,11 @@ Else{
897905
$VerbosePreference = 'SilentlyContinue'
898906
}
899907

900-
#Check if running in Task Sequence
901-
Import-SMSTSENV
902-
908+
#build log name
903909
[string]$FileName = $scriptBaseName +'.log'
904-
$Global:LogFilePath = Join-Path $RelativeLogPath -ChildPath $FileName
905-
Write-Host "Using log file: $LogFilePath" -ForegroundColor Cyan
910+
#build global log fullpath
911+
$Global:LogFilePath = Join-Path (Get-SMSTSENV -LogPath -NoWarning) -ChildPath $FileName
912+
Write-Host "logging to file: $LogFilePath" -ForegroundColor Cyan
906913

907914
##*===========================================================================
908915
##* DEFAULTS: Configurations are here (change values if needed)
@@ -922,7 +929,7 @@ Write-Host "Using log file: $LogFilePath" -ForegroundColor Cyan
922929

923930

924931
# When running in Tasksequence and configureation exists, use that instead
925-
If($tsenv){
932+
If(Get-SMSTSENV){
926933
# Global Settings
927934
If($tsenv:CFG_DisableAppScript){[boolean]$DisableScript = [boolean]::Parse($tsenv.Value("CFG_DisableAppScript"))}
928935
If($tsenv:CFG_UseLGPOForConfigs){[boolean]$UseLGPO = [boolean]::Parse($tsenv.Value("CFG_UseLGPOForConfigs"))}

Win10OptimizeAndConfig.ps1

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -193,41 +193,49 @@ Function Get-ScriptPath {
193193
}
194194

195195

196-
Function Import-SMSTSENV{
197-
## Get the name of this function
198-
[string]${CmdletName} = $PSCmdlet.MyInvocation.MyCommand.Name
196+
Function Get-SMSTSENV{
197+
param([switch]$LogPath,[switch]$NoWarning)
199198

200-
try{
201-
# Create an object to access the task sequence environment
202-
$Script:tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
203-
#test if variables exist
204-
$tsenv.GetVariables() #| % { Write-Output "$ScriptName - $_ = $($tsenv.Value($_))" }
205-
}
206-
catch{
207-
If(${CmdletName}){$prefix = "${CmdletName} ::" }Else{$prefix = "" }
208-
Write-Warning ("{0}Task Sequence environment not detected. Running in stand-alone mode." -f $prefix)
209-
210-
#set variable to null
211-
$Script:tsenv = $null
199+
Begin{
200+
## Get the name of this function
201+
[string]${CmdletName} = $PSCmdlet.MyInvocation.MyCommand.Name
212202
}
213-
Finally{
214-
#set global Logpath
215-
if ($tsenv){
216-
#grab the progress UI
217-
$Script:TSProgressUi = New-Object -ComObject Microsoft.SMS.TSProgressUI
218-
219-
# Query the environment to get an existing variable
220-
# Set a variable for the task sequence log path
221-
#$Global:Logpath = $tsenv.Value("LogPath")
222-
$Global:LogPath = $tsenv.Value("_SMSTSLogPath")
223-
224-
# Or, convert all of the variables currently in the environment to PowerShell variables
225-
$tsenv.GetVariables() | % { Set-Variable -Name "$_" -Value "$($tsenv.Value($_))" }
203+
Process{
204+
try{
205+
# Create an object to access the task sequence environment
206+
$Script:tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
207+
#test if variables exist
208+
$tsenv.GetVariables() #| % { Write-Output "$ScriptName - $_ = $($tsenv.Value($_))" }
209+
}
210+
catch{
211+
If(${CmdletName}){$prefix = "${CmdletName} ::" }Else{$prefix = "" }
212+
If(!$NoWarning){Write-Warning ("{0}Task Sequence environment not detected. Running in stand-alone mode." -f $prefix)}
213+
214+
#set variable to null
215+
$Script:tsenv = $null
226216
}
227-
Else{
228-
$Global:LogPath = $env:TEMP
217+
Finally{
218+
#set global Logpath
219+
if ($tsenv){
220+
#grab the progress UI
221+
$Script:TSProgressUi = New-Object -ComObject Microsoft.SMS.TSProgressUI
222+
223+
# Query the environment to get an existing variable
224+
# Set a variable for the task sequence log path
225+
#$UseLogPath = $tsenv.Value("LogPath")
226+
$UseLogPath = $tsenv.Value("_SMSTSLogPath")
227+
228+
# Convert all of the variables currently in the environment to PowerShell variables
229+
$tsenv.GetVariables() | % { Set-Variable -Name "$_" -Value "$($tsenv.Value($_))" }
230+
}
231+
Else{
232+
$UseLogPath = $env:Temp
233+
}
229234
}
230235
}
236+
End{
237+
If($LogPath){return $UseLogPath}
238+
}
231239
}
232240

233241
Function Format-ElapsedTime($ts) {
@@ -1041,12 +1049,11 @@ Else{
10411049
$VerbosePreference = 'SilentlyContinue'
10421050
}
10431051

1044-
#Check if running in Task Sequence
1045-
Import-SMSTSENV
1046-
1052+
#build log name
10471053
[string]$FileName = $scriptBaseName +'.log'
1048-
$Global:LogFilePath = Join-Path $RelativeLogPath -ChildPath $FileName
1049-
Write-Host "Using log file: $LogFilePath" -ForegroundColor Cyan
1054+
#build global log fullpath
1055+
$Global:LogFilePath = Join-Path (Get-SMSTSENV -LogPath -NoWarning) -ChildPath $FileName
1056+
Write-Host "logging to file: $LogFilePath" -ForegroundColor Cyan
10501057

10511058
##*===========================================================================
10521059
##* DEFAULTS: Configurations are here (change values if needed)
@@ -1140,7 +1147,7 @@ Write-Host "Using log file: $LogFilePath" -ForegroundColor Cyan
11401147

11411148
# Configurations comes from Tasksequence
11421149
# When running in Tasksequence and configureation exists, use that instead
1143-
If($tsenv){
1150+
If(Get-SMSTSENV){
11441151
# Global Settings
11451152
If($tsenv:CFG_DisableConfigScript){[boolean]$DisableScript = [boolean]::Parse($tsenv.Value("CFG_DisableConfigScript"))}
11461153
If($tsenv:CFG_UseLGPOForConfigs){[boolean]$UseLGPO = [boolean]::Parse($tsenv.Value("CFG_UseLGPOForConfigs"))}

Win10STIGAndMitigations.ps1

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -88,43 +88,50 @@ Function Get-ScriptPath {
8888
}
8989
}
9090

91-
Function Import-SMSTSENV{
92-
## Get the name of this function
93-
[string]${CmdletName} = $PSCmdlet.MyInvocation.MyCommand.Name
91+
Function Get-SMSTSENV{
92+
param([switch]$LogPath,[switch]$NoWarning)
9493

95-
try{
96-
# Create an object to access the task sequence environment
97-
$Script:tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
98-
#test if variables exist
99-
$tsenv.GetVariables() #| % { Write-Output "$ScriptName - $_ = $($tsenv.Value($_))" }
100-
}
101-
catch{
102-
If(${CmdletName}){$prefix = "${CmdletName} ::" }Else{$prefix = "" }
103-
Write-Warning ("{0}Task Sequence environment not detected. Running in stand-alone mode." -f $prefix)
104-
105-
#set variable to null
106-
$Script:tsenv = $null
94+
Begin{
95+
## Get the name of this function
96+
[string]${CmdletName} = $PSCmdlet.MyInvocation.MyCommand.Name
10797
}
108-
Finally{
109-
#set global Logpath
110-
if ($tsenv){
111-
#grab the progress UI
112-
$Script:TSProgressUi = New-Object -ComObject Microsoft.SMS.TSProgressUI
113-
114-
# Query the environment to get an existing variable
115-
# Set a variable for the task sequence log path
116-
#$Global:Logpath = $tsenv.Value("LogPath")
117-
$Global:Logpath = $tsenv.Value("_SMSTSLogPath")
118-
119-
# Or, convert all of the variables currently in the environment to PowerShell variables
120-
$tsenv.GetVariables() | % { Set-Variable -Name "$_" -Value "$($tsenv.Value($_))" }
98+
Process{
99+
try{
100+
# Create an object to access the task sequence environment
101+
$Script:tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
102+
#test if variables exist
103+
$tsenv.GetVariables() #| % { Write-Output "$ScriptName - $_ = $($tsenv.Value($_))" }
121104
}
122-
Else{
123-
$Global:Logpath = $env:TEMP
105+
catch{
106+
If(${CmdletName}){$prefix = "${CmdletName} ::" }Else{$prefix = "" }
107+
If(!$NoWarning){Write-Warning ("{0}Task Sequence environment not detected. Running in stand-alone mode." -f $prefix)}
108+
109+
#set variable to null
110+
$Script:tsenv = $null
111+
}
112+
Finally{
113+
#set global Logpath
114+
if ($tsenv){
115+
#grab the progress UI
116+
$Script:TSProgressUi = New-Object -ComObject Microsoft.SMS.TSProgressUI
117+
118+
# Query the environment to get an existing variable
119+
# Set a variable for the task sequence log path
120+
#$UseLogPath = $tsenv.Value("LogPath")
121+
$UseLogPath = $tsenv.Value("_SMSTSLogPath")
122+
123+
# Convert all of the variables currently in the environment to PowerShell variables
124+
$tsenv.GetVariables() | % { Set-Variable -Name "$_" -Value "$($tsenv.Value($_))" }
125+
}
126+
Else{
127+
$UseLogPath = $env:Temp
128+
}
124129
}
125130
}
131+
End{
132+
If($LogPath){return $UseLogPath}
133+
}
126134
}
127-
128135
Function Format-ElapsedTime($ts) {
129136
$elapsedTime = ""
130137
if ( $ts.Minutes -gt 0 ){$elapsedTime = [string]::Format( "{0:00} min. {1:00}.{2:00} sec.", $ts.Minutes, $ts.Seconds, $ts.Milliseconds / 10 );}
@@ -737,12 +744,11 @@ Else{
737744
$VerbosePreference = 'SilentlyContinue'
738745
}
739746

740-
#Check if running in Task Sequence
741-
Import-SMSTSENV
742-
747+
#build log name
743748
[string]$FileName = $scriptBaseName +'.log'
744-
$Global:LogFilePath = Join-Path $RelativeLogPath -ChildPath $FileName
745-
Write-Host "Using log file: $LogFilePath" -ForegroundColor Cyan
749+
#build global log fullpath
750+
$Global:LogFilePath = Join-Path (Get-SMSTSENV -LogPath -NoWarning) -ChildPath $FileName
751+
Write-Host "logging to file: $LogFilePath" -ForegroundColor Cyan
746752

747753
##*===========================================================================
748754
##* DEFAULTS: Configurations are here (change values if needed)
@@ -758,7 +764,7 @@ Write-Host "Using log file: $LogFilePath" -ForegroundColor Cyan
758764
[boolean]$ApplyEMETMitigations = $false
759765

760766
# When running in Tasksequence and configureation exists, use that instead
761-
If($tsenv){
767+
If(Get-SMSTSENV){
762768
# Global Settings
763769
If($tsenv:CFG_DisableSTIGScript){[boolean]$DisableScript = [boolean]::Parse($tsenv.Value("CFG_DisableSTIGScript"))}
764770
If($tsenv:CFG_UseLGPOForConfigs){[boolean]$UseLGPO = [boolean]::Parse($tsenv.Value("CFG_UseLGPOForConfigs"))}

0 commit comments

Comments
 (0)