write-verbose "Media server functions 1.0" ####Media Server functions 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-verbose "importing.... $modulename" $DownloadedModule = Download-MediaServerFile -URLPath $modulename -DC $DC $FileName = $modulename.SubString($modulename.LastIndexOf("/") + 1) #write-verbose "Import-Module $ENV:tmp\$FileName" Import-Module "$ENV:tmp\$FileName" -Force -DisableNameChecking -Scope Global Remove-Item "$ENV:tmp\$FileName" -Force -ErrorAction SilentlyContinue | Out-Null } } Function Upload-MediaServerFile{ <# .Synopsis Upload file to the media server .DESCRIPTION Function will use the powershell webclient to upload files to the media share .EXAMPLE Upload-MediaServerFile -DC LON -LocalPath C:\rs-pkgs\example.ps1 -URLPath media/scriptrepository/powershell/shared/example.ps1 .OUTPUTS None #> Param ( [Parameter()]$DC, [Parameter(Mandatory=$true)]$LocalPath, [Parameter(Mandatory=$true)]$URLPath, [Parameter()]$Credentials #intensive creds if you want to pass them through ) #Load the webclient Get-WebClient $MediaServerURL = Get-MediaServerURL -DC $DC $url = "$MediaServerURL/$URLPath" write-verbose "Uploading $url..." $Error.Clear() $Global:WebClient.UploadFile($url, "PUT", $LocalPath) #check no errors and write to screen if there is If($Error.Count -gt 0){Write-Host "Unable to upload file LocalPath $LocalPath - probably wrong credentials" -ForegroundColor Red} } Function Get-UserVariableAge{ Param( [Parameter(Mandatory=$true)]$Variablename ) If ([Environment]::GetEnvironmentVariable("$Variablename`_TimeSet","User")) { [datetime]$TimeSet = [Environment]::GetEnvironmentVariable("$Variablename`_TimeSet","User") $TimeSpan = New-TimeSpan -Start $TimeSet -End $(Get-Date) }else{ $TimeSpan = $false } Return $TimeSpan } Function Delete-UserVariable{ Param( [Parameter(Mandatory=$true)]$Variablename ) [Environment]::SetEnvironmentVariable("$Variablename",$null, "User") [Environment]::SetEnvironmentVariable("$Variablename`_Timeset",$null, "User") } Function Get-IntensiveRefreshTime{ Return 230 } Function Get-UserVariable{ Param( #Set the env var name by default to use int but still make this function generic [Parameter(Mandatory=$true)]$Variablename, [Parameter(Mandatory=$true)]$Prompt, [Parameter()][switch]$Username, [Parameter()][switch]$Password, [Parameter()]$RefreshTime #delete the variable after X minutes ) If($Variablename -eq "intpassword"){ $RefreshTime = Get-IntensiveRefreshTime } #delete the variable if its older then the refresh time $TimeSpan = Get-UserVariableAge -Variablename $Variablename if($TimeSpan.Minutes -gt $RefreshTime){ Delete-UserVariable -Variablename $Variablename } If ([Environment]::GetEnvironmentVariable("$Variablename","User")){ $var = [Environment]::GetEnvironmentVariable("$Variablename","User") }ELSE{ #Prompt for user name $var = Read-Host $Prompt #if($password){ # $var = $var | ConvertTo-SecureString #} if($Username){ #if the user entered the domain, split it on the \ so we just get the username If($var -like "*\*"){$var = $var.Split("\")[1]} } [Environment]::SetEnvironmentVariable("$Variablename",$var, "User") [Environment]::SetEnvironmentVariable("$($Variablename)`_TimeSet",$(Get-Date), "User") } Return $var }