简体   繁体   English

使用批处理变量以管理员身份运行 powershell 命令

[英]Run powershell command as admin with a batch variable

I currently have a problem where there is a process that gets stuck in task manager.我目前有一个问题,其中有一个进程卡在任务管理器中。 I'm tired of going into the task manager and ending the random amount of tasks for my users.我厌倦了进入任务管理器并为我的用户结束随机数量的任务。 I would love to simplify the script that I have set up to not require placing the variable in a text file.我很想简化我设置的脚本,不需要将变量放在文本文件中。 This is what I have set up now:这是我现在设置的:

set curuser=Domain\%username%
powershell -noprofile -executionpolicy Bypass -command " '%curuser%' | Out-File -filepath C:\software\lynctemp.txt"
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""powershellscript.ps1""' -Verb RunAs}"
pause
del C:\software\lynctemp.txt
powershell -executionpolicy Bypass -command "start-process -filepath 'C:\Program Files (x86)\Microsoft Lync\communicator.exe'"

The powershell script that gets called is literaly one command but it requires the current user variable.被调用的 powershell 脚本实际上是一个命令,但它需要当前的用户变量。 I couldn't for the life of me figure out how to get the Run As switch to work in just calling the powershell command.我一生都无法弄清楚如何让运行方式开关仅通过调用 powershell 命令来工作。 Instead I made a text file to hold the variable and then I delete it after its done being used.相反,我制作了一个文本文件来保存变量,然后在使用完成后将其删除。 the only problem is that I would love to just pass the %curuser% variable right into the powershell call for example:唯一的问题是我很想将 %curuser% 变量直接传递到 powershell 调用中,例如:

powershell -noprofile -executionpolicy Bypass -command "get-process -includeUsername | Where-Object {$_.Username -eq %curuser% -and $_.processname -eq "communicator"} | Stop-Process -force" -verb runas

The reason I want to do that, is because if this gets ran twice on the same host, the current user wont be the current user and its set for failure.我想这样做的原因是,如果这在同一台主机上运行两次,当前用户将不会是当前用户,并且设置为失败。

I was also looking at the taskkill.exe but I need to make sure it doesn't just end a random task because the users are using a shared rds host.我也在查看 taskkill.exe,但我需要确保它不只是结束随机任务,因为用户正在使用共享的 rds 主机。 so if I end task it needs to be for that user.因此,如果我结束任务,则需要针对该用户。 If anyone has any ideas on better way of fixing this bug with Microsoft Lync 2010, Please let me know.如果有人对使用 Microsoft Lync 2010 修复此错误的更好方法有任何想法,请告诉我。 im just setting up a bandaid for when it opens 50 communicator processes which makes the program unusable.我只是在它打开 50 个通信器进程时设置了一个bandaid,这使得程序无法使用。

Hi I ended up finding a different solution after digging into the filter for taskkill.exe嗨,在深入研究 taskkill.exe 的过滤器后,我最终找到了不同的解决方案

set curuser=Domain\%username%
taskkill.exe /f /fi "USERNAME eq %curuser%" /im communicator.exe
ping pleasesleep.com
powershell -executionpolicy Bypass -command "start-process -filepath 'C:\Program Files (x86)\Microsoft Lync\communicator.exe'"

I've tested it on my local machine and am going to test tomorrow when the tickets come in.我已经在我的本地机器上测试过了,明天票进来的时候再测试一下。

I still am curious about my initial question though:不过,我仍然对我最初的问题感到好奇:

If I did want to pass paramaters to a powershell script that is being run as admin, How would I do so from a batch file?如果我确实想将参数传递给以管理员身份运行的 powershell 脚本,我将如何从批处理文件中执行此操作?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM