Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

PART 2 : POWERSHELL - COPY DSC MODULE REMOTELY USING LOCAL WEB REPOSITORY

August 7, 2016 10:22PM

PART 1 : POWERSHELL - CREATE LOCAL IIS WEB REPOSITORY FOR DSC MODULE 
PART 2 : POWERSHELL - COPY DSC MODULE REMOTELY USING LOCAL WEB REPOSITORY

After building my centralized IIS web repository for DSC Module, it can be used to download modules on remote computer easily and DSC can uncompress zipped module in the required folder. So in first place why do I require this Module repository? Because I don't have Internet on remote computer and other scenario is computers are not joined into domain, So copying modules files from local web server is my best choice here. I will go through breakup of script.

This DSC Block makes sure there is a Temp folder present on c: drive.
       File CreateFolder {
            Ensure = 'Present'
            Type = 'Directory'
            DestinationPath = 'C:\Temp'
        }

This will download DSC module zip from internal web server to C:\temp location, It will first verify if C:\temp folder exist, Earlier block is a dependancy for this box. 
        Script Download-xActiveDirectory {
            GetScript = {
                @{Result = Test-Path 'C:\Temp\xActiveDirectory.zip'}
            }
            SetScript = {
                $xActiveDirectoryUrl = 'http://192.168.33.11/modules/xActiveDirectory.zip'
                Invoke-WebRequest -Uri $xActiveDirectoryUrl -OutFile 'C:\Temp\xActiveDirectory.zip'
                Unblock-File -Path 'C:\Temp\xActiveDirectory.zip'
            }
            TestScript = {
                Test-Path 'C:\Temp\xActiveDirectory.zip'
            }
            DependsOn = '[File]CreateFolder'
        }

This block will uncompress zip file to "C:\Program Files\WindowsPowerShell\Modules", Again this block is depends on earlier script block, If zip file present on the C:\temp only this will be executed.
        Archive Uncompress-xActiveDirectory {
            Ensure = 'Present'
            Path = 'C:\Temp\xActiveDirectory.zip'
            Destination = 'C:\Program Files\WindowsPowerShell\Modules\xActiveDirectory'
            DependsOn = '[Script]Download-xActiveDirectory'
        }

  $ComputerName = Read-Host -Prompt 'Remote computer Hostname or IP'  
  $MOFfiles = "c:\temp\$computername"   
    
  #Create connection to remote computer   
  $RemoteAdministratorCred = Get-Credential -UserName Administrator -Message "$ComputerName Administrator password"   
  $CimSession = New-CimSession -ComputerName $ComputerName -Credential $RemoteAdministratorCred -Name $ComputerName   
   
  Configuration CopyDSCModuleRemotely {   
   Node $ComputerName {   
     File CreateFolder {  
       Ensure = 'Present'  
       Type = 'Directory'  
       DestinationPath = 'C:\Temp'  
     }  
   
     Script Download-xActiveDirectory {  
       GetScript = {  
         @{Result = Test-Path 'C:\Temp\xActiveDirectory.zip'}  
       }  
       SetScript = {  
         $xActiveDirectoryUrl = 'http://192.168.33.11/modules/xActiveDirectory.zip'  
         Invoke-WebRequest -Uri $xActiveDirectoryUrl -OutFile 'C:\Temp\xActiveDirectory.zip'  
         Unblock-File -Path 'C:\Temp\xActiveDirectory.zip'  
           
       }  
       TestScript = {  
         Test-Path 'C:\Temp\xActiveDirectory.zip'  
       }  
       DependsOn = '[File]CreateFolder'  
     }  
   
     Archive Uncompress-xActiveDirectory {  
       Ensure = 'Present'  
       Path = 'C:\Temp\xActiveDirectory.zip'  
       Destination = 'C:\Program Files\WindowsPowerShell\Modules\xActiveDirectory'  
       DependsOn = '[Script]Download-xActiveDirectory'  
     }  
   }  
 }  
   
  #Generate mof files   
  CopyDSCModuleRemotely -OutputPath $MOFfiles #-ConfigurationData $ConfigurationData   
    
  #Start Deployment remotely   
  Start-DscConfiguration -Path $MOFfiles -Verbose -CimSession $CimSession -Wait -Force   
   
  #Get deployied settings  
  #Get-DscConfiguration -CimSession $CimSession  

This code can be used in any Powershell DSC Push implementation, Code can be downloaded here download powershell script free 

Go Back

Comment