- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
This blog talks about a solution on how to use a Windows VM as a controller to stop / start midservers installed on linux VMs.
Youtube demo : https://youtu.be/Zwt8B20-nu0
Install- Posh-SSH on Windows vm
Install-Module Posh-SSH
Get-Help New-SSHSession
Create a shell script mid.sh in linux vm that will restart midserver
bash /home/zzawesomenow/mid.sh
Create powershell script passwordgen.ps1 to store linux vm password
Create powershell script restartlinuxmid.ps1 to make ssh connection and execute a shell script
Create a flow in ServiceNow to call powershell script
Test action with following input variables
scriptname = C:\awesomenowscripts\restartlinuxmid.ps1
midservername = 20.X5.2X8.230
midserverusername = zzawesomenow
Password script (passwordgen.ps1)
##############
# Input and validate password and store encrypted in file for later use.
$userId= "zzawesomenow"
$pwFile = "$PSScriptRoot\$userId.Pw.txt"
do {
$password1 = Read-Host "$tryAgain`Enter $adminId Password: " -AsSecureString
$password2 = Read-Host "Verify $adminId Password: " -AsSecureString
$check1 = ([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password1)).ToString()
$check2 = ([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password2)).ToString()
if ($check1.SubString(0,($check1.Length)-4) -eq $check2.SubString(0,($check2.Length)-4)) {$pwMatch = $true}
else {$tryAgain = "Passwords did not match, try again.`n"; $pwMatch = $false}
}
Until ($pwMatch)
$password1 | ConvertFrom-SecureString | Out-File $pwFile -Force
Actual PS script (restartlinuxmid.ps1)
##############
$computerName=$args[0]
#$computerName = "linuxhostname"
$userId=$args[1]
#$userId = 'zzawesomenow'
$pwFile = "$PSScriptRoot\$userId.Pw.txt"
$pwd = Get-Content $pwFile | ConvertTo-SecureString
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $userId, $pwd
try {
"Attempting SSH to $computerName"
$sshSession = New-SSHSession -ComputerName $computerName -Credential $creds -AcceptKey -ConnectionTimeout 10 -ErrorAction Stop
$sessionId = $sshSession.SessionId
"Session $sessionId opened."
$command = "bash /home/zzawesomenow/mid.sh"
"Command: {0}" -f $command
$sshOut = (Invoke-SSHCommand -SessionId $sessionId -Command $command).Output
"Results: '{0}'" -f $sshOut
Remove-SSHSession -SessionId $sessionId | Out-Null
"Session $sessionId closed."
}
catch {$_.exception.Message}
Linux script
************
[zzawesomenow@zzawesomenowm ~]$ more mid.sh
#!/bin/bash
sudo -u root /usr/local/zzawesomenow/opt/servicenow/mid/agent/bin/mid.sh stop
sleep 15s
sudo -u root /usr/local/zzawesomenow/opt/servicenow/mid/agent/bin/mid.sh start
Input to flow action
********************
script name = C:\ServiceNowScripts\midserverhk\midserverhsv4_fc.ps1
midservername = "linuxhostname"
midserverusername = 'zzawesomenow'
ServiceNow flow action script
************************
Input variables
script name
midserver name
midserver username
(function execute(inputs, outputs) {
/************************************************
* Get the name of a Active Mid Server
*************************************************/
var midName;
var eccObj = new GlideRecord('ecc_agent_capability_m2m');
eccObj.addQuery('capability.capability', 'PowerShell');
eccObj.addQuery('agent.status', 'Up');
eccObj.query();
if (eccObj.next()) {
midName = 'mid.server.' + eccObj.agent.name;
}
/************************************************
* Construct powershell file name from input variables
*************************************************/
var scriptName = inputs.scriptname + " " + inputs.midservername + " " + inputs.midserverusername;
/************************************************
* Create output ecc record to execute the powershell file in mid server
/*************************************************/
var ecc = new GlideRecord('ecc_queue');
ecc.initialize();
ecc.agent = midName;
ecc.topic = 'Command';
ecc.name = 'powershell ' + scriptName;
ecc.queue = 'output';
ecc.state = 'ready';
ecc.source = 'linuxvm.Powershell';
ecc.payload = '<?xml version="1.0" encoding="UTF-8"?><parameters><parameter name="skip_sensor" value="true"/></parameters>';
ecc.insert();
})(inputs, outputs);
No output variables
Pre reqs :
There should be a windows VM that has a running midserver and that midserver should have capability as powershell.
Reference : SSH from PowerShell with Posh-SSH | Frank Contreras
Posh-SSH/New-SSHSession.md at master · darkoperator/Posh-SSH · GitHub
How to extract RPM package on Linux system (sharadchhetri.com)
- 582 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.