Powershell Execute Script Action

Viewed 0

I'm trying to develop a PS script that will automatically run a command as sudo on the Ubuntu server when a disk space threshold is reached. The script works when run from my pc, but doesn't run when the action executes, apparently. Is there anyway to see why an execute script failed? The actual sudo command is just to test to make sure its doing an action Script below

#install module
Install-Module -Name Posh-SSH -force

#create username/pw
$userName = "DOMAIN\ubuntuadmin"
$password = "AdminPW"

#create $cred as secure string using $username and $password
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr

#create ssh session to server that fired alert using $cred
$sessionSSH = New-SSHSession -ComputerName $mon.ComputerName -Credential $cred -AcceptKey

#set stream
$stream = $sessionSSH.session.CreateShellStream("PS-SSH", 0, 0, 0, 0, 1000)

#invokes stream to run command as sudo. gets pw from $secstr
$result = Invoke-SSHStreamExpectSecureAction -ShellStream $stream -command "sudo rm /boot/initrd.img-4.4.0-93-generic.delete" -ExpectString "[sudo] password for" -SecureAction $secstr

sleep -s 60

#gets all sessions and ends them
Get-SSHSession | Remove-SSHSession
2 Answers

I've discovered this is, at least in part, because I don't have the Posh-SSH module installed on the monitor server or satellites.

I've since installed and imported the module needed on both the central server and satellites. I've discovered the error preventing the script from running is:

The term ‘New-SSHSession’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

But I don't know what to do from this point because I can successfully run the script manually from the server and satellite.

Related