write-host "Cloud Module file 0.1" Function Set-Credentials{ if(-NOT $Global:Credentials){ $Global:Credentials = Get-Credential } } Function Get-WebClient{ Param ( [Parameter()]$Credentials ) #invoke our web client $WebClient = New-Object System.Net.WebClient #Disable SSL cert check [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} Set-Credentials #Set our credentials $WebClient.Credentials = $Global:Credentials #add to global var $Global:WebClient = $WebClient } Function Get-MediaServerURL{ Param ( [Parameter()]$DC ) if (-NOT $DC){ #Find our DC from the registry $RegDC = (Get-ItemProperty "HKLM:\SOFTWARE\Rackspace\" -Name Datacenter -ErrorAction SilentlyContinue).Datacenter IF (-NOT $RegDC){Write-Host "ERROR: Could not get DC from registry" -ForegroundColor red ;break} $DC = $RegDC.substring(0,3) } if($DC -eq "LON"){ $URL = "https://92.52.127.142" }else{ $URL = "https://media.$DC.rackspace.com" } return $url } Function Run-MediaServerScript{ Param ( [Parameter(Mandatory=$true)]$URLPath, #URLpath of the module you want to import [Parameter()]$Credentials, #Intensive credentials if you want to pass them through [Parameter()]$ScriptParams, #params you want to run the script with [Parameter()]$DC #params you want to run the script with ) $DownloadedModule = Download-MediaServerString -URLPath $URLPath -DC $DC If ($ScriptParams){ #if we provide a splat, pass them into invoke expression . Invoke-Expression "$DownloadedModule $ScriptParams" }else{ #run the downloaded string as a script . Invoke-Expression $DownloadedModule } } Function Download-MediaServerString{ Param ( [Parameter(Mandatory=$true)]$URLPath, [Parameter()]$Credentials, [Parameter()]$DC ) Get-WebClient #Build our URL $MediaServerURL = Get-MediaServerURL -DC $DC $URL = "$MediaServerURL/$URLPath" Write-Host "Downloading $URL..." -ForegroundColor Green #download the script as a string straight into a variable $DownloadedModule = $Global:WebClient.DownloadString($URL) Return $DownloadedModule } Function Download-MediaServerFile{ <# .Synopsis Download file to the media server .DESCRIPTION Function will use the powershell webclient to download files to the media share .EXAMPLE Download-MediaServerFile -DC LON -URLPath media/scriptrepository/powershell/shared/example.ps1 .OUTPUTS None #> Param ( [Parameter(Mandatory=$true)]$URLPath, [Parameter()]$DownloadPath, [Parameter()]$Credentials, [Parameter()]$DC ) #Load the webclient Get-WebClient $MediaServerURL = Get-MediaServerURL -DC $DC $url = "$MediaServerURL/$URLPath" #Work out the file name by splitting the urlpath and getting the last entry $FileName = $URLPath.SubString($URLPath.LastIndexOf("/") + 1) if(-NOT $DownloadPath){ $DownloadPath = "$env:tmp\$FileName" } #delete previous downloads Remove-Item $DownloadPath -Force -ErrorAction SilentlyContinue | Out-Null $Error.Clear() $Global:WebClient.DownloadFile($URL,$DownloadPath) #check no errors and write to screen if there is If($Error.Count -gt 0){Write-Host "Unable to download URL $URL - probably wrong credentials" -ForegroundColor Red} } Function Import-MediaServerModule{ Param ( [Parameter(Mandatory=$true)]$Modules, [Parameter()]$DC, [Parameter()]$Credentials ) foreach($modulename in $Modules){ Write-Host "importing.... $modulename" -ForegroundColor Yellow $DownloadedModule = Download-MediaServerFile -URLPath $modulename -DC $DC $FileName = $modulename.SubString($modulename.LastIndexOf("/") + 1) write-host "Import-Module $ENV:tmp\$FileName" Import-Module "$ENV:tmp\$FileName" -Force -DisableNameChecking -Scope Global Remove-Item "$ENV:tmp\$FileName" -Force -ErrorAction SilentlyContinue | Out-Null } }