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

asked 21 Sep '17, 12:40

davidwolf's gravatar image

davidwolf
271512
accept rate: 14%


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.

link

answered 21 Sep '17, 17:16

davidwolf's gravatar image

davidwolf
271512
accept rate: 14%

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.

link

answered 27 Sep '17, 15:48

davidwolf's gravatar image

davidwolf
271512
accept rate: 14%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×16
×10
×4

Asked: 21 Sep '17, 12:40

Seen: 139,057 times

Last updated: 27 Sep '17, 15:48