【发布时间】: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 developed 和 there 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