Home > Software engineering >  How can I run a PowerShell script via SMB remotely?
How can I run a PowerShell script via SMB remotely?

Time:11-28

I am trying to run a PowerShell script via SMB connection, but it does not run the script:

smbclient hostname -U username%password -c "Powershell -File run.ps1"

It fails saying:

Powershell: command not found

I want to run a PowerShell script via SMB on a remote server.

linux(from) -> Windows(to)

CodePudding user response:

The -c/--command parameter of smbclient is for SMB command strings only, not for arbitrary shell commands. See man pages.

These are your available commands:

smb: \> ?
?              allinfo        altname        archive        backup         
blocksize      cancel         case_sensitive cd             chmod          
chown          close          del            deltree        dir            
du             echo           exit           get            getfacl        
geteas         hardlink       help           history        iosize         
lcd            link           lock           lowercase      ls             
l              mask           md             mget           mkdir          
more           mput           newer          notify         open           
posix          posix_encrypt  posix_open     posix_mkdir    posix_rmdir    
posix_unlink   posix_whoami   print          prompt         put            
pwd            q              queue          quit           readlink       
rd             recurse        reget          rename         reput          
rm             rmdir          showacls       setea          setmode        
scopy          stat           symlink        tar            tarmode        
timeout        translate      unlock         volume         vuid           
wdel           logon          listconnect    showconnect    tcon           
tdis           tid            utimes         logoff         ..             
!

You can use ! <SHELL_COMMAND> for shell commands, but they will be executed locally, not on your remote server.

In other words, you cannot execute PowerShell scripts remotely via SMB. Look for other remoting technologies. You will find more than enough examples for PowerShell remoting or SSH here on [SO].

CodePudding user response:

Is Powershell Remoting not an option?

Just use Invoke-Command with either a PSSession object or with -ComputerName <remotecomputer>. It will let you pass files and script blocks but you'll need to pass arguments to script blocks (if any) using -ArgumentList and their position as defined in the script block's PARAM() section.

CodePudding user response:

The error indicates that Powershell isn't found? Most likely because it's not part of your PATH, and your shell doesn't know where to look to run PS commands Depending on your Linux, flavor, have a quick look at the MS doc on the topic to see if it's installed

  • Related