简体   繁体   English

从命令行运行PowerShell ISE?

[英]Run PowerShell ISE from the command line?

So I have a script that works fine when I run it from PowerShell ISE. 所以当我从PowerShell ISE运行它时,我有一个可以正常工作的脚本。 However, I need to automate it, so I run powershell.exe .\\script.ps1 , but I get errors about some of the commands not being recognized (they are from a non-standard module). 但是,我需要自动化它,所以我运行powershell.exe .\\script.ps1 ,但是我得到一些未被识别的命令的错误(它们来自非标准模块)。

Any help is appreciated, thanks! 任何帮助表示赞赏,谢谢!

Edit the beginning of your script to import all dependencies(modules). 编辑脚本的开头以导入所有依赖项(模块)。 This is good practice as it makes the code more readable and works with both PS 2.0 an 3.0+ 这是一个很好的做法,因为它使代码更具可读性,适用于PS 2.0和3.0+

script.ps1 script.ps1

#Import example module
Import-Module ActiveDirectory

#Script start
$name = Read-Host "Username"
$user = Get-ADUser $name
.....

One reason should be that a script is dot sourced or a module is loaded from your profile script. 一个原因应该是脚本来源或从您的配置文件脚本加载模块。 In this case your problem can come from the fact that starting PowerShell from the command line and starting PowerShell ISE does not use systematicaly the same profile script. 在这种情况下,您的问题可能来自以下事实:从命令行启动PowerShell并启动PowerShell ISE不会使用系统相同的配置文件脚本。 Have a look at $Profile var in each one and edit the associated file. 看看每个中的$Profile var并编辑相关文件。

$Profile on my ISE : $Profile ISE上的$Profile

C:\Users\JPB\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

$Profile on my command Line : 在我的命令行上的$Profile

C:\Users\JPB\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

More about profiles. 更多关于档案。 About_Profile About_Profile

A way to know all your $Profile paths : 一种了解所有$Profile路径的方法: 在此输入图像描述

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

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