├── DeleteComputers.ps1 ├── LoadComputers.ps1 ├── MDTDB.psm1 ├── README.md └── computers.csv /DeleteComputers.ps1: -------------------------------------------------------------------------------- 1 | # Script to import computer information from a .csv file and 2 | # delete the info in the MDT database 3 | # 4 | # Script requires the MDTDB.psm1 writen by Michael Niehaus and assumes 5 | # that it is located in the same directory. 6 | # 7 | # Written by Vaughn Miller 8 | 9 | #Import the module that allows us to interact with MDT 10 | Import-Module -name .\MDTDB.psm1 11 | 12 | #Open a connection to our database 13 | Connect-MDTDatabase -sqlServer deploy.example.com -instance ADSQL -database msdeploy 14 | 15 | #Read in the csv file 16 | $machines = Import-Csv .\computers.csv 17 | 18 | #Set up the log file information and initialize counters 19 | 20 | $date = get-date -format MMddyyHHmmss 21 | $logfile = ".\logs\Import"+$date+".log" 22 | $validrecords = 0 23 | $invalidrecords = 0 24 | 25 | 26 | #Loop through each record from the file and process the information 27 | 28 | For ($i=1; $i -le $machines.count; $i++) 29 | { 30 | $machines[$i-1].mac=$machines[$i-1].mac.ToUpper() 31 | $macvalid=$TRUE 32 | $temp = $machines[$i-1].mac.Split(":") 33 | if (($temp.count -ne 6) -or ($machines[$i-1].mac.length -ne 17)) 34 | { $macvalid = $FALSE 35 | } 36 | 37 | $namevalid=$TRUE 38 | if ($machines[$i-1].name.length -gt 15) 39 | { $namevalid=$FALSE 40 | } 41 | 42 | if ($namevalid -and $macvalid) 43 | { 44 | $validrecords=$validrecords+1 45 | $machineid=Get-MDTComputer -macAddress $machines[$i-1].mac 46 | if ($machineid.id -gt 0) 47 | { 48 | Remove-MDTComputer $machineid.id 49 | } 50 | } 51 | else 52 | { #log the invalid record 53 | $invalidrecords=$invalidrecords+1 54 | $text = "Invalid Record : "+$i+" "+$machines[$i-1].mac+" "+$machines[$i-1].name 55 | $text >> $logfile 56 | } 57 | } 58 | #Some closing information for the logfile 59 | 60 | " " >> $logfile 61 | $text="Total records processed = "+($invalidrecords+$validrecords) 62 | $text>>$logfile 63 | $text="Invalid records = "+$invalidrecords 64 | $text>>$logfile 65 | $text="Valid records = "+$validrecords 66 | $text>>$logfile 67 | 68 | #Let the user know the script is finished and where the log file is 69 | " " 70 | " " 71 | "Script execution Complete" 72 | "Check "+$logfile+" "+"for invalid records" -------------------------------------------------------------------------------- /LoadComputers.ps1: -------------------------------------------------------------------------------- 1 | # Script to import computer information from a .csv file and 2 | # load the info into the MDT database 3 | # 4 | # Script requires the MDTDB.psm1 writen by Michael Niehaus and assumes 5 | # that it is located in the same directory. 6 | # 7 | # Written by Vaughn Miller 8 | 9 | #Import the module that allows us to interact with MDT 10 | Import-Module -name .\MDTDB.psm1 11 | 12 | #Open a connection to our database 13 | Connect-MDTDatabase -sqlServer deploy.example.com -instance ADSQL -database msdeploy 14 | 15 | #Read in the csv file 16 | $machines = Import-Csv .\computers.csv 17 | 18 | #Set up the log file information and initialize counters 19 | 20 | $date = get-date -format MMddyyHHmmss 21 | $logfile = ".\logs\Import"+$date+".log" 22 | $validrecords = 0 23 | $invalidrecords = 0 24 | 25 | 26 | #Loop through each record from the file and process the information 27 | 28 | For ($i=1; $i -le $machines.count; $i++) 29 | { 30 | $machines[$i-1].mac=$machines[$i-1].mac.ToUpper() 31 | $macvalid=$TRUE 32 | $temp = $machines[$i-1].mac.Split(":") 33 | if (($temp.count -ne 6) -or ($machines[$i-1].mac.length -ne 17)) 34 | { $macvalid = $FALSE 35 | } 36 | 37 | $namevalid=$TRUE 38 | if ($machines[$i-1].name.length -gt 15) 39 | { $namevalid=$FALSE 40 | } 41 | 42 | if ($namevalid -and $macvalid) 43 | { 44 | $validrecords=$validrecords+1 45 | $machineid=Get-MDTComputer -macAddress $machines[$i-1].mac 46 | if ($machineid.id -gt 0) 47 | { 48 | Remove-MDTComputer $machineid.id 49 | } 50 | New-MDTComputer -macAddress $machines[$i-1].mac -description $machines[$i-1].name -settings @{ 51 | OSInstall='YES'; 52 | OSDComputerName=$machines[$i-1].name; 53 | AdminPassword=$machines[$i-1].password; 54 | } 55 | $machineid=Get-MDTComputer -macAddress $machines[$i-1].mac 56 | Set-MDTComputerRole $machineid.id $machines[$i-1].role 57 | } 58 | else 59 | { #log the invalid record 60 | $invalidrecords=$invalidrecords+1 61 | $text = "Invalid Record : "+$i+" "+$machines[$i-1].mac+" "+$machines[$i-1].name 62 | $text >> $logfile 63 | } 64 | } 65 | #Some closing information for the logfile 66 | 67 | " " >> $logfile 68 | $text="Total records processed = "+($invalidrecords+$validrecords) 69 | $text>>$logfile 70 | $text="Invalid records = "+$invalidrecords 71 | $text>>$logfile 72 | $text="Valid records = "+$validrecords 73 | $text>>$logfile 74 | 75 | #Let the user know the script is finished and where the log file is 76 | " " 77 | " " 78 | "Script execution Complete" 79 | "Check "+$logfile+" "+"for invalid records" -------------------------------------------------------------------------------- /MDTDB.psm1: -------------------------------------------------------------------------------- 1 | # *************************************************************************** 2 | # 3 | # This version was forked from Michael Niehaus's original by Vaughn Miller. 4 | # Same disclaimer applies, it is provided as is with no waranty 5 | # 6 | # *************************************************************************** 7 | # 8 | # File: MDTDB.psm1 9 | # 10 | # Author: Michael Niehaus 11 | # 12 | # Purpose: Provides a set of PowerShell advanced functions (cmdlets) to 13 | # manipulate the Microsoft Deployment Toolkit database contents. 14 | # This required at least PowerShell 2.0 CTP3. 15 | # 16 | # Usage: This script must be imported using "import-module", e.g.: 17 | # import-module C:\MDTDB.psm1 18 | # After it has been imported, the indivual functions below can be 19 | # used. For details on the parameters each one takes, you can 20 | # use "get-help", e.g. "get-help Connect-MDTDatabase". Note that 21 | # there is no detailed help provided on the cmdlets. Feel free to 22 | # add your own... 23 | # 24 | # ------------- DISCLAIMER ------------------------------------------------- 25 | # This script code is provided as is with no guarantee or waranty concerning 26 | # the usability or impact on systems and may be used, distributed, and 27 | # modified in any way provided the parties agree and acknowledge the 28 | # Microsoft or Microsoft Partners have neither accountabilty or 29 | # responsibility for results produced by use of this script. 30 | # 31 | # Microsoft will not provide any support through any means. 32 | # ------------- DISCLAIMER ------------------------------------------------- 33 | # 34 | # *************************************************************************** 35 | 36 | # --------------------------------------------------------------------- 37 | # Helper functions (not intended to be called directly) 38 | # --------------------------------------------------------------------- 39 | # 40 | 41 | function Clear-MDTArray { 42 | 43 | PARAM 44 | ( 45 | $id, 46 | $type, 47 | $table 48 | ) 49 | 50 | # Build the delete command 51 | $delCommand = "DELETE FROM $table WHERE ID = $id and Type = '$type'" 52 | 53 | # Issue the delete command 54 | Write-Verbose "About to issue command: $delCommand" 55 | $cmd = New-Object System.Data.SqlClient.SqlCommand($delCommand, $mdtSQLConnection) 56 | $null = $cmd.ExecuteScalar() 57 | 58 | Write-Host "Removed all records from $table for Type = $type and ID = $id." 59 | } 60 | 61 | function Get-MDTArray { 62 | 63 | PARAM 64 | ( 65 | $id, 66 | $type, 67 | $table, 68 | $column 69 | ) 70 | 71 | # Build the select command 72 | $sql = "SELECT $column FROM $table WHERE ID = $id AND Type = '$type' ORDER BY Sequence" 73 | 74 | # Issue the select command and return the results 75 | Write-Verbose "About to issue command: $sql" 76 | $selectAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($sql, $mdtSQLConnection) 77 | $selectDataset = New-Object System.Data.Dataset 78 | $null = $selectAdapter.Fill($selectDataset, "$table") 79 | $selectDataset.Tables[0].Rows 80 | } 81 | 82 | function Set-MDTArray { 83 | 84 | PARAM 85 | ( 86 | $id, 87 | $type, 88 | $table, 89 | $column, 90 | $array 91 | ) 92 | 93 | # First clear the existing array 94 | Clear-MDTArray $id $type $table 95 | 96 | # Now insert each row in the array 97 | $seq = 1 98 | foreach ($item in $array) 99 | { 100 | # Insert the row 101 | $sql = "INSERT INTO $table (Type, ID, Sequence, $column) VALUES ('$type', $id, $seq, '$item')" 102 | Write-Verbose "About to execute command: $sql" 103 | $settingsCmd = New-Object System.Data.SqlClient.SqlCommand($sql, $mdtSQLConnection) 104 | $null = $settingsCmd.ExecuteScalar() 105 | 106 | # Increment the counter 107 | $seq = $seq + 1 108 | } 109 | 110 | Write-Host "Added records to $table for Type = $type and ID = $id." 111 | } 112 | 113 | # --------------------------------------------------------------------- 114 | # Connection function 115 | # --------------------------------------------------------------------- 116 | 117 | function Connect-MDTDatabase { 118 | 119 | [CmdletBinding()] 120 | PARAM 121 | ( 122 | [Parameter(Position=1)] $drivePath = "", 123 | [Parameter()] $sqlServer, 124 | [Parameter()] $instance = "", 125 | [Parameter()] $database 126 | ) 127 | 128 | # If $mdtDatabase exists from a previous execution, clear it 129 | if ($mdtDatabase) 130 | { 131 | Clear-Variable -name mdtDatabase 132 | } 133 | 134 | # If a drive path is specified, use PowerShell to build the connection string. 135 | # Otherwise, build it from the other parameters 136 | if ($drivePath -ne "") 137 | { 138 | # Get the needed properties to build the connection string 139 | $mdtProperties = get-itemproperty $drivePath 140 | 141 | $mdtSQLConnectString = "Server=$($mdtProperties.'Database.SQLServer')" 142 | if ($mdtProperties."Database.Instance" -ne "") 143 | { 144 | $mdtSQLConnectString = "$mdtSQLConnectString\$($mdtProperties.'Database.Instance')" 145 | } 146 | $mdtSQLConnectString = "$mdtSQLConnectString; Database='$($mdtProperties.'Database.Name')'; Integrated Security=true;" 147 | } 148 | else 149 | { 150 | $mdtSQLConnectString = "Server=$($sqlServer)" 151 | if ($instance -ne "") 152 | { 153 | $mdtSQLConnectString = "$mdtSQLConnectString\$instance" 154 | } 155 | $mdtSQLConnectString = "$mdtSQLConnectString; Database='$database'; Integrated Security=true;" 156 | } 157 | 158 | # Make the connection and save it in a global variable 159 | Write-Host "Connecting to: $mdtSQLConnectString" 160 | $global:mdtSQLConnection = new-object System.Data.SqlClient.SqlConnection 161 | $global:mdtSQLConnection.ConnectionString = $mdtSQLConnectString 162 | $global:mdtSQLConnection.Open() 163 | } 164 | 165 | # --------------------------------------------------------------------- 166 | # Computer functions 167 | # --------------------------------------------------------------------- 168 | 169 | function New-MDTComputer { 170 | 171 | [CmdletBinding()] 172 | PARAM 173 | ( 174 | [Parameter(ValueFromPipelineByPropertyName=$true)] $assetTag, 175 | [Parameter(ValueFromPipelineByPropertyName=$true)] $macAddress, 176 | [Parameter(ValueFromPipelineByPropertyName=$true)] $serialNumber, 177 | [Parameter(ValueFromPipelineByPropertyName=$true)] $uuid, 178 | [Parameter(ValueFromPipelineByPropertyName=$true)] $description, 179 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $settings 180 | ) 181 | 182 | Process 183 | { 184 | # Insert a new computer row and get the identity result 185 | $sql = "INSERT INTO ComputerIdentity (AssetTag, SerialNumber, MacAddress, UUID, Description) VALUES ('$assetTag', '$serialNumber', '$macAddress', '$uuid', '$description') SELECT @@IDENTITY" 186 | Write-Verbose "About to execute command: $sql" 187 | $identityCmd = New-Object System.Data.SqlClient.SqlCommand($sql, $mdtSQLConnection) 188 | $identity = $identityCmd.ExecuteScalar() 189 | Write-Verbose "Added computer identity record" 190 | 191 | # Insert the settings row, adding the values as specified in the hash table 192 | $settingsColumns = $settings.Keys -join "," 193 | $settingsValues = $settings.Values -join "','" 194 | $sql = "INSERT INTO Settings (Type, ID, $settingsColumns) VALUES ('C', $identity, '$settingsValues')" 195 | Write-Verbose "About to execute command: $sql" 196 | $settingsCmd = New-Object System.Data.SqlClient.SqlCommand($sql, $mdtSQLConnection) 197 | $null = $settingsCmd.ExecuteScalar() 198 | 199 | Write-Host "Added settings for the specified computer" 200 | 201 | # Write the new record back to the pipeline 202 | Get-MDTComputer -ID $identity 203 | } 204 | } 205 | 206 | function Get-MDTComputer { 207 | 208 | [CmdletBinding()] 209 | PARAM 210 | ( 211 | [Parameter(ValueFromPipelineByPropertyName=$true)] $id = "", 212 | [Parameter(ValueFromPipelineByPropertyName=$true)] $assetTag = "", 213 | [Parameter(ValueFromPipelineByPropertyName=$true)] $macAddress = "", 214 | [Parameter(ValueFromPipelineByPropertyName=$true)] $serialNumber = "", 215 | [Parameter(ValueFromPipelineByPropertyName=$true)] $uuid = "", 216 | [Parameter(ValueFromPipelineByPropertyName=$true)] $description = "" 217 | ) 218 | 219 | Process 220 | { 221 | # Build a select statement based on what parameters were specified 222 | if ($id -eq "" -and $assetTag -eq "" -and $macAddress -eq "" -and $serialNumber -eq "" -and $uuid -eq "" -and $description -eq "") 223 | { 224 | $sql = "SELECT * FROM ComputerSettings" 225 | } 226 | elseif ($id -ne "") 227 | { 228 | $sql = "SELECT * FROM ComputerSettings WHERE ID = $id" 229 | } 230 | else 231 | { 232 | # Specified the initial command 233 | $sql = "SELECT * FROM ComputerSettings WHERE " 234 | 235 | # Add the appropriate where clauses 236 | if ($assetTag -ne "") 237 | { 238 | $sql = "$sql AssetTag='$assetTag' AND" 239 | } 240 | 241 | if ($macAddress -ne "") 242 | { 243 | $sql = "$sql MacAddress='$macAddress' AND" 244 | } 245 | 246 | if ($serialNumber -ne "") 247 | { 248 | $sql = "$sql SerialNumber='$serialNumber' AND" 249 | } 250 | 251 | if ($uuid -ne "") 252 | { 253 | $sql = "$sql UUID='$uuid' AND" 254 | } 255 | 256 | if ($description -ne "") 257 | { 258 | $sql = "$sql Description='$description' AND" 259 | } 260 | 261 | # Chop off the last " AND" 262 | $sql = $sql.Substring(0, $sql.Length - 4) 263 | } 264 | 265 | $selectAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($sql, $mdtSQLConnection) 266 | $selectDataset = New-Object System.Data.Dataset 267 | $null = $selectAdapter.Fill($selectDataset, "ComputerSettings") 268 | $selectDataset.Tables[0].Rows 269 | } 270 | } 271 | 272 | function Set-MDTComputer { 273 | 274 | [CmdletBinding()] 275 | PARAM 276 | ( 277 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id, 278 | [Parameter(Mandatory=$true)] $settings 279 | ) 280 | 281 | Process 282 | { 283 | # Add each each hash table entry to the update statement 284 | $sql = "UPDATE Settings SET" 285 | foreach ($setting in $settings.GetEnumerator()) 286 | { 287 | $sql = "$sql $($setting.Key) = '$($setting.Value)', " 288 | } 289 | 290 | # Chop off the trailing ", " 291 | $sql = $sql.Substring(0, $sql.Length - 2) 292 | 293 | # Add the where clause 294 | $sql = "$sql WHERE ID = $id AND Type = 'C'" 295 | 296 | # Execute the command 297 | Write-Verbose "About to execute command: $sql" 298 | $settingsCmd = New-Object System.Data.SqlClient.SqlCommand($sql, $mdtSQLConnection) 299 | $null = $settingsCmd.ExecuteScalar() 300 | 301 | Write-Host "Added settings for the specified computer" 302 | 303 | # Write the updated record back to the pipeline 304 | Get-MDTComputer -ID $id 305 | } 306 | } 307 | 308 | function Remove-MDTComputer { 309 | 310 | [CmdletBinding()] 311 | PARAM 312 | ( 313 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 314 | ) 315 | 316 | Process 317 | { 318 | # Build the delete command 319 | $delCommand = "DELETE FROM ComputerIdentity WHERE ID = $id" 320 | 321 | # Issue the delete command 322 | Write-Verbose "About to issue command: $delCommand" 323 | $cmd = New-Object System.Data.SqlClient.SqlCommand($delCommand, $mdtSQLConnection) 324 | $null = $cmd.ExecuteScalar() 325 | 326 | Write-Host "Removed the computer with ID = $id." 327 | } 328 | } 329 | 330 | function Set-MDTComputerIdentity { 331 | [CmdletBinding()] 332 | param 333 | ( 334 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id, 335 | [Parameter(Mandatory=$true)] [Hashtable]$settings 336 | ) 337 | 338 | Process 339 | { 340 | # Add each each hash table entry to the update statement 341 | $sql = "UPDATE ComputerIdentity SET" 342 | foreach ($setting in $settings.GetEnumerator()) 343 | { 344 | $sql = "$sql $($setting.Key) = '$($setting.Value)', " 345 | } 346 | 347 | # Chop off the trailing ", " 348 | $sql = $sql.Substring(0, $sql.Length - 2) 349 | 350 | # Add the where clause 351 | $sql = "$sql WHERE ID = $id" 352 | 353 | # Execute the command 354 | Write-Verbose "About to execute command: $sql" 355 | $settingsCmd = New-Object System.Data.SqlClient.SqlCommand($sql, $mdtSQLConnection) 356 | $null = $settingsCmd.ExecuteScalar() 357 | 358 | Write-Verbose "Update settings for the specified computer" 359 | 360 | # Write the updated record back to the pipeline 361 | Get-MDTComputer -ID $id 362 | } 363 | } 364 | 365 | 366 | function Get-MDTComputerApplication { 367 | 368 | [CmdletBinding()] 369 | PARAM 370 | ( 371 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 372 | ) 373 | 374 | Process 375 | { 376 | Get-MDTArray $id 'C' 'Settings_Applications' 'Applications' 377 | } 378 | } 379 | 380 | function Clear-MDTComputerApplication { 381 | 382 | [CmdletBinding()] 383 | PARAM 384 | ( 385 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 386 | ) 387 | 388 | Process 389 | { 390 | Clear-MDTArray $id 'C' 'Settings_Applications' 391 | } 392 | } 393 | 394 | function Set-MDTComputerApplication { 395 | 396 | [CmdletBinding()] 397 | PARAM 398 | ( 399 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id, 400 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $applications 401 | ) 402 | 403 | Process 404 | { 405 | Set-MDTArray $id 'C' 'Settings_Applications' 'Applications' $applications 406 | } 407 | } 408 | 409 | function Get-MDTComputerPackage { 410 | 411 | [CmdletBinding()] 412 | PARAM 413 | ( 414 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 415 | ) 416 | 417 | Process 418 | { 419 | Get-MDTArray $id 'C' 'Settings_Packages' 'Packages' 420 | } 421 | } 422 | 423 | function Clear-MDTComputerPackage { 424 | 425 | [CmdletBinding()] 426 | PARAM 427 | ( 428 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 429 | ) 430 | 431 | Process 432 | { 433 | Clear-MDTArray $id 'C' 'Settings_Packages' 434 | } 435 | } 436 | 437 | function Set-MDTComputerPackage { 438 | 439 | [CmdletBinding()] 440 | PARAM 441 | ( 442 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id, 443 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $packages 444 | ) 445 | 446 | Process 447 | { 448 | Set-MDTArray $id 'C' 'Settings_Packages' 'Packages' $packages 449 | } 450 | } 451 | 452 | function Get-MDTComputerRole { 453 | 454 | [CmdletBinding()] 455 | PARAM 456 | ( 457 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 458 | ) 459 | 460 | Process 461 | { 462 | Get-MDTArray $id 'C' 'Settings_Roles' 'Role' 463 | } 464 | } 465 | 466 | function Clear-MDTComputerRole { 467 | 468 | [CmdletBinding()] 469 | PARAM 470 | ( 471 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 472 | ) 473 | 474 | Process 475 | { 476 | Clear-MDTArray $id 'C' 'Settings_Roles' 477 | } 478 | } 479 | 480 | function Set-MDTComputerRole { 481 | 482 | [CmdletBinding()] 483 | PARAM 484 | ( 485 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id, 486 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $roles 487 | ) 488 | 489 | Process 490 | { 491 | Set-MDTArray $id 'C' 'Settings_Roles' 'Role' $roles 492 | } 493 | } 494 | 495 | function Get-MDTComputerAdministrator { 496 | 497 | [CmdletBinding()] 498 | PARAM 499 | ( 500 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 501 | ) 502 | 503 | Process 504 | { 505 | Get-MDTArray $id 'C' 'Settings_Administrators' 'Administrators' 506 | } 507 | } 508 | 509 | function Clear-MDTComputerAdministrator { 510 | 511 | [CmdletBinding()] 512 | PARAM 513 | ( 514 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 515 | ) 516 | 517 | Process 518 | { 519 | Clear-MDTArray $id 'C' 'Settings_Administrators' 520 | } 521 | } 522 | 523 | function Set-MDTComputerAdministrator { 524 | 525 | [CmdletBinding()] 526 | PARAM 527 | ( 528 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id, 529 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $administrators 530 | ) 531 | 532 | Process 533 | { 534 | Set-MDTArray $id 'C' 'Settings_Administrators' 'Administrators' $administrators 535 | } 536 | } 537 | 538 | # --------------------------------------------------------------------- 539 | # Role functions 540 | # --------------------------------------------------------------------- 541 | 542 | function New-MDTRole { 543 | 544 | [CmdletBinding()] 545 | PARAM 546 | ( 547 | [Parameter(ValueFromPipelineByPropertyName=$true)] $name, 548 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $settings 549 | ) 550 | 551 | Process 552 | { 553 | # Insert a new role row and get the identity result 554 | $sql = "INSERT INTO RoleIdentity (Role) VALUES ('$name') SELECT @@IDENTITY" 555 | Write-Verbose "About to execute command: $sql" 556 | $identityCmd = New-Object System.Data.SqlClient.SqlCommand($sql, $mdtSQLConnection) 557 | $identity = $identityCmd.ExecuteScalar() 558 | Write-Verbose "Added role identity record" 559 | 560 | # Insert the settings row, adding the values as specified in the hash table 561 | $settingsColumns = $settings.Keys -join "," 562 | $settingsValues = $settings.Values -join "','" 563 | $sql = "INSERT INTO Settings (Type, ID, $settingsColumns) VALUES ('R', $identity, '$settingsValues')" 564 | Write-Verbose "About to execute command: $sql" 565 | $settingsCmd = New-Object System.Data.SqlClient.SqlCommand($sql, $mdtSQLConnection) 566 | $null = $settingsCmd.ExecuteScalar() 567 | 568 | Write-Host "Added settings for the specified role" 569 | 570 | # Write the new record back to the pipeline 571 | Get-MDTRole -ID $identity 572 | } 573 | } 574 | 575 | function Get-MDTRole { 576 | 577 | [CmdletBinding()] 578 | PARAM 579 | ( 580 | [Parameter(ValueFromPipelineByPropertyName=$true)] $id = "", 581 | [Parameter(ValueFromPipelineByPropertyName=$true)] $name = "" 582 | ) 583 | 584 | Process 585 | { 586 | # Build a select statement based on what parameters were specified 587 | if ($id -eq "" -and $name -eq "") 588 | { 589 | $sql = "SELECT * FROM RoleSettings" 590 | } 591 | elseif ($id -ne "") 592 | { 593 | $sql = "SELECT * FROM RoleSettings WHERE ID = $id" 594 | } 595 | else 596 | { 597 | $sql = "SELECT * FROM RoleSettings WHERE Role = '$name'" 598 | } 599 | 600 | # Execute the statement and return the results 601 | $selectAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($sql, $mdtSQLConnection) 602 | $selectDataset = New-Object System.Data.Dataset 603 | $null = $selectAdapter.Fill($selectDataset, "RoleSettings") 604 | $selectDataset.Tables[0].Rows 605 | } 606 | } 607 | 608 | function Set-MDTRole { 609 | 610 | [CmdletBinding()] 611 | PARAM 612 | ( 613 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id, 614 | [Parameter(Mandatory=$true)] $settings 615 | ) 616 | 617 | Process 618 | { 619 | # Add each each hash table entry to the update statement 620 | $sql = "UPDATE Settings SET" 621 | foreach ($setting in $settings.GetEnumerator()) 622 | { 623 | $sql = "$sql $($setting.Key) = '$($setting.Value)', " 624 | } 625 | 626 | # Chop off the trailing ", " 627 | $sql = $sql.Substring(0, $sql.Length - 2) 628 | 629 | # Add the where clause 630 | $sql = "$sql WHERE ID = $id AND Type = 'R'" 631 | 632 | # Execute the command 633 | Write-Verbose "About to execute command: $sql" 634 | $settingsCmd = New-Object System.Data.SqlClient.SqlCommand($sql, $mdtSQLConnection) 635 | $null = $settingsCmd.ExecuteScalar() 636 | 637 | Write-Host "Added settings for the specified role" 638 | 639 | # Write the updated record back to the pipeline 640 | Get-MDTRole -ID $id 641 | } 642 | } 643 | 644 | function Remove-MDTRole { 645 | 646 | [CmdletBinding()] 647 | PARAM 648 | ( 649 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 650 | ) 651 | 652 | Process 653 | { 654 | # Build the delete command 655 | $delCommand = "DELETE FROM RoleIdentity WHERE ID = $id" 656 | 657 | # Issue the delete command 658 | Write-Verbose "About to issue command: $delCommand" 659 | $cmd = New-Object System.Data.SqlClient.SqlCommand($delCommand, $mdtSQLConnection) 660 | $null = $cmd.ExecuteScalar() 661 | 662 | Write-Host "Removed the role with ID = $id." 663 | } 664 | } 665 | 666 | function Get-MDTRoleApplication { 667 | 668 | [CmdletBinding()] 669 | PARAM 670 | ( 671 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 672 | ) 673 | 674 | Process 675 | { 676 | Get-MDTArray $id 'R' 'Settings_Applications' 'Applications' 677 | } 678 | } 679 | 680 | function Clear-MDTRoleApplication { 681 | 682 | [CmdletBinding()] 683 | PARAM 684 | ( 685 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 686 | ) 687 | 688 | Process 689 | { 690 | Clear-MDTArray $id 'R' 'Settings_Applications' 691 | } 692 | } 693 | 694 | function Set-MDTRoleApplication { 695 | 696 | [CmdletBinding()] 697 | PARAM 698 | ( 699 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id, 700 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $applications 701 | ) 702 | 703 | Process 704 | { 705 | Set-MDTArray $id 'R' 'Settings_Applications' 'Applications' $applications 706 | } 707 | } 708 | 709 | function Get-MDTRolePackage { 710 | 711 | [CmdletBinding()] 712 | PARAM 713 | ( 714 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 715 | ) 716 | 717 | Process 718 | { 719 | Get-MDTArray $id 'R' 'Settings_Packages' 'Packages' 720 | } 721 | } 722 | 723 | function Clear-MDTRolePackage { 724 | 725 | [CmdletBinding()] 726 | PARAM 727 | ( 728 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 729 | ) 730 | 731 | Process 732 | { 733 | Clear-MDTArray $id 'R' 'Settings_Packages' 734 | } 735 | } 736 | 737 | function Set-MDTRolePackage { 738 | 739 | [CmdletBinding()] 740 | PARAM 741 | ( 742 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id, 743 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $packages 744 | ) 745 | 746 | Process 747 | { 748 | Set-MDTArray $id 'R' 'Settings_Packages' 'Packages' $packages 749 | } 750 | } 751 | 752 | function Get-MDTRoleRole { 753 | 754 | [CmdletBinding()] 755 | PARAM 756 | ( 757 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 758 | ) 759 | 760 | Process 761 | { 762 | Get-MDTArray $id 'R' 'Settings_Roles' 'Role' 763 | } 764 | } 765 | 766 | function Clear-MDTRoleRole { 767 | 768 | [CmdletBinding()] 769 | PARAM 770 | ( 771 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 772 | ) 773 | 774 | Process 775 | { 776 | Clear-MDTArray $id 'R' 'Settings_Roles' 777 | } 778 | } 779 | 780 | function Set-MDTRoleRole { 781 | 782 | [CmdletBinding()] 783 | PARAM 784 | ( 785 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id, 786 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $roles 787 | ) 788 | 789 | Process 790 | { 791 | Set-MDTArray $id 'R' 'Settings_Roles' 'Role' $roles 792 | } 793 | } 794 | 795 | function Get-MDTRoleAdministrator { 796 | 797 | [CmdletBinding()] 798 | PARAM 799 | ( 800 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 801 | ) 802 | 803 | Process 804 | { 805 | Get-MDTArray $id 'R' 'Settings_Administrators' 'Administrators' 806 | } 807 | } 808 | 809 | function Clear-MDTRoleAdministrator { 810 | 811 | [CmdletBinding()] 812 | PARAM 813 | ( 814 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 815 | ) 816 | 817 | Process 818 | { 819 | Clear-MDTArray $id 'R' 'Settings_Administrators' 820 | } 821 | } 822 | 823 | function Set-MDTRoleAdministrator { 824 | 825 | [CmdletBinding()] 826 | PARAM 827 | ( 828 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id, 829 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $administrators 830 | ) 831 | 832 | Process 833 | { 834 | Set-MDTArray $id 'R' 'Settings_Administrators' 'Administrators' $administrators 835 | } 836 | } 837 | 838 | # --------------------------------------------------------------------- 839 | # Location functions 840 | # --------------------------------------------------------------------- 841 | 842 | function New-MDTLocation { 843 | 844 | [CmdletBinding()] 845 | PARAM 846 | ( 847 | [Parameter(ValueFromPipelineByPropertyName=$true)] $name, 848 | [Parameter(ValueFromPipelineByPropertyName=$true)] $gateways, 849 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $settings 850 | ) 851 | 852 | Process 853 | { 854 | # Insert a new role row and get the identity result 855 | $sql = "INSERT INTO LocationIdentity (Location) VALUES ('$name') SELECT @@IDENTITY" 856 | Write-Verbose "About to execute command: $sql" 857 | $identityCmd = New-Object System.Data.SqlClient.SqlCommand($sql, $mdtSQLConnection) 858 | $identity = $identityCmd.ExecuteScalar() 859 | Write-Verbose "Added location identity record" 860 | 861 | # Set the gateways 862 | $null = Set-MDTLocation -id $identity -gateways $gateways 863 | 864 | # Insert the settings row, adding the values as specified in the hash table 865 | $settingsColumns = $settings.Keys -join "," 866 | $settingsValues = $settings.Values -join "','" 867 | $sql = "INSERT INTO Settings (Type, ID, $settingsColumns) VALUES ('L', $identity, '$settingsValues')" 868 | Write-Verbose "About to execute command: $sql" 869 | $settingsCmd = New-Object System.Data.SqlClient.SqlCommand($sql, $mdtSQLConnection) 870 | $null = $settingsCmd.ExecuteScalar() 871 | 872 | Write-Host "Added settings for the specified location" 873 | 874 | # Write the new record back to the pipeline 875 | Get-MDTLocation -ID $identity 876 | } 877 | } 878 | 879 | function Get-MDTLocation { 880 | 881 | [CmdletBinding()] 882 | PARAM 883 | ( 884 | [Parameter(ValueFromPipelineByPropertyName=$true)] $id = "", 885 | [Parameter(ValueFromPipelineByPropertyName=$true)] $name = "", 886 | [Parameter()][switch] $detail = $false 887 | ) 888 | 889 | Process 890 | { 891 | # Build a select statement based on what parameters were specified 892 | if ($id -eq "" -and $name -eq "") 893 | { 894 | if ($detail) 895 | { 896 | $sql = "SELECT * FROM LocationSettings" 897 | } 898 | else 899 | { 900 | $sql = "SELECT DISTINCT ID, Location FROM LocationSettings" 901 | } 902 | } 903 | elseif ($id -ne "") 904 | { 905 | if ($detail) 906 | { 907 | $sql = "SELECT * FROM LocationSettings WHERE ID = $id" 908 | } 909 | else 910 | { 911 | $sql = "SELECT DISTINCT ID, Location FROM LocationSettings WHERE ID = $id" 912 | } 913 | } 914 | else 915 | { 916 | if ($detail) 917 | { 918 | $sql = "SELECT * FROM LocationSettings WHERE Location = '$name'" 919 | } 920 | else 921 | { 922 | $sql = "SELECT DISTINCT ID, Location FROM LocationSettings WHERE Location = '$name'" 923 | } 924 | } 925 | 926 | # Execute the statement and return the results 927 | $selectAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($sql, $mdtSQLConnection) 928 | $selectDataset = New-Object System.Data.Dataset 929 | $null = $selectAdapter.Fill($selectDataset, "LocationSettings") 930 | $selectDataset.Tables[0].Rows 931 | } 932 | } 933 | 934 | function Set-MDTLocation { 935 | 936 | [CmdletBinding()] 937 | PARAM 938 | ( 939 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id, 940 | [Parameter(ValueFromPipelineByPropertyName=$true)] $gateways = $null, 941 | [Parameter()] $settings = $null 942 | ) 943 | 944 | Process 945 | { 946 | # If there are some new settings save them 947 | if ($settings -ne $null) 948 | { 949 | # Add each each hash table entry to the update statement 950 | $sql = "UPDATE Settings SET" 951 | foreach ($setting in $settings.GetEnumerator()) 952 | { 953 | $sql = "$sql $($setting.Key) = '$($setting.Value)', " 954 | } 955 | 956 | # Chop off the trailing ", " 957 | $sql = $sql.Substring(0, $sql.Length - 2) 958 | 959 | # Add the where clause 960 | $sql = "$sql WHERE ID = $id AND Type = 'L'" 961 | 962 | # Execute the command 963 | Write-Verbose "About to execute command: $sql" 964 | $settingsCmd = New-Object System.Data.SqlClient.SqlCommand($sql, $mdtSQLConnection) 965 | $null = $settingsCmd.ExecuteScalar() 966 | 967 | Write-Host "Added settings for the specified location" 968 | } 969 | 970 | # If there are some gateways save them 971 | if ($gateways -ne $null) 972 | { 973 | # Build the delete command to remove the existing gateways 974 | $delCommand = "DELETE FROM LocationIdentity_DefaultGateway WHERE ID = $id" 975 | 976 | # Issue the delete command 977 | Write-Verbose "About to issue command: $delCommand" 978 | $cmd = New-Object System.Data.SqlClient.SqlCommand($delCommand, $mdtSQLConnection) 979 | $null = $cmd.ExecuteScalar() 980 | 981 | # Now insert the specified values 982 | foreach ($gateway in $gateways) 983 | { 984 | # Insert the row 985 | $sql = "INSERT INTO LocationIdentity_DefaultGateway (ID, DefaultGateway) VALUES ($id, '$gateway')" 986 | Write-Verbose "About to execute command: $sql" 987 | $settingsCmd = New-Object System.Data.SqlClient.SqlCommand($sql, $mdtSQLConnection) 988 | $null = $settingsCmd.ExecuteScalar() 989 | 990 | } 991 | Write-Host "Set the default gateways for the location with ID = $id." 992 | } 993 | 994 | # Write the updated record back to the pipeline 995 | Get-MDTLocation -ID $id 996 | } 997 | } 998 | 999 | function Remove-MDTLocation { 1000 | 1001 | [CmdletBinding()] 1002 | PARAM 1003 | ( 1004 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 1005 | ) 1006 | 1007 | Process 1008 | { 1009 | # Build the delete command 1010 | $delCommand = "DELETE FROM LocationIdentity WHERE ID = $id" 1011 | 1012 | # Issue the delete command 1013 | Write-Verbose "About to issue command: $delCommand" 1014 | $cmd = New-Object System.Data.SqlClient.SqlCommand($delCommand, $mdtSQLConnection) 1015 | $null = $cmd.ExecuteScalar() 1016 | 1017 | Write-Host "Removed the location with ID = $id." 1018 | } 1019 | } 1020 | 1021 | function Get-MDTLocationApplication { 1022 | 1023 | [CmdletBinding()] 1024 | PARAM 1025 | ( 1026 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 1027 | ) 1028 | 1029 | Process 1030 | { 1031 | Get-MDTArray $id 'L' 'Settings_Applications' 'Applications' 1032 | } 1033 | } 1034 | 1035 | function Clear-MDTLocationApplication { 1036 | 1037 | [CmdletBinding()] 1038 | PARAM 1039 | ( 1040 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 1041 | ) 1042 | 1043 | Process 1044 | { 1045 | Clear-MDTArray $id 'L' 'Settings_Applications' 1046 | } 1047 | } 1048 | 1049 | function Set-MDTLocationApplication { 1050 | 1051 | [CmdletBinding()] 1052 | PARAM 1053 | ( 1054 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id, 1055 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $applications 1056 | ) 1057 | 1058 | Process 1059 | { 1060 | Set-MDTArray $id 'L' 'Settings_Applications' 'Applications' $applications 1061 | } 1062 | } 1063 | 1064 | function Get-MDTLocationPackage { 1065 | 1066 | [CmdletBinding()] 1067 | PARAM 1068 | ( 1069 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 1070 | ) 1071 | 1072 | Process 1073 | { 1074 | Get-MDTArray $id 'L' 'Settings_Packages' 'Packages' 1075 | } 1076 | } 1077 | 1078 | function Clear-MDTLocationPackage { 1079 | 1080 | [CmdletBinding()] 1081 | PARAM 1082 | ( 1083 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 1084 | ) 1085 | 1086 | Process 1087 | { 1088 | Clear-MDTArray $id 'L' 'Settings_Packages' 1089 | } 1090 | } 1091 | 1092 | function Set-MDTLocationPackage { 1093 | 1094 | [CmdletBinding()] 1095 | PARAM 1096 | ( 1097 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id, 1098 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $packages 1099 | ) 1100 | 1101 | Process 1102 | { 1103 | Set-MDTArray $id 'L' 'Settings_Packages' 'Packages' $packages 1104 | } 1105 | } 1106 | 1107 | function Get-MDTLocationRole { 1108 | 1109 | [CmdletBinding()] 1110 | PARAM 1111 | ( 1112 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 1113 | ) 1114 | 1115 | Process 1116 | { 1117 | Get-MDTArray $id 'L' 'Settings_Roles' 'Role' 1118 | } 1119 | } 1120 | 1121 | function Clear-MDTLocationRole { 1122 | 1123 | [CmdletBinding()] 1124 | PARAM 1125 | ( 1126 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 1127 | ) 1128 | 1129 | Process 1130 | { 1131 | Clear-MDTArray $id 'L' 'Settings_Roles' 1132 | } 1133 | } 1134 | 1135 | function Set-MDTLocationRole { 1136 | 1137 | [CmdletBinding()] 1138 | PARAM 1139 | ( 1140 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id, 1141 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $roles 1142 | ) 1143 | 1144 | Process 1145 | { 1146 | Set-MDTArray $id 'L' 'Settings_Roles' 'Role' $roles 1147 | } 1148 | } 1149 | 1150 | function Get-MDTLocationAdministrator { 1151 | 1152 | [CmdletBinding()] 1153 | PARAM 1154 | ( 1155 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 1156 | ) 1157 | 1158 | Process 1159 | { 1160 | Get-MDTArray $id 'L' 'Settings_Administrators' 'Administrators' 1161 | } 1162 | } 1163 | 1164 | function Clear-MDTLocationAdministrator { 1165 | 1166 | [CmdletBinding()] 1167 | PARAM 1168 | ( 1169 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 1170 | ) 1171 | 1172 | Process 1173 | { 1174 | Clear-MDTArray $id 'L' 'Settings_Administrators' 1175 | } 1176 | } 1177 | 1178 | function Set-MDTLocationAdministrator { 1179 | 1180 | [CmdletBinding()] 1181 | PARAM 1182 | ( 1183 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id, 1184 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $administrators 1185 | ) 1186 | 1187 | Process 1188 | { 1189 | Set-MDTArray $id 'L' 'Settings_Administrators' 'Administrators' $administrators 1190 | } 1191 | } 1192 | 1193 | # --------------------------------------------------------------------- 1194 | # Make Model functions 1195 | # --------------------------------------------------------------------- 1196 | 1197 | function New-MDTMakeModel { 1198 | 1199 | [CmdletBinding()] 1200 | PARAM 1201 | ( 1202 | [Parameter(ValueFromPipelineByPropertyName=$true)] $make, 1203 | [Parameter(ValueFromPipelineByPropertyName=$true)] $model, 1204 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $settings 1205 | ) 1206 | 1207 | Process 1208 | { 1209 | # Insert a new role row and get the identity result 1210 | $sql = "INSERT INTO MakeModelIdentity (Make, Model) VALUES ('$make', '$model') SELECT @@IDENTITY" 1211 | Write-Verbose "About to execute command: $sql" 1212 | $identityCmd = New-Object System.Data.SqlClient.SqlCommand($sql, $mdtSQLConnection) 1213 | $identity = $identityCmd.ExecuteScalar() 1214 | Write-Verbose "Added make model identity record" 1215 | 1216 | # Insert the settings row, adding the values as specified in the hash table 1217 | $settingsColumns = $settings.Keys -join "," 1218 | $settingsValues = $settings.Values -join "','" 1219 | $sql = "INSERT INTO Settings (Type, ID, $settingsColumns) VALUES ('M', $identity, '$settingsValues')" 1220 | Write-Verbose "About to execute command: $sql" 1221 | $settingsCmd = New-Object System.Data.SqlClient.SqlCommand($sql, $mdtSQLConnection) 1222 | $null = $settingsCmd.ExecuteScalar() 1223 | 1224 | Write-Host "Added settings for the specified make model" 1225 | 1226 | # Write the new record back to the pipeline 1227 | Get-MDTMakeModel -ID $identity 1228 | } 1229 | } 1230 | 1231 | function Get-MDTMakeModel { 1232 | 1233 | [CmdletBinding()] 1234 | PARAM 1235 | ( 1236 | [Parameter(ValueFromPipelineByPropertyName=$true)] $id = "", 1237 | [Parameter(ValueFromPipelineByPropertyName=$true)] $make = "", 1238 | [Parameter(ValueFromPipelineByPropertyName=$true)] $model = "" 1239 | ) 1240 | 1241 | Process 1242 | { 1243 | # Build a select statement based on what parameters were specified 1244 | if ($id -eq "" -and $make -eq "" -and $model -eq "") 1245 | { 1246 | $sql = "SELECT * FROM MakeModelSettings" 1247 | } 1248 | elseif ($id -ne "") 1249 | { 1250 | $sql = "SELECT * FROM MakeModelSettings WHERE ID = $id" 1251 | } 1252 | elseif ($make -ne "" -and $model -ne "") 1253 | { 1254 | $sql = "SELECT * FROM MakeModelSettings WHERE Make = '$make' AND Model = '$model'" 1255 | } 1256 | elseif ($make -ne "") 1257 | { 1258 | $sql = "SELECT * FROM MakeModelSettings WHERE Make = '$make'" 1259 | } 1260 | else 1261 | { 1262 | $sql = "SELECT * FROM MakeModelSettings WHERE Model = '$model'" 1263 | } 1264 | 1265 | # Execute the statement and return the results 1266 | $selectAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($sql, $mdtSQLConnection) 1267 | $selectDataset = New-Object System.Data.Dataset 1268 | $null = $selectAdapter.Fill($selectDataset, "MakeModelSettings") 1269 | $selectDataset.Tables[0].Rows 1270 | } 1271 | } 1272 | 1273 | function Set-MDTMakeModel { 1274 | 1275 | [CmdletBinding()] 1276 | PARAM 1277 | ( 1278 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id, 1279 | [Parameter(Mandatory=$true)] $settings 1280 | ) 1281 | 1282 | Process 1283 | { 1284 | # Add each each hash table entry to the update statement 1285 | $sql = "UPDATE Settings SET" 1286 | foreach ($setting in $settings.GetEnumerator()) 1287 | { 1288 | $sql = "$sql $($setting.Key) = '$($setting.Value)', " 1289 | } 1290 | 1291 | # Chop off the trailing ", " 1292 | $sql = $sql.Substring(0, $sql.Length - 2) 1293 | 1294 | # Add the where clause 1295 | $sql = "$sql WHERE ID = $id AND Type = 'M'" 1296 | 1297 | # Execute the command 1298 | Write-Verbose "About to execute command: $sql" 1299 | $settingsCmd = New-Object System.Data.SqlClient.SqlCommand($sql, $mdtSQLConnection) 1300 | $null = $settingsCmd.ExecuteScalar() 1301 | 1302 | Write-Host "Added settings for the specified make model" 1303 | 1304 | # Write the updated record back to the pipeline 1305 | Get-MDTMakeModel -ID $id 1306 | } 1307 | } 1308 | 1309 | function Remove-MDTMakeModel { 1310 | 1311 | [CmdletBinding()] 1312 | PARAM 1313 | ( 1314 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 1315 | ) 1316 | 1317 | Process 1318 | { 1319 | # Build the delete command 1320 | $delCommand = "DELETE FROM MakeModelIdentity WHERE ID = $id" 1321 | 1322 | # Issue the delete command 1323 | Write-Verbose "About to issue command: $delCommand" 1324 | $cmd = New-Object System.Data.SqlClient.SqlCommand($delCommand, $mdtSQLConnection) 1325 | $null = $cmd.ExecuteScalar() 1326 | 1327 | Write-Host "Removed the make model with ID = $id." 1328 | } 1329 | } 1330 | 1331 | function Get-MDTMakeModelApplication { 1332 | 1333 | [CmdletBinding()] 1334 | PARAM 1335 | ( 1336 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 1337 | ) 1338 | 1339 | Process 1340 | { 1341 | Get-MDTArray $id 'M' 'Settings_Applications' 'Applications' 1342 | } 1343 | } 1344 | 1345 | function Clear-MDTMakeModelApplication { 1346 | 1347 | [CmdletBinding()] 1348 | PARAM 1349 | ( 1350 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 1351 | ) 1352 | 1353 | Process 1354 | { 1355 | Clear-MDTArray $id 'M' 'Settings_Applications' 1356 | } 1357 | } 1358 | 1359 | function Set-MDTMakeModelApplication { 1360 | 1361 | [CmdletBinding()] 1362 | PARAM 1363 | ( 1364 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id, 1365 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $applications 1366 | ) 1367 | 1368 | Process 1369 | { 1370 | Set-MDTArray $id 'M' 'Settings_Applications' 'Applications' $applications 1371 | } 1372 | } 1373 | 1374 | function Get-MDTMakeModelPackage { 1375 | 1376 | [CmdletBinding()] 1377 | PARAM 1378 | ( 1379 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 1380 | ) 1381 | 1382 | Process 1383 | { 1384 | Get-MDTArray $id 'M' 'Settings_Packages' 'Packages' 1385 | } 1386 | } 1387 | 1388 | function Clear-MDTMakeModelPackage { 1389 | 1390 | [CmdletBinding()] 1391 | PARAM 1392 | ( 1393 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 1394 | ) 1395 | 1396 | Process 1397 | { 1398 | Clear-MDTArray $id 'M' 'Settings_Packages' 1399 | } 1400 | } 1401 | 1402 | function Set-MDTMakeModelPackage { 1403 | 1404 | [CmdletBinding()] 1405 | PARAM 1406 | ( 1407 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id, 1408 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $packages 1409 | ) 1410 | 1411 | Process 1412 | { 1413 | Set-MDTArray $id 'M' 'Settings_Packages' 'Packages' $packages 1414 | } 1415 | } 1416 | 1417 | function Get-MDTMakeModelRole { 1418 | 1419 | [CmdletBinding()] 1420 | PARAM 1421 | ( 1422 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 1423 | ) 1424 | 1425 | Process 1426 | { 1427 | Get-MDTArray $id 'M' 'Settings_Roles' 'Role' 1428 | } 1429 | } 1430 | 1431 | function Clear-MDTMakeModelRole { 1432 | 1433 | [CmdletBinding()] 1434 | PARAM 1435 | ( 1436 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 1437 | ) 1438 | 1439 | Process 1440 | { 1441 | Clear-MDTArray $id 'M' 'Settings_Roles' 1442 | } 1443 | } 1444 | 1445 | function Set-MDTMakeModelRole { 1446 | 1447 | [CmdletBinding()] 1448 | PARAM 1449 | ( 1450 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id, 1451 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $roles 1452 | ) 1453 | 1454 | Process 1455 | { 1456 | Set-MDTArray $id 'M' 'Settings_Roles' 'Role' $roles 1457 | } 1458 | } 1459 | 1460 | function Get-MDTMakeModelAdministrator { 1461 | 1462 | [CmdletBinding()] 1463 | PARAM 1464 | ( 1465 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 1466 | ) 1467 | 1468 | Process 1469 | { 1470 | Get-MDTArray $id 'M' 'Settings_Administrators' 'Administrators' 1471 | } 1472 | } 1473 | 1474 | function Clear-MDTMakeModelAdministrator { 1475 | 1476 | [CmdletBinding()] 1477 | PARAM 1478 | ( 1479 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id 1480 | ) 1481 | 1482 | Process 1483 | { 1484 | Clear-MDTArray $id 'M' 'Settings_Administrators' 1485 | } 1486 | } 1487 | 1488 | function Set-MDTMakeModelAdministrator { 1489 | 1490 | [CmdletBinding()] 1491 | PARAM 1492 | ( 1493 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $id, 1494 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $administrators 1495 | ) 1496 | 1497 | Process 1498 | { 1499 | Set-MDTArray $id 'M' 'Settings_Administrators' 'Administrators' $administrators 1500 | } 1501 | } 1502 | 1503 | 1504 | # --------------------------------------------------------------------- 1505 | # Package mapping functions 1506 | # --------------------------------------------------------------------- 1507 | 1508 | function New-MDTPackageMapping { 1509 | 1510 | [CmdletBinding()] 1511 | PARAM 1512 | ( 1513 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $ARPName, 1514 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $package 1515 | ) 1516 | 1517 | Process 1518 | { 1519 | # Insert a new row 1520 | $sql = "INSERT INTO PackageMapping (ARPName, Packages) VALUES ('$ARPName','$package')" 1521 | Write-Verbose "About to execute command: $sql" 1522 | $identityCmd = New-Object System.Data.SqlClient.SqlCommand($sql, $mdtSQLConnection) 1523 | $null = $identityCmd.ExecuteScalar() 1524 | Write-Verbose "Added package mapping record for $ARPName" 1525 | 1526 | # Write the new record back to the pipeline 1527 | Get-MDTPackageMapping -ARPName $ARPName 1528 | } 1529 | } 1530 | 1531 | function Get-MDTPackageMapping { 1532 | 1533 | [CmdletBinding()] 1534 | PARAM 1535 | ( 1536 | [Parameter(ValueFromPipelineByPropertyName=$true)] $ARPName = "", 1537 | [Parameter(ValueFromPipelineByPropertyName=$true)] $package = "" 1538 | ) 1539 | 1540 | Process 1541 | { 1542 | # Build a select statement based on what parameters were specified 1543 | if ($ARPName -eq "" -and $package -eq "") 1544 | { 1545 | $sql = "SELECT * FROM PackageMapping" 1546 | } 1547 | elseif ($ARPName -ne "" -and $package -ne "") 1548 | { 1549 | $sql = "SELECT * FROM PackageMapping WHERE ARPName = '$ARPName' AND Packages = '$package'" 1550 | } 1551 | elseif ($ARPName -ne "") 1552 | { 1553 | $sql = "SELECT * FROM PackageMapping WHERE ARPName = '$ARPName'" 1554 | } 1555 | else 1556 | { 1557 | $sql = "SELECT * FROM PackageMapping WHERE Packages = '$package'" 1558 | } 1559 | 1560 | # Execute the statement and return the results 1561 | $selectAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($sql, $mdtSQLConnection) 1562 | $selectDataset = New-Object System.Data.Dataset 1563 | $null = $selectAdapter.Fill($selectDataset, "PackageMapping") 1564 | $selectDataset.Tables[0].Rows 1565 | } 1566 | } 1567 | 1568 | function Set-MDTPackageMapping { 1569 | 1570 | [CmdletBinding()] 1571 | PARAM 1572 | ( 1573 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $ARPName, 1574 | [Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)] $package = $null 1575 | ) 1576 | 1577 | Process 1578 | { 1579 | # Update the row 1580 | $sql = "UPDATE PackageMapping SET Packages = '$package' WHERE ARPName = '$ARPName'" 1581 | Write-Verbose "About to execute command: $sql" 1582 | $settingsCmd = New-Object System.Data.SqlClient.SqlCommand($sql, $mdtSQLConnection) 1583 | $null = $settingsCmd.ExecuteScalar() 1584 | Write-Host "Updated the package mapping record for $ARPName to install package $package." 1585 | 1586 | # Write the updated record back to the pipeline 1587 | Get-MDTPackageMapping -ARPName $ARPName 1588 | } 1589 | } 1590 | 1591 | function Remove-MDTPackageMapping { 1592 | 1593 | [CmdletBinding()] 1594 | PARAM 1595 | ( 1596 | [Parameter(ValueFromPipelineByPropertyName=$true)] $ARPName = "", 1597 | [Parameter(ValueFromPipelineByPropertyName=$true)] $package = "" 1598 | ) 1599 | 1600 | Process 1601 | { 1602 | # Build a delete statement based on what parameters were specified 1603 | if ($ARPName -eq "" -and $package -eq "") 1604 | { 1605 | # Dangerous, delete them all 1606 | $sql = "DELETE FROM PackageMapping" 1607 | } 1608 | elseif ($ARPName -ne "" -and $package -ne "") 1609 | { 1610 | $sql = "DELETE FROM PackageMapping WHERE ARPName = '$ARPName' AND Packages = '$package'" 1611 | } 1612 | elseif ($ARPName -ne "") 1613 | { 1614 | $sql = "DELETE FROM PackageMapping WHERE ARPName = '$ARPName'" 1615 | } 1616 | else 1617 | { 1618 | $sql = "DELETE FROM PackageMapping WHERE Packages = '$package'" 1619 | } 1620 | 1621 | # Execute the delete command 1622 | Write-Verbose "About to execute command: $sql" 1623 | $settingsCmd = New-Object System.Data.SqlClient.SqlCommand($sql, $mdtSQLConnection) 1624 | $null = $settingsCmd.ExecuteScalar() 1625 | Write-Host "Removed package mapping records matching the specified parameters." 1626 | } 1627 | } 1628 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | MDT-Database-script 2 | =================== 3 | This is an example of a Powershell script that builds on the MDT Powershell module made by Michael Neihaus. Module was obtained from [Michael's blog post](https://blogs.technet.microsoft.com/mniehaus/2009/05/14/manipulating-the-microsoft-deployment-toolkit-database-using-powershell/) and is included in this repo for convenience. 4 | 5 | This scirpt is designed to read in a csv file containing headers and the computer information. A template csv file is included in this repo. 6 | 7 | This example loads Mac address, Computer name, Admin Password, and Role. It could easily be adjusted for any set of fields. 8 | 9 | Obviously the line of code that sets up the connection to the database will need to be modified to match the environment being used. 10 | 11 | The DeleteComputers.ps1 script needs some clean up as it was created by simply deleting some parts out of the original LoadComputers.ps1 script. -------------------------------------------------------------------------------- /computers.csv: -------------------------------------------------------------------------------- 1 | mac,name,password,role 2 | 00:00:00:00:00:00,Computer1,password1,Role 1 3 | 00:00:00:00:00:00,Computer2,password2,Role 2 4 | --------------------------------------------------------------------------------