简体   繁体   English

PowerShell 脚本在 ISE 中运行,但不作为脚本运行

[英]PowerShell script runs in ISE but not as a script

OBJECTIVES :-目标:-

I'm having trouble with something that should be relatively simple?我在一些应该相对简单的事情上遇到了麻烦? Searched similar questions here but no cigar.在这里搜索了类似的问题,但没有雪茄。 I dont want to have to pass a command to the file as a Ps1 script (common suggestion).我不想将命令作为 Ps1 脚本传递给文件(常见建议)。 I'm hoping I can just right click, run with PowerShell and it works.我希望我可以右键单击,使用 PowerShell 运行并且它可以工作。

SITUATION :-情况 :-

I have to automate uninstalling of software often.我必须经常自动卸载软件。 Rather than scour the registry for msiexec strings, I edited some PS code so I can search for what I want, then do whatever I need to do with the info I receive.我没有在注册表中搜索 msiexec 字符串,而是编辑了一些 PS 代码,这样我就可以搜索我想要的内容,然后对我收到的信息做任何我需要做的事情。 The below runs fine in PowerShell ISE, but as a PS1 script (right click, run with PS) it outputs nothing on the screen.以下在 PowerShell ISE 中运行良好,但作为 PS1 脚本(右键单击,使用 PS 运行),它在屏幕上不输出任何内容。

CODE :-代码 :-

$Prog = Read-Host -Prompt 'Input your Prog name'
Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Where-Object {$_.DisplayName -match "$Prog"} | Select-Object -Property DisplayName, UninstallString

pause

Lets say at the prompt I search for 7-Zip in ISE I get this...让我们说在提示符下我在 ISE 中搜索 7-Zip 我得到了这个......

Input your Prog name: 7-zip

DisplayName       UninstallString                     
-----------       ---------------                     
7-Zip 19.00 (x64) C:\Program Files\7-Zip\Uninstall.exe

Running as a script, I get nothing output into the console?作为脚本运行,我没有得到任何输出到控制台?

Input your Prog name: 7-zip

Press Enter to continue...:

QUESTION :- What do I need to do to make the script behave like it does in the ISE console?问题:- 我需要做什么才能使脚本在 ISE 控制台中表现得像它一样?

Many thanks for any assistance非常感谢您的帮助

PS (<see what I did there, I'm new to the PowerShell game, thanks>) PS(<看看我在那里做了什么,我是 PowerShell 游戏的新手,谢谢>)

It works in Powershell too, if you run it from Powershell Interactive Prompt or run using powershell -file "FILEPATH" in CMD/PowerShell, the output remains on the screen.它也适用于 Powershell,如果您从 Powershell Interactive Prompt 运行它或在 CMD/PowerShell 中使用powershell -file "FILEPATH"运行,输出将保留在屏幕上。 But when you run directly with a double-click output doesn't stay persistent on the screen, instead when it reaches EOF then it quits.但是,当您直接使用双击输出运行时,它不会在屏幕上持续存在,而是在到达 EOF 时退出。 So, Add Read-Host or cmd /c pause at the end to stop the program at the end, to view output.因此,在末尾添加Read-Hostcmd /c pause以在最后停止程序,以查看输出。

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

相关问题 脚本在ISE中运行,但不在Powershell中运行 - Script runs in ISE but not in Powershell 脚本可以在ISE中正常运行,但不能在Powershell控制台中运行 - Script runs fine in ISE, but not in Powershell Console 脚本使用 ISE 运行,但不通过 PowerShell 控制台运行 - Script runs with ISE but not via PowerShell console 脚本与 ISE 一起运行,但不是 powershell.exe - Script runs with ISE but not powershell.exe Powershell:脚本不是从命令行运行,而是从ISE运行? - Powershell: Script not running from command line but runs from ISE? Powershell 脚本仅在 ISE 中运行 - Powershell script only running in ISE Powershell脚本可在ISE中使用,但不能在控制台中使用 - Powershell script works in ISE but not in console 在 PowerShell ISE 中作为 PowerShell 脚本的一部分运行的 c# 代码中使用 ReadLine 从用户获取输入 - Get input from the user using ReadLine in c# code that runs as part of PowerShell script in PowerShell ISE 无论脚本在“常规” PowerShell控制台还是在Powershell ISE中运行,如何以相同的方式处理错误? - How to handle errors in a way that works the same whether the script runs in “regular” powershell console or in powershell ISE? Powershell脚本可在ISE中使用,但不能在Power With Powershell中运行 - Powershell script works in ISE but not in Run With Powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM