logo

NJP

Awesomenow - MidServer restart linux vm midserver using windows midserver vm

Import · Jun 16, 2021 · article

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/bashsudo -u root /usr/local/zzawesomenow/opt/servicenow/mid/agent/bin/mid.sh stopsleep 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.ps1midservername = "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"?>';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)

Install a MID Server on Linux | ServiceNow Docs

View original source

https://www.servicenow.com/community/itom-blog/awesomenow-midserver-restart-linux-vm-midserver-using-windows/ba-p/2274802