Rundeck output as clickable link - rundeck

Does anyone know if there is a way to have the output of an executed job be a clickable link?
I have a script that gets a link for a virtual environment in vCenter via powercli and I wanted to simplify the process in which the user got the vmdk console brought up. They currently have to select->copy->paste to search bar for it to work.
Here is my script:
Echo "Your Virtual Machine is now created and is being retrieved now."
$vcenter_password= "#option.vcenter_password#"
$Template_Name= "#option.Template_Name#"
if(-not (Get-Module -Name VMware.PowerCLI -ListAvailable)){
Install-Module -Name VMware.PowerCLI -AllowClobber -Force -Confirm:$false
}
Connect-VIServer -Server {{vcenter}} -User Administrator#DEPLOY.COMMS -Password $vcenter_password -Force
Get-VM $Template_Name | Select-Object #{N="IP Address";E={#($_.guest.IPAddress[0])}}
Echo "Your VM is available at the following URL for the next 30 seconds:"
Open-VMConsoleWindow -VM "$Template_Name" -urlonly

If your app supports HTML links like appname://, you can generate an HTML anchor element output in your Rundeck job. Take a look at this.

Related

Powershell popup after command execution

I want my script to run silently however with one command I am having an issue where I receive a popup for confirmation that I am unable to get rid of.
Set-GPPermissions -Name Lock -PermissionLevel None -TargetName "Authenticated Users" -TargetType Group -WarningAction SilentlyContinue -Confirm:$false

Stop connection message coming back for Connect-VIServer

I have an IIS machine hosting some PHP that calls a Powershell file. After adding the Powercli snap in in my Powershell file, I makes a connection to vSphere.
When it does it produces some output that I don't want as this gets put on to each php output page.
Name Port User ---- ---- ---- myhostaddress.com 443 mynetwork\reporteruser
I have tried to add variables to the end to stop the feedback
Connect-VIServer -server myhostaddress.com -User $logincred.User -Password $logincred.Password -WarningAction SilentlyContinue -ErrorAction Continue
but no use.
Is there a way to stop it. Or a clever way to call it so the output is dumped somewhere else please?
You can assign the connection to a variable to suppress the output:
$connection = Connect-VIServer -Server myhostaddress.com -Credential $cred
Working on PS v5.
before executing, run the following code:
Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false
You can pipe the output to Out-Null:
Connect-VIServer -Server ... | Out-Null
This is the PowerShell equivalent of redirecting stdout to /dev/null in Linux (... > /dev/null).

Powershell Workflow not able to use -parameters to create a VM

I am having a problem with my Powershell Workflow that I am authoring in Windows Azure Pack Admin Site. In Powershell ISE, the following works but in Service Management Portal Admin Site Runbook Authoring, it does not work. Where it is getting stuck is that it is saying that it cannot validate the arguments passed -Name. I remove -Name out and now -FullName doesnt work. It seems like all the switch parameters for the command is not working. Can anyone help me out?
param (
[string]$DomainAdminAcct,
[string]$DomainAdminPass,
[string]$ServerName
)
InlineScript {
Add-PSSnapin VMWare.VimAutomation.Core
$vCenter = "test300"
Connect-ViServer -server $vCenter -ErrorAction Stop
$myCluster = Get-Cluster -Name "DC Test"
$myTemplate = Get-Template -Name "2012dc" -Location "our company"
$OSCustomizationSpec = New-OSCustomizationSpec –Name “$ServerName” –FullName “$ServerName” –OrgName “our company” –Domain “our.domain.com” –DomainUsername “$DomainAdminAcct” –DomainPassword "$DomainAdminPass" -AdminPassword "changeme" -ChangeSid
New-VM -Name $ServerName -ResourcePool $myCluster -Template $myTemplate -OSCustomizationSpec $OSCustomizationSpec
}
}
Sounds like you have Hyper-V and PowerCLI modules loaded and the commands are conflicting. It is trying to run New-VM from the Hyper-V module. You can confirm this by running:
get-command New-VM -all
You should see two commands one from the Hyper-V module and one from the Vmware module.
To get past the problem you can add the module name to the command name:
VMware.VimAutomation.Core\New-VM
for using parameters inside INLINESTRING structure, you must use $using:<>

Recycle an application pool using a PowerShell script

I want to recycle my application pool using one-liner command which I can put in my PowerShell script. I added the following code in my PowerShell script:
Import-Module WebAdministration
$site = "Default Web Site"
$pool = (Get-Item "IIS:\Sites\$site"| Select-Object applicationPool).applicationPool
Restart-WebAppPool $pool
But I am getting an error that name IIS doesn't exist. How can I fix it?
Short and simple, like this...
Restart-WebAppPool (Get-Website -Name <YourSiteName>).applicationPool
You can use appcmd.exe:
appcmd recycle apppool /apppool.name:'MyAppPool'
You can also retrieve the corresponding WMI instance and invoke the Recycle() method:
$myAppPool = Get-WmiObject -Namespace root\WebAdministration -Class ApplicationPool -Filter "Name = 'MyAppPool'"
$myAppPool.Recycle()
The following command works for me
invoke-command -computername servername -scriptblock {C:\Windows\System32\inetsrv\appcmd.exe recycle apppool "apppoolname"}
This may work:
Write-Host "App Pool Recycling Started...."
& $env:windir\system32\inetsrv\appcmd list apppools /state:Started /xml | & $env:windir\system32\inetsrv\appcmd recycle apppools /in
Write-Host "App Pool Recycling Completed"
It works for me in AWS through the Run command.
Use:
Import-Module WebAdministration
$site = "MySite"
$pool = (Get-Item "IIS:\Sites\$site"| Select-Object applicationPool).applicationPool
#Recycle the application pool:
Restart-WebAppPool $pool
Since WebAdministration is old and badly supported under PowerShell 7, here's an updated answer using the fresher IISAdministration module:
Import-Module IISAdministration
$pool = Get-IISAppPool -Name "DefaultAppPool"
$pool.Recycle()
You can also do this as a two-liner, or even one-liner if you import the module in your profile:
Import-Module IISAdministration
(Get-IISAppPool("DefaultAppPool")).Recycle()
Do note that accessing the IISServerManager object requires administrative permissions.
I don't run all PowerShell scripts as administrator - it's dangerous.
But appcmd.exe is not available unless running in an administrator prompt.
This command line will prompt for elevation if you're running in a regular, un-elevated prompt:
Start-Process c:\windows\System32\inetsrv\appcmd.exe "recycle apppool /apppool.name:MyAppPool" -Verb RunAs
In case you need to recycle all the Application Pools:
Import-Module IISAdministration
Get-IISAppPool | ForEach-Object{ if($_.State -eq "Started"){ $_.Recycle() } }
If the AppPool is stopped it will throw an exception
Use the -Name option:
IIS:\> Restart-WebAppPool -Name "DefaultAppPool"

How do I troubleshoot Powershell user data scripts on AWS EC2?

I am trying to run a powershell script from the user data box when creating an ec2 instance from a custom AMI. I have enabled user data execution on the config before creating the ami.
This is what i put into user data
<powershell>
c:\scripts\github-download.ps1 someuser somepassword
</powershell>
The script it is calling is shown below.
Param($gituser, $gitpass)
C:\Users\Administrator\AppData\Local\GitHub\shell.ps1
git clone https://"$gituser":"$gitpass"#github.com/somegitrepo |out-null
I have no idea why this isn't working. Am i doing something wrong here? Any help really appreciated.
Instead of calling the user data using the <powsershell> tag, call PowerShell itself using the <script> tag. You gain command line control over its invocation, and can control execution policy and other command line settings directly:
<script>
PowerShell -ExecutionPolicy Bypass -NoProfile -File c:\scripts\github-download.ps1 -user USER -password PASSWORD
</script>
In your script, setup the beginning and end sections of your script as below:
# Server script called from userdata in this format
# <script>
# PowerShell -ExecutionPolicy Bypass -NoProfile -File c:\scripts\github-download.ps1 -user USER -password PASSWORD
# </script>
param (
[string]$user = $(throw "-user is required."),
[string]$password = $(throw "-password is required."),
)
Start-Transcript -Path C:\userscriptlog.txt
Import-Module WebAdministration
if ([System.Diagnostics.EventLog]::SourceExists("Userdata") -eq $False) {
New-Eventlog -Logname Application -Source 'Userdata'
}
Write-Eventlog -Logname Application -Source 'Userdata' -EventId 1 -EntryType Information -Message 'Begining post-deployment configuration script'
-- YOUR MAIN SCRIPT HERE --
Write-Eventlog -Logname Application -Source 'Userdata' -EventId 1 -EntryType Information -Message 'Post-deployment configuration script complete'
Stop-Transcript
For error handling in your script, you need to use robust exception handling and logging for each command, again to make troubleshooting and debugging easy. This block simply gets the current instance ID, but note the exception handling and logging built in:
# get instance-id
try {
$InstanceId = (Invoke-WebRequest http://169.254.169.254/latest/meta-data/instance-id).content
} catch {
$_.Exception.message | out-file c:\InstanceId_error.log
Write-Host "FATAL: InstanceId exception"
Exit
}
if (!$InstanceId) {
Write-Host "FATAL: InstanceId is null"
Exit
} else {
$InstanceId | out-file C:\InstanceId.txt
Write-Host "InstanceId: $InstanceId"
}
Try that approach to any command or shell invocation that you need to implement.
This powershell script 'wrapper' for user data scripts allows optional command line parameters, produces a transcript of execution, and logs events to the Windows event log, to confirm basic execution of the script.
It will provide a flexible framework for any Powershell based user data script, allow for easy debugging and testing.
| out-null silences any errors that could be happening with git clone so you won't know what is wrong unless you pipe the error somewhere else or just don't use | out-null.
I would manually run the command on the EC2 instance without the | out-null before you try and use user data to automate anything.