【问题标题】:How can I run a PowersShell script via SMB remotely?如何通过 SMB 远程运行 Powershell 脚本?
【发布时间】:2022-11-25 09:22:58
【问题描述】:
我正在尝试通过 SMB 连接运行 Powershell 脚本,但它不运行脚本:
smbclient hostname -U username%password -c "Powershell -File run.ps1"
它没有说:
Powershell: command not found
我想通过远程服务器上的 SMB 运行 PowerShell 脚本。
linux(从)-> Windows(到)
【问题讨论】:
标签:
windows
powershell
cmd
samba
smb
【解决方案1】:
-c/--command smbclient 参数仅用于SMB命令字符串,不适用于任意shell命令。见man pages。
这些是您可用的命令:
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 ..
!
您可以使用 ! <SHELL_COMMAND> 作为 shell 命令,但它们将在本地执行,而不是在您的远程服务器上执行。
换句话说,您无法通过 SMB 远程执行 PowerShell 脚本。寻找其他远程处理技术。您会在 [SO] 上找到足够多的 PowerShell 远程处理或 SSH 示例。
【解决方案2】:
Powershell Remoting 不是一种选择吗?
只需将 Invoke-Command 与 PSSession 对象或 -ComputerName <remotecomputer> 一起使用。它将允许您传递文件和脚本块,但您需要使用-ArgumentList 及其在脚本块的PARAM() 部分中定义的位置将参数传递给脚本块(如果有)。
【解决方案3】:
该错误表明未找到 Powershell?很可能是因为它不是您的 PATH 的一部分,并且您的 shell 不知道去哪里运行 PS 命令
根据您的 Linux 风格,快速查看 MS doc on the topic 以查看它是否已安装