powershell - 批处理文件、Powershell 脚本、PSExec 和系统用户

标签 powershell batch-file privileges psexec

我正在尝试对 Windows 任务计划程序进行一些监控,我有一个运行以下内容的 Powershell 脚本:

$serverName = hostname
$schedule = new-object -com("Schedule.Service")
$schedule.connect($serverName)
$tasks = $schedule.getfolder("\").gettasks(0)
$tasks |select name, lasttaskresult, lastruntime

这将返回运行它的服务器上的计划任务列表、上次任务结果和上次运行时间。这样做的目的是将数据集返回到我们的监控解决方案 (Geneos),我们可以将其用于警报。

我们有一个很大的 Win2008 Assets ,所以我希望脚本集中化,允许任何 Geneos 探测器调用它并为其主机返回数据集。为此,我将 powershell 包装在一个 .bat 文件中,该文件执行以下操作:
\\fileserverhk\psexec.exe -accepteula -u admin -p "pwd" powershell.exe cpi \\fileserverhk\scripts\TaskSchedulerMonitor.ps1 -Destination C:\Monitor\TaskSchedulerMonitor.ps1
\\fileserverhk\psexec.exe -accepteula -u admin -p "pwd" powershell.exe -ExecutionPolicy Bypass -File C:\Monitor\TaskSchedulerMonitor.ps1

第一步在本地复制 .ps1 文件以绕过不信任 UNC 路径的 Powershell,第二部分运行脚本。

如果我从测试服务器手动运行 .bat 文件,它运行良好(这是在管理员帐户下登录的)。但是,当我通过 Geneos(在 SYSTEM 帐户下运行)启动 .bat 文件时,我得到:
Access is denied.
PsExec could not start powershell.exe:

所以基本上我的问题是,如何让 PsExec 在 SYSTEM 帐户下运行时切换用户?尽管 PsExec 为另一个帐户设置了凭据,但在系统下运行时显然有一些东西阻止它更改。

我阅读尝试使用 运行它-h 切换,但出现以下错误:
The handle is invalid.
Connecting to local system...


Starting PsExec service on local system...

Connecting with PsExec service on <server>...

Starting powershell.exe on <server>...

Error communicating with PsExec service on <server>:

除了上述错误之外,我最终将 PSExec 和 powershell 进程卡在远程机器上。有趣的部分是我可以看到在 SYSTEM 下运行的 PSExec 和 PSEXEC.SVC 以及在 admin 下运行的 powershell,所以它几乎就在那里,但有些地方不太正确。

最佳答案

我们设法在 Windows 上使用 powershell 包装器到达那里。 schtasks 命令( link here )。 Schtasks 可以在 SYSTEM 帐户下运行,并将返回所有必要的任务信息,因此我们不再需要对权限进行胡说八道,也不再需要在环境中使用明文密码(奖金)。

我们包装了:

schtasks.exe Query /FO CSV

在 powershell 脚本中,并使用 PS 将输出格式化为 Geneos 期望的 csv 样式。

关于powershell - 批处理文件、Powershell 脚本、PSExec 和系统用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13301633/

相关文章:

linux - 在没有 --privileged 的​​ docker 容器中使用 perf

PowerShell 命令在文件名的特定位置添加字符

Powershell Test-NetConnection 在循环脚本中返回 False

azure - 如何将 AzureKeyVault 中的 secret 用于 PowerShell -Credential?

batch-file - 如何在批处理文件中增加txt文件中的值?

sql - 仅使用 SELECT 用户执行不受信任的 SQL

powershell - 通过Powershell为另一个用户设置环境变量

编译一段C代码,然后检查输出是否为空并运行它

mysql - JDBC 批量插入性能

javascript - 为什么 window.showModalDialog 被弃用?用什么代替?