SetBIOS.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
<#
.Synopsis
    The Set-BIOS function allows you to change BIOS settings from your local or a remote computer or also multiple remote computers.
    You can change BIOS for the following manufacturer: Dell, HP, Lenovo.
    Settings to change should be located in a CSV file.
    If you need other manufacturer send me a mail at: damien.vanrobaeys@gmail.com
     
.DESCRIPTION
    The Set-BIOS function allows you to change BIOS settings from your local or a remote computer or also multiple remote computers.
    You can change BIOS for the following manufacturer: Dell, HP, Lenovo
     
    Settings to change should be located in a CSV file.
    The CSV file should contain two columns: Setting and Value, see below an example:
    Setting,Value
    Fast Boot,Enable
    Select Language, Francais
    Audio Alerts During Boot, Disable
     
    See below available parameters:
    -CSV "CSVPath": Path of the CSV file containing BIOS settings to change
    -Computer "ComputerName": Remote computer name on which you want to change settings
    -Multiple: Use it to change settings on multiple remote computers (Do not use -Computer with it)
    -Manufacturer "HP / Dell, Lenovo": To use with the -Multiple parameter. Type the manufacturer of the list of remote computers: Dell, HP or Lenovo.
    -List "ComputersList.txt": To use with the -Multiple parameter. Path of the TXT file that contains the list of remote computer name
    -Password: If a BIOS is setted. You will have to type the password once it's asked
 
.EXAMPLE
PS Root\> Set-BIOS
The command above will change BIOS settings on your local computer.
It will ask for the CSV path as below:
CSV PATH - Please enter the CSV path containing settings to change with new values:
 
.EXAMPLE
PS Root\> Set-BIOS -CSV "C:\BIOS_Settings.csv"
The command above will change BIOS settings on your local computer and apply settings values located in the file C:\BIOS_Settings.csv
 
.EXAMPLE
PS Root\> Set-BIOS Computer "MyComputer" -CSV "C:\BIOS_Settings.csv"
The command above will change BIOS settings on the computer MyComputer and apply settings values located in the file C:\BIOS_Settings.csv
It will prompt you to enter credentials to connect to the remote computer
 
.EXAMPLE
PS Root\> Set-BIOS -Computer "MyComputer" -CSV "C:\BIOS_Settings.csv" -Password PASSWORD
The command above will change BIOS settings on the computer MyComputer and apply settings values located in the file C:\BIOS_Settings.csv
It will ask for the BIOS password as below:
PASSWORD - Please enter your BIOS password: ************
 
.EXAMPLE
PS Root\> Set-BIOS -multiple -Manufacturer HP -List "C:\ComputersList.txt" -CSV "C:\BIOS_Settings.csv"
The command above will change BIOS settings for the list of HP computers located in C:\ComputersList.txt
It will apply setting values located in the file C:\BIOS_Settings.csv
 
.NOTES
    Author: Damien VAN ROBAEYS - @syst_and_deploy - http://www.systanddeploy.com
#>


function Set-Bios
{
    [CmdletBinding()]
    Param(
            [string]$Computer,
            [switch]$Password,    
            [switch]$Multiple,    
            [string]$List,
            [string]$Manufacturer,            
            [string]$CSV            
         )

    Begin
    {
        # Check if user has selected multiple switch
        If(($Multiple) -and ($Computer -ne ""))
            {
                write-host ""
                write-host "###############################################################" -Foreground yellow
                write-host "The Computer parameter can't be used with the muliple parameter" -Foreground yellow
                write-host "###############################################################" -Foreground yellow    
                write-host ""
                break                    
            }    
            
        # If user has selected both List and Computer parameter
        If(($List -ne "") -and ($Computer -ne ""))
            {
                write-host ""
                write-host "###############################################################" -Foreground yellow
                write-host "The Computer parameter can't be used with the muliple parameter" -Foreground yellow
                write-host "###############################################################" -Foreground yellow    
                write-host ""
                break                    
            }                
    
        # If user has typed a computer name
        If(($Computer -ne ""))
            {    
                # Check connection to the remote connection
                $Check_Connection = test-connection -computername $Computer -quiet -count 1
                If($Check_Connection -eq $true)
                    {
                        $Script:Creds = get-credential
                        $Get_Remote_Vendor = {(Get-WmiObject Win32_Computersystem).manufacturer}
                        $Script:Get_manufacturer = Invoke-Command -ComputerName $Computer -ScriptBlock $Get_Remote_Vendor -credential $Creds -ErrorVariable errmsg 2>$null    
                        If($errmsg -ne $null)
                            {
                                write-host "KO ==> Connexion KO on $Computer (Check your credentials)" -Foreground yellow          
                                break                                            
                            }
                    }
                Else
                    {
                        write-host "KO ==> Connexion KO on $Computer" -Foreground yellow              
                        break
                    }    
            }
        Else
            {
                # In this case user hasn't typed a computer name, meaning we will set bios on local computer
                $Script:Get_manufacturer = (Get-WmiObject Win32_Computersystem).manufacturer
            }
            
        # if user has selected password switch, we will use a BIOS password
        If($Password)
            {    
                $Script:BIOS_PWD = Read-Host -assecurestring "PASSWORD - Please enter your BIOS password"
                $Script:MyPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($BIOS_PWD))
            }    

        # If user has typed a CSV path
        If(($CSV -ne ""))
            {    
                $Script:Settings_CSV = $CSV
            }
        Else
            {
                # If not we will ask for the CSV path
                $Script:Settings_CSV = $CSV
                $Script:Settings_CSV = read-host "CSV PATH - Please enter the CSV path containing settings to change with new values"
            }
            
        # Test if CSV path exists
        If(!(test-path $Settings_CSV))
            {
                write-host "KO ==> Can not find file $Settings_CSV" -Foreground yellow              
                break                    
            }            

        # if user has selected the Multiple switch
        If($Multiple)
            {        
                If($Manufacturer -ne "")
                    {
                        $Script:Get_manufacturer = $Manufacturer
                    }
                Else
                    {
                        $Script:Get_manufacturer = read-host "MANUFACTURER - Please enter the manufacturer of remote computer"                     
                    }
                    
                If($List -ne "") 
                    {
                        $Script:Computers_List = $List
                    }
                Else
                    {
                        $Script:Computers_List = read-host "COMPUTERS LIST - Please enter the path of the TXT file containing computers list" 
                    }
                    
                If(!(test-path $Settings_CSV))
                    {
                        write-host "KO ==> Can not find file $Computers_List" -Foreground yellow              
                        break                    
                    }                        
                    
                $Script:Creds = get-credential 
            }                
            

        Function Set_Dell_BIOS_Settings
            {                        
                param
                (
                    [array]$CSV_Parameters,
                    [string]$MyPassword    
                )                
                
                If(($Muliple) -or ($Computer -ne ""))
                    {
                        $Exported_CSV = "c:\windows\temp\Exported_BIOS_Settings.csv"                                                                        
                        $CSV_Parameters | out-file -LiteralPath $Exported_CSV
                        $Get_CSV_Content = import-csv $Exported_CSV                                
                    }
                Else
                    {
                        $Script:Get_CSV_Content = import-csv $Settings_CSV                    
                    }

                $WarningPreference='silentlycontinue'
                If (Get-Module -ListAvailable -Name DellBIOSProvider)
                    {} 
                Else 
                    {
                        Install-Module -Name DellBIOSProvider -Force 
                    }         
                get-command -module DellBIOSProvider | out-null            

                $IsPasswordSet = (Get-Item -Path DellSmbios:\Security\IsAdminPasswordSet).currentvalue    
                If ((($MyPassword -eq $null) -or ($MyPassword -eq "")) -and ($IsPasswordSet -eq $true))                                
                    {
                        write-host ""
                        write-host "###############################################################" -Foreground yellow
                        write-host " A password is configured in your BIOS ($env:computername)" -Foreground yellow
                        write-host " Please add the -password parameter" -Foreground yellow                
                        write-host "###############################################################" -Foreground yellow    
                        write-host ""
                        break                                        
                    }
                    
                $Dell_BIOS = get-childitem -path DellSmbios:\ | foreach {
                get-childitem -path @("DellSmbios:\" + $_.Category)  | select-object attribute, currentvalue, possiblevalues, PSChildName
                }                         
                    
                ForEach($New_Setting in $Get_CSV_Content)
                    {    
                        $Setting_To_Set = $New_Setting.Setting    
                        $Setting_NewValue_To_Set = $New_Setting.Value    
                        ForEach($Current_Setting in $Dell_BIOS | Where {$_.attribute -eq $Setting_To_Set})
                            {    
                                $Attribute = $Current_Setting.attribute
                                $Setting_Cat = $Current_Setting.PSChildName
                                $Setting_Current_Value = $Current_Setting.CurrentValue
                
                                If (($IsPasswordSet -eq $true))
                                    {            
                                        $Password_To_Use = $MyPassword
                                        # & Set-Item -Path Dellsmbios:\$Setting_Cat\$Attribute -Value $Setting_NewValue_To_Set -Password $Password_To_Use
                                        & Set-Item -Path Dellsmbios:\$Setting_Cat\$Attribute -Value $Setting_NewValue_To_Set -Password $Password_To_Use -errorvariable ohgodanerror -ea silentlycontinue    
                                        If($ohgodanerror -ne $null)
                                            {
                                                write-host "KO ==> Can not change setting $Attribute (See below)" -Foreground Yellow    
                                                $ohgodanerror 
                                            }
                                        Else
                                            {
                                                write-host "OK ==> New value for $Attribute is $Setting_NewValue_To_Set"                                            
                                            }                                        
                                    }
                                Else
                                    {
                                        # & Set-Item -Path Dellsmbios:\$Setting_Cat\$Attribute -Value $Setting_NewValue_To_Set
                                        & Set-Item -Path Dellsmbios:\$Setting_Cat\$Attribute -Value $Setting_NewValue_To_Set -errorvariable ohgodanerror -ea silentlycontinue    
                                        If($ohgodanerror -ne $null)
                                            {
                                                write-host "KO ==> Can not change setting $Attribute (See below)" -Foreground Yellow    
                                                $ohgodanerror 
                                            }
                                        Else
                                            {
                                                write-host "OK ==> New value for $Attribute is $Setting_NewValue_To_Set"                                            
                                            }
                                    }                            
                            }                            
                    }    
                    
                If(($Muliple) -or ($Computer -ne ""))
                    {
                        remove-item $Exported_CSV -force                
                    }            
            }        


        Function Set_HP_BIOS_Settings
            {    
                Param
                (
                    [array]$CSV_Parameters,
                    [string]$MyPassword    
                )                
                
                If(($Muliple) -or ($Computer -ne ""))
                    {
                        $Exported_CSV = "c:\windows\temp\Exported_BIOS_Settings.csv"                                                                        
                        $CSV_Parameters | out-file -LiteralPath $Exported_CSV
                        $Get_CSV_Content = import-csv $Exported_CSV                        
                    }
                Else
                    {
                        $Script:Get_CSV_Content = import-csv $Settings_CSV                                                
                    }
                
                $IsPasswordSet = (Get-WmiObject -Namespace root/hp/instrumentedBIOS -Class hp_biossetting | Where {$_.Name -eq "Setup Password"}).IsSet                            
                If ((($MyPassword -eq $null) -or ($MyPassword -eq "")) -and ($IsPasswordSet -eq $true))                
                    {
                        write-host ""
                        write-host "###############################################################" -Foreground yellow
                        write-host " A password is configured in your BIOS ($env:computername)" -Foreground yellow
                        write-host " Please add the -password parameter" -Foreground yellow                
                        write-host "###############################################################" -Foreground yellow    
                        write-host ""
                        break                                        
                    }                

                $bios = Get-WmiObject -Namespace root/hp/instrumentedBIOS -Class HP_BIOSSettingInterface
                ForEach($Settings in $Get_CSV_Content)
                    {
                        $MySetting = $Settings.Setting
                        $NewValue = $Settings.Value        

                        If (($IsPasswordSet -eq 1))
                            {            
                                $Password_To_Use = "<utf-16/>"+$MyPassword
                                $Execute_Change_Action = $bios.setbiossetting("$MySetting", "$NewValue",$Password_To_Use)     
                                $Change_Return_Code = $Execute_Change_Action.return
                                If(($Change_Return_Code) -eq 0)                                
                                    {
                                        write-host "OK ==> New value for $MySetting is $NewValue"
                                    }
                                Else
                                    {
                                        write-host "KO ==> Can not change setting $MySetting (Return code $Change_Return_Code)" -Foreground Yellow
                                    }
                            }
                        Else
                            {
                                $Execute_Change_Action = $bios.setbiossetting("$MySetting", "$NewValue","")
                                $Change_Return_Code = $Execute_Change_Action.return
                                If(($Change_Return_Code) -eq 0)                                
                                    {
                                        write-host "OK ==> New value for $MySetting is $NewValue"
                                    }
                                Else
                                    {
                                        write-host "KO ==> Can not change setting $MySetting (Return code $Change_Return_Code)" -Foreground Yellow
                                    }                                
                            }
                    }    
                    
                If(($Muliple) -or ($Computer -ne ""))
                    {
                        remove-item $Exported_CSV -force                
                    }
            }


    
        Function Set_Lenovo_BIOS_Settings
            {
                Param
                (
                    [array]$CSV_Parameters,
                    [string]$MyPassword                                        
                )        

                If(($Muliple) -or ($Computer -ne ""))
                    {
                        $Exported_CSV = "c:\windows\temp\Exported_BIOS_Settings.csv"                                                                        
                        $CSV_Parameters | out-file -LiteralPath $Exported_CSV
                        $Get_CSV_Content = import-csv $Exported_CSV                        
                    }
                Else
                    {
                        $Script:Get_CSV_Content = import-csv $Settings_CSV                    
                    }

                $IsPasswordSet = (gwmi -Class Lenovo_BiosPasswordSettings -Namespace root\wmi).PasswordState
                If ((($MyPassword -eq $null) -or ($MyPassword -eq "")) -and ($IsPasswordSet -eq 2))                                
                    {
                        write-host ""
                        write-host "###############################################################" -Foreground yellow
                        write-host " A password is configured in your BIOS ($env:computername)" -Foreground yellow
                        write-host " Please add the -password parameter" -Foreground yellow                
                        write-host "###############################################################" -Foreground yellow    
                        write-host ""
                        break                                        
                    }    

                $BIOS = gwmi -class Lenovo_SetBiosSetting -namespace root\wmi
                $SAVE_BIOS = (gwmi -class Lenovo_SaveBiosSettings -namespace root\wmi)
                ForEach($Settings in $Get_CSV_Content)
                    {
                        $MySetting = $Settings.Setting
                        $NewValue = $Settings.Value        
                        
                        If (($IsPasswordSet -eq 2))
                            {                                        
                                $Script:Password_To_Use = $MyPassword                                    
                                $Execute_Change_Action = $bios.SetBiosSetting("$MySetting,$NewValue,$Password_To_Use,ascii,us") 
                                $Change_Return_Code = $Execute_Change_Action.return 
                                If(($Change_Return_Code) -eq "Success")                                
                                    {
                                        write-host "OK ==> New value for $MySetting is $NewValue"
                                    }
                                Else
                                    {
                                        write-host "KO ==> Can not change setting $MySetting (Return code $Change_Return_Code)" -Foreground Yellow
                                    }                                
                            }
                        Else
                            {
                                $Execute_Change_Action = $BIOS.SetBiosSetting("$MySetting,$NewValue") 
                                $Change_Return_Code = $Execute_Change_Action.return 
                                If(($Change_Return_Code) -eq "Success")                                
                                    {
                                        write-host "OK ==> New value for $MySetting is $NewValue"
                                    }
                                Else
                                    {
                                        write-host "KO ==> Can not change setting $MySetting (Return code $Change_Return_Code)" -Foreground Yellow
                                    }                                    
                            }                        
                    }
                    
                If (($IsPasswordSet -eq 2))
                    {    
                        $SAVE_BIOS.SaveBiosSettings("$Password_To_Use,ascii,us")                                                                        
                    }
                Else
                    {
                        $SAVE_BIOS.SaveBiosSettings()                                                    
                    }
                    
                If(($Muliple) -or ($Computer -ne ""))
                    {
                        remove-item $Exported_CSV -force                
                    }                    
            }

        If($Get_manufacturer -like "*dell*")
            {
                $manufacturer = "Dell"
            }
        ElseIf($Get_manufacturer -like "*lenovo*")
            {
                $manufacturer = "Lenovo"
            }
        ElseIf(($Get_manufacturer -like "*HP*") -or ($Get_manufacturer -like "*hewlet*"))
            {
                $manufacturer = "HP"
            }
        ElseIf($Get_manufacturer -like "*toshiba*")
            {
                # $manufacturer = "Toshiba"
                write-host ""
                write-host "########################################################" -Foreground yellow
                write-host " Your manufacturer will be soon supported" -Foreground yellow
                write-host "########################################################" -Foreground yellow    
                write-host ""
                break                
            }
        Else
            {
                write-host ""
                write-host "########################################################" -Foreground yellow
                write-host " Your manufacturer is not supported by the module" -Foreground yellow
                write-host " Supported manufacturer: Dell, HP, Lenovo, Toshiba" -Foreground yellow                
                write-host "########################################################" -Foreground yellow    
                write-host ""
                break
            }
    }

    Process
    {
        write-host ""
        write-host "###################################################" -Foreground Cyan
        write-host " Your manufacturer is $manufacturer" -Foreground Cyan
        write-host "###################################################" -Foreground Cyan    
        write-host ""        

       switch ($manufacturer)
       {
           'Dell'
           {
                $Global:CSV_Content = get-content $Settings_CSV    

                If($Multiple) # Meaning you want to export from a list of computer
                    {
                        $Get_List_Content = Get-Content $Computers_List
                        $Script:Dest_Path = "c:\windows\temp"                            
                        ForEach($Computer_Line in $Get_List_Content)
                            {
                            
                                write-host ""
                                write-host "Working on $Computer_Line" -Foreground green     
                                $Check_Connection = test-connection -computername $Computer_Line -quiet -count 1
                                If($Check_Connection -eq $true)
                                    {
                                        Invoke-Command -ComputerName $Computer_Line -ScriptBlock ${Function:Set_Dell_BIOS_Settings} -ArgumentList (,$CSV_Content), $MyPassword -credential $Creds                                                                        
                                    }
                                Else
                                    {
                                        write-host "Connexion KO on $Computer_Line" -Foreground yellow                                  
                                    }                                
                            
                            }
                    }
                ElseIf(($Computer -ne ""))
                    {                        
                        Invoke-Command -ComputerName $Computer -ScriptBlock ${Function:Set_Dell_BIOS_Settings} -ArgumentList (,$CSV_Content), $MyPassword -credential $Creds                                
                    }
                Else
                    {
                        Set_Dell_BIOS_Settings $CSV_Content $MyPassword    
                    }                    

           }
           'HP'
           {
             Try
                {        
                    $Script:CSV_Content = get-content $Settings_CSV    
                    If($Multiple) # Meaning you want to export from a list of computer
                        {                                        
                            $Get_List_Content = Get-Content $Computers_List
                            $Script:Dest_Path = "c:\windows\temp"    
                            ForEach($Computer_Line in $Get_List_Content)
                                {                                
                                    write-host ""
                                    write-host "Working on $Computer_Line" -Foreground green     
                                    $Check_Connection = test-connection -computername $Computer_Line -quiet -count 1
                                    If($Check_Connection -eq $true)
                                        {
                                            Invoke-Command -ComputerName $Computer_Line -ScriptBlock ${Function:Set_HP_BIOS_Settings} -ArgumentList (,$CSV_Content), $MyPassword -credential $Creds        
                                        }
                                    Else
                                        {
                                            write-host "Connexion KO on $Computer_Line" -Foreground yellow                                  
                                        }                                    
                                }
                        }                            
                    ElseIf(($Computer -ne ""))
                        {                        

                            Invoke-Command -ComputerName $Computer -ScriptBlock ${Function:Set_HP_BIOS_Settings} -ArgumentList (,$CSV_Content), $MyPassword -credential $Creds                                        
                        }
                    Else
                        {                        
                            Set_HP_BIOS_Settings $CSV_Content $MyPassword    
                        }
                }
                    Catch
                    {}
           }
           'Lenovo'
           {
                $Global:CSV_Content = get-content $Settings_CSV    
                If($Multiple) # Meaning you want to export from a list of computer
                    {
                        $Get_List_Content = Get-Content $Computers_List
                        $Script:Dest_Path = "c:\windows\temp"                            
                        ForEach($Computer_Line in $Get_List_Content)
                            {
                                write-host ""
                                write-host "Working on $Computer_Line" -Foreground green                
                                $Check_Connection = test-connection -computername $Computer_Line -quiet -count 1
                                If($Check_Connection -eq $true)
                                    {
                                        Invoke-Command -ComputerName $Computer_Line -ScriptBlock ${Function:Set_Lenovo_BIOS_Settings} -ArgumentList (,$CSV_Content), $MyPassword -credential $Creds        
                                    }
                                Else
                                    {
                                        write-host "Connexion KO on $Computer_Line" -Foreground yellow                                  
                                    }    
                            }
                    }
                ElseIf(($Computer -ne ""))
                    {
                        Invoke-Command -ComputerName $Computer -ScriptBlock ${Function:Set_Lenovo_BIOS_Settings} -ArgumentList (,$CSV_Content), $MyPassword -credential $Creds                                                        
                    }
                Else
                    {
                        Set_Lenovo_BIOS_Settings $CSV_Content $MyPassword    
                    }    
           }
       }
    }
    End
    {
    }
}