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