-
-
Save FlorianAlikoff/7743ac28ce3d543b60ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
########################################################## | |
# # | |
# Script PowerShell v4.0 # | |
# Library Pull Configuration # | |
# # | |
# Name : Library Pull Configuration.ps1 # | |
# Author : Florian Alikoff # | |
# Date : 10/02/2014 # | |
# # | |
########################################################## | |
################################################################## | |
# Functions # | |
################################################################## | |
Function Get-ComputerGuid { | |
<# | |
.SYNOPSIS | |
Retrieve GUID for a specific AD computer. | |
.DESCRIPTION | |
Retrieve GUID for a specific AD computer using a computer name (samaccountname). | |
.PARAMETER ComputerName | |
Computer name | |
.EXAMPLE | |
Get-ComputerGuid -ComputerName "Server01" | |
Description | |
----------- | |
This script retrieve the GUID attribute of a computer in Active Directory using his samaccountname. | |
#> | |
param( | |
[Parameter(Mandatory=$true)] | |
[string]$ComputerName | |
) | |
Process { | |
([guid]([adsisearcher]"(samaccountname=$ComputerName`$)").FindOne().Properties["objectguid"][0]).Guid | |
} | |
} | |
Function Get-ClientComputer { | |
<# | |
.SYNOPSIS | |
Retrieve computer with adsisearcher (you don't need to have Active Directory Powershell Module). | |
.DESCRIPTION | |
Retrieve computer with adsisearcher (you don't need to have Active Directory Powershell Module). | |
You can filter by a specific organizational unit or by a group. | |
.PARAMETER Filter | |
Organizational unit or AD group name | |
.PARAMETER SearchIn | |
Filter type : OU or Group | |
.EXAMPLE | |
Get-ClientComputer -Filter "Pull Clients" -SearchIn "Group" | |
Description | |
----------- | |
Retrieve computer with adsisearcher (you don't need to have Active Directory Powershell Module). | |
You can filter by a specific organizational unit or by a group. if there is multiple OU with the same name, | |
it retrieves all computers in each OU. Return computer name. | |
#> | |
param( | |
[Parameter(Mandatory=$true)] | |
[string]$Filter, | |
[Parameter(Mandatory=$true)] | |
[ValidateSet("OU", "Group")] | |
[string]$SearchIn | |
) | |
Process { | |
if($SearchIn -eq "OU"){ | |
[Array]$Result = @() | |
try{ | |
$OUs = ([adsisearcher]"(&(ObjectCategory=OrganizationalUnit)(Name=$Filter))").FindAll() | |
ForEach($OU in $OUs.Path){ | |
$Root = [ADSI]"$OU" | |
$Search = [adsisearcher]$Root | |
$Search.Filter = "ObjectCategory=Computer" | |
$Result += $Search.FindAll() | |
} | |
}catch{ | |
Write-Host "Invalid organizational unit." | |
} | |
}else{ | |
$Group = ([adsisearcher]"(&(ObjectClass=Group)(Name=$Filter))").FindOne() | |
$Result = ($Group.Properties["member"] | select @{Name="ADObject";Expression={([adsisearcher]"(distinguishedname=$_)").FindOne()}} | Where-Object {$_.ADObject.Properties["ObjectClass"] -contains "computer"}).ADObject | |
} | |
return $Result.Properties.name | |
} | |
} | |
function New-FileChecksum { | |
<# | |
.SYNOPSIS | |
Get hash from a file, then write this hash into a file with the same name (add the extension .checksum). | |
.DESCRIPTION | |
Get hash from a file, then write this hash into a file with the same name (add the extension .checksum). | |
.PARAMETER FilePath | |
Organizational unit or AD group name | |
.EXAMPLE | |
New-FileChecksum -FilePath "C:\Data\Test.mof" | |
Description | |
----------- | |
Get hash from "C:\Data\Test.mof", then write this hash into a file with the same name (add the extension .checksum), eg : "C:\Data\Test.mof.checksum". | |
#> | |
param( | |
[Parameter(Mandatory=$true)] | |
[String[]] $FilePath | |
) | |
Process { | |
$HashMOF = (Get-FileHash $FilePath).hash | |
[System.IO.File]::WriteAllText("$FilePath.checksum",$HashMOF) | |
} | |
} | |
Configuration LocalConfigurationManager { | |
<# | |
.SYNOPSIS | |
Generate pull configuration for LocalConfigurationManager ressource for specific computers. | |
.DESCRIPTION | |
Generate pull configuration for LocalConfigurationManager ressource for specific computers. | |
.PARAMETER Computers | |
Computer name list | |
.PARAMETER PullServer | |
Name of the pull server | |
.PARAMETER PullShare | |
Name of the share on pull server containing MOF file | |
.PARAMETER ConfigurationFolder | |
Name of the sub folder on Pull share containing MOF file | |
.PARAMETER ConfigurationModeFrequencyMins | |
Frequency between two configuration conformity check (in minutes) | |
.PARAMETER RefreshFrequencyMins | |
Frequency between two refresh of configuration file in minutes (check on pull server) | |
.EXAMPLE | |
LocalConfigurationManager -OutputPath "C:\Configuration" -Computers @(Server01,Server02) -PullServer "ServerPULL01" -PullShare "DSC" ` | |
-ConfigurationFolder "LCM" -RefreshFrequencyMins 30 -ConfigurationModeFrequencyMins 45 | |
Description | |
----------- | |
Generate pull configuration for LocalConfigurationManager ressource for specific computers. Local configuration manager will be configured to retrieve | |
configuration on \\ServerPULL01\DSC\Configuration\ every 30 minutes and check configuration every 45 minutes. | |
#> | |
param( | |
[Parameter(Mandatory=$true)] | |
[String[]] $Computers, | |
[Parameter(Mandatory=$true)] | |
[String] $PullServer, | |
[Parameter(Mandatory=$true)] | |
[String] $PullShare, | |
[Parameter(Mandatory=$true)] | |
[String] $ConfigurationFolder, | |
[Parameter(Mandatory=$true)] | |
[int] $ConfigurationModeFrequencyMins, | |
[Parameter(Mandatory=$true)] | |
[int] $RefreshFrequencyMins | |
) | |
ForEach($Computer in $Computers){ | |
Node $Computer { | |
LocalConfigurationManager { | |
ConfigurationID = (Get-ComputerGuid $Computer) | |
ConfigurationMode="ApplyandAutoCorrect" | |
ConfigurationModeFrequencyMins = $ConfigurationModeFrequencyMins | |
RebootNodeIfNeeded = $True | |
RefreshFrequencyMins = $RefreshFrequencyMins | |
RefreshMode = "PULL" | |
AllowModuleOverwrite = $True | |
DownloadManagerName = "DSCFileDownloadManager" | |
DownloadManagerCustomData = (@{SourcePath ="\\$PullServer\$PullShare\$ConfigurationFolder"}) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment