【问题标题】:Can't Run PSExec Command Within ISE无法在 ISE 中运行 PSExec 命令
【发布时间】:2022-01-27 10:08:06
【问题描述】:

第一篇文章和脚本/powershell 的新手,所以对于任何格式或我遗漏了什么表示歉意。目前正在尝试使用脚本从文本文件中重新启动 IP 列表。

当我运行脚本并在我的测试设备上运行持续 ping 时,没有任何反应。当我在 powershell 中运行 psexec \insert IP shutdown /r /t 0 时,它可以工作。想知道为什么我不能让 ISE 为我运行我的 psexec 命令?

$user = $env:USERNAME

if(!(Test-Path -Path c:\temp\Scripts\$user))
{
mkdir c:\temp\Scripts\$user -Force
}

if(!(Test-Path -Path C:\temp\Scripts\$user\DeviceIps.txt))
{
New-Item -Path C:\temp\Scripts\$user\DeviceIps.txt
}

Else
{
#If the file exists, it clears your previous query
Clear-Content -Path C:\temp\Scripts\$user\DeviceIps.txt
}

#Invokes file to enter multiple searches/filters
Invoke-Item -Path C:\temp\Scripts\$user\DeviceIps.txt
Pause

$ip = Get-Content -Path C:\Temp\Scripts\$user\DeviceIps.txt

foreach($ip in $DeviceIps){
& psexec.exe \\$ip shutdown /r /t 0 
}

【问题讨论】:

  • 看看this answer是否有帮助。
  • 顺便说一句:PowerShell ISE 是 no longer actively developedthere are reasons not to use it(底部),特别是无法运行 PowerShell (Core) 6+。提供最佳 PowerShell 开发体验的积极开发的跨平台编辑器是 Visual Studio Code 及其 PowerShell extension
  • 另外,有什么不好的呢?有什么错误吗? PowerShell 有自己的 cmdlet,也可以让您重新启动远程计算机:Restart-Computer -ComputerName computer。那么你就不必调用shutdown.exe了;尽管此实用程序还支持远程主机:/m \\ip-or-name。您为什么要使用 PSExec.exe 开始?

标签: powershell


【解决方案1】:

再看看您的 PSEXEC 命令,您可以像在批处理脚本中一样运行任何命令,因此请从命令行尝试,然后将其粘贴到您的代码中。

我没有时间对此进行测试,但这是一种处理方式:

psexec \\$IP "cmd.exe" "/c shutdown /f /r/ /t 60"
PsExec64.exe @IPList.txt -u UserId -p Password -c "/c shutdown /f /r/ /t 60"

如果您正在运行 Windows 远程管理,则不需要 psexec:

Invoke-Command -ComputerName $env:Computername -ScriptBlock { do something }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    • 1970-01-01
    • 2018-12-22
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    相关资源
    最近更新 更多