Public/Initialize-CMAgent.ps1

Function Initialize-CMAgent {
  <#
      .Synopsis
      Configures the Agent on the Client with all needed Dependencies
 
      .Description
      Configures the Agent on the Client with all needed Dependencies
 
  #>


  [CmdletBinding()]
  param(
    [Parameter(Mandatory=$true)]
    [Alias('p')]
    [string]$Path,
    [Parameter(Mandatory=$true)]
    [Alias('g')]
    [string]$Git,
    [Parameter(Mandatory=$true)]
    [Alias('ad')]
    [string]$ActiveDirectory,
    [Parameter(Mandatory=$true)]
    [Alias('f')]
    [string]$Filter,
    [Parameter(Mandatory=$true)]
    [Alias('b')]
    [string]$Baseline
    
    
  )
  begin
  {
    $PreReq = Test-Prerequisites
    $RegPath = 'HKLM:\SOFTWARE\PSWCMA'
  }
  process
  {
    #Write Configuration Cache
    New-Item -Path $RegPath -Force
    New-ItemProperty -Path $RegPath -Name 'FilePath' -Value $Path -PropertyType String -Force
    New-ItemProperty -Path $RegPath -Name 'Git' -Value $Git -PropertyType String -Force
    New-ItemProperty -Path $RegPath -Name 'ActiveDirectory' -Value $ActiveDirectory -PropertyType String -Force
    New-ItemProperty -Path $RegPath -Name 'AdFilter' -Value $Filter -PropertyType String -Force
    New-ItemProperty -Path $RegPath -Name 'BaseLineConfig' -Value $Baseline -PropertyType String -Force
    
    #Install Pre-Reqs
    if(!$PreReq.Win10) {
      Write-Warning 'This is not a Windows 10 Device. Going to End'
    }
    if(!$PreReq.Git) {
      Install-Git
    }
    if(!$PreReq.WinRM) {
      Set-WSManQuickConfig -Force
    }
    if(!$PreReq.CFW) {
      Install-Module -Name 'cFirewall' -Force -Confirm:$false
    }
    if(!$PreReq.XPSDSC) {
      Install-Module -Name 'xPSDesiredStateConfiguration' -Force -Confirm:$false
    }
    
    if(!(Test-Prerequisites).All) {
      Write-Error 'There was an error installing the Prequisites'
    }
    
    #Configure Scheduler
    $Random = Get-Random -Maximum 15
    $SchedulerAction = New-ScheduledTaskAction -Execute 'powershell' -Argument '-NoProfile -WindowStyle Hidden -command "& {Import-Module PSWCMA; Install-Configurations}"'
    $SchedulerTrigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 15) -RepetitionDuration (New-TimeSpan -Days (365 * 20)) -RandomDelay (New-TimeSpan -Minutes $Random)
    Register-ScheduledTask -User System -TaskName 'Configuration Management Agent' -Action $SchedulerAction -Trigger $SchedulerTrigger -Force
    
  }
  end
  {
    
  }

}