Examples/IC/run.ps1
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "")] param() # run.ps1 auto-generated by EasyAzureFunction module, https://github.com/iricigor/EasyAzureFunction, iricigor@gmail.com 'run.ps1 start' # POST method: $req $requestContent = Get-Content $req -Raw try { $requestBody = $requestContent | ConvertFrom-Json $Failed = $false "Params (JSON): $($requestBody.PSObject.Properties.Name -join `",`")" $EzInvokeCommand = $requestBody.EzInvokeCommand $EzSession = $requestBody.EzSession $EzComputerName = $requestBody.EzComputerName $EzCredential = $requestBody.EzCredential $EzPort = $requestBody.EzPort $EzUseSSL = $requestBody.EzUseSSL $EzConfigurationName = $requestBody.EzConfigurationName $EzApplicationName = $requestBody.EzApplicationName $EzThrottleLimit = $requestBody.EzThrottleLimit $EzConnectionUri = $requestBody.EzConnectionUri $EzAsJob = $requestBody.EzAsJob $EzInDisconnectedSession = $requestBody.EzInDisconnectedSession $EzSessionName = $requestBody.EzSessionName $EzHideComputerName = $requestBody.EzHideComputerName $EzJobName = $requestBody.EzJobName $EzScriptBlock = $requestBody.EzScriptBlock $EzNoNewScope = $requestBody.EzNoNewScope $EzFilePath = $requestBody.EzFilePath $EzAllowRedirection = $requestBody.EzAllowRedirection $EzSessionOption = $requestBody.EzSessionOption $EzAuthentication = $requestBody.EzAuthentication $EzEnableNetworkAccess = $requestBody.EzEnableNetworkAccess $EzRunAsAdministrator = $requestBody.EzRunAsAdministrator $EzInputObject = $requestBody.EzInputObject $EzArgumentList = $requestBody.EzArgumentList $EzVMId = $requestBody.EzVMId $EzVMName = $requestBody.EzVMName $EzContainerId = $requestBody.EzContainerId $EzCertificateThumbprint = $requestBody.EzCertificateThumbprint } catch {$Failed = $true} if ($Failed) { "Params (URLEncoded): $requestContent" $requestContent -split '&' | % { $v = $_ -split '=' if ($v[1]) {Set-Variable -Name $v[0] -Value $v[1]} } } # prepare output, either default web-page or invoke command cd $EXECUTION_CONTEXT_FUNCTIONDIRECTORY if (!$EzInvokeCommand) { 'show default web page' $Output = Get-Content .\index.html -Raw } else { 'invoke command' try { $ParamsHash = @{} if ($EzSession) {$ParamsHash.Add('Session',@($EzSession -replace '%2C',',' -split ','))} if ($EzComputerName) {$ParamsHash.Add('ComputerName',@($EzComputerName -replace '%2C',',' -split ','))} if ($EzCredentialUserName -and $EzCredentialPassword) { $EzCredentialSecPass = ConvertTo-SecureString $EzCredentialPassword -AsPlainText -Force $EzCredential = New-Object System.Management.Automation.PSCredential ($EzCredentialUserName, $EzCredentialSecPass) $ParamsHash.Add('Credential',$EzCredential) } if ($EzPort) {$ParamsHash.Add('Port',$EzPort)} if ($EzUseSSL) {$ParamsHash.Add('UseSSL',$True)} if ($EzConfigurationName) {$ParamsHash.Add('ConfigurationName',$EzConfigurationName)} if ($EzApplicationName) {$ParamsHash.Add('ApplicationName',$EzApplicationName)} if ($EzThrottleLimit) {$ParamsHash.Add('ThrottleLimit',$EzThrottleLimit)} if ($EzConnectionUri) {$ParamsHash.Add('ConnectionUri',@($EzConnectionUri -replace '%2C',',' -split ','))} if ($EzAsJob) {$ParamsHash.Add('AsJob',$True)} if ($EzInDisconnectedSession) {$ParamsHash.Add('InDisconnectedSession',$True)} if ($EzSessionName) {$ParamsHash.Add('SessionName',@($EzSessionName -replace '%2C',',' -split ','))} if ($EzHideComputerName) {$ParamsHash.Add('HideComputerName',$True)} if ($EzJobName) {$ParamsHash.Add('JobName',$EzJobName)} if ($EzScriptBlock) {$ParamsHash.Add('ScriptBlock',$EzScriptBlock)} if ($EzNoNewScope) {$ParamsHash.Add('NoNewScope',$True)} if ($EzFilePath) {$ParamsHash.Add('FilePath',$EzFilePath)} if ($EzAllowRedirection) {$ParamsHash.Add('AllowRedirection',$True)} if ($EzSessionOption) {$ParamsHash.Add('SessionOption',$EzSessionOption)} if ($EzAuthentication) {$ParamsHash.Add('Authentication',$EzAuthentication)} if ($EzEnableNetworkAccess) {$ParamsHash.Add('EnableNetworkAccess',$True)} if ($EzRunAsAdministrator) {$ParamsHash.Add('RunAsAdministrator',$True)} if ($EzInputObject) {$ParamsHash.Add('InputObject',$EzInputObject)} if ($EzArgumentList) {$ParamsHash.Add('ArgumentList',@($EzArgumentList -replace '%2C',',' -split ','))} if ($EzVMId) {$ParamsHash.Add('VMId',@($EzVMId -replace '%2C',',' -split ','))} if ($EzVMName) {$ParamsHash.Add('VMName',@($EzVMName -replace '%2C',',' -split ','))} if ($EzContainerId) {$ParamsHash.Add('ContainerId',@($EzContainerId -replace '%2C',',' -split ','))} if ($EzCertificateThumbprint) {$ParamsHash.Add('CertificateThumbprint',$EzCertificateThumbprint)} "Params: $($ParamsHash.Keys -join `",`")" $Output = Invoke-Command @ParamsHash -ea Stop | Out-String if ($Output) {$Color = 'white'} else {$Color = 'gray'; $Output = 'Command run successfully, but it returned no output'} } catch { $Output = $_ $Color = 'red' } $Head = "<head><style>body {background-color: #012456; color: $Color;}</style><title>EasyAzureFunction - Invoke-Command running example</title></head>" $Back = '<p><a href="javascript:history.back()" style="color:yellow;">Go Back</a></p>' $Output = $Head + '<pre>' + $Output + $Back + '</pre>' $Output = $Output -replace "`n",'</br>' } # parse and send back output $Output2 = [PSCustomObject]@{Status = 200; Body = ''; Headers = @{}} $Output2.Headers.Add('content-type','text/html') $Output2.Body = $Output -replace '"',"'" Out-File -Encoding utf8 -FilePath $res -inputObject ($Output2 | ConvertTo-JSON) |