A function to import clients from txt-file in to collection. If the client don´t exist or already are a member of collection the function creates a logfile.
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 |
#requires -Version 2 -Modules ConfigurationManager <# .Synopsis Import Clients into Collections .DESCRIPTION Import clients to collections from txt-fil .EXAMPLE Import-ClientsTOCollection -collectionname 'O365 test' -txtfile \\sodra.com\cm2012\source\o365\txt\textfile.txt -site sod: .EXAMPLE #> function Import-ClientsTOCollection { [CmdletBinding()] [Alias()] [OutputType([int])] Param ( # Param1 help description [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 1)] $Collectionname, # Param2 help description [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 2)] $txtfile, # Param3 help description [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 3)] $site ) Begin { } Process { Set-Location -Path \\sodra.com\cm2012\Source\O365\txt $importfile = Get-Content $txtfile Set-Location -Path $site foreach($computer in $importfile) { try { Add-CMDeviceCollectionDirectMembershipRule -CollectionName $Collectionname -ResourceId $(Get-CMDevice -Name $computer).ResourceID } catch { "Invalid client or direct membership rule may already exist: $computer" | Out-File -FilePath "c:\$Collectionname`_invalid.log" -Append } Set-Location -Path $site } Set-Location $myPShome } End { $checkfile = (Test-Path -Path "c:\$Collectionname`_invalid.log") if ($checkfile -eq $true) { Invoke-Item -Path "c:\$Collectionname`_invalid.log" Copy-Item -Path "c:\$Collectionname`_invalid.log" -Destination \\sodra.com\cm2012\source\O365\logs -Force } if ($checkfile -eq $false) { Write-Host -Object 'Allt gick bra, inga fel.' -ForegroundColor white -BackgroundColor Green } } } |