简体   繁体   English

Powershell 脚本在没有“powershell.exe -File”的情况下无法正常工作,并且在“powershell.exe -File”的情况下也无法正常工作

[英]Powershell script not working without "powershell.exe -File" and it's also not working properly with "powershell.exe -File"

I have the script that works perfectly when run from ISE or VS Code with parameters given in code like我有一个脚本,当从 ISE 或 VS Code 运行时,使用代码中给出的参数完美运行

 param(
      [string]$Param1 = "example",
      [string[]]$Param2 = @('arrayparam1','arrayparam2'),
      [string[]]$Param3 = @('arrayparam1','arrayparam2'),
      [switch]Param4 = $true
    )

But whenever I run this script without params in code but with params from console like但是每当我运行这个脚本时,代码中没有参数,但有来自控制台的参数,比如

.\Script -Param1 example -Param2 'arrayparam1','arrayparam2' -Param3 'arrayparam1','arrayparam2' -Param4

it starts running but it generates empty csv file (with param in code this works perfectly).它开始运行,但它生成空的 csv 文件(在代码中使用参数可以完美地工作)。 So I was running this script with "powershell.exe -File" (which generated nice file) like this:所以我用“powershell.exe -File”(它生成了不错的文件)运行这个脚本,如下所示:

powershell.exe -File .\Script -Param1 example -Param2 'arrayparam1','arrayparam2' -Param3 'arrayparam1','arrayparam2' -Param4

But the problem is that it takes only first item from array.但问题是它只需要数组中的第一项。 If second array is empty it gives second item from first array to second array as first item (this blew my mind).如果第二个数组为空,它将从第一个数组到第二个数组的第二个项目作为第一项(这让我大吃一惊)。

My goal is to run this script with all parameters from console without "-powershell.exe -File".我的目标是在没有“-powershell.exe -File”的情况下使用来自控制台的所有参数运行此脚本。 I have tried various ExecutionPolicy and now I'm using RemoteSigned.我已经尝试了各种 ExecutionPolicy,现在我正在使用 RemoteSigned。 I am using powershell, powershell ISE and VS code to test this but it always works the same.我正在使用 powershell、powershell ISE 和 VS 代码来测试这个,但它总是一样工作。 I have tried passing arrays as "1","2", ('1','2'), @("1","2") but it never works.我试过将数组传递为 "1","2", ('1','2'), @("1","2") 但它从来没有工作。

Please help请帮忙

Thanks @mklement0, you actually helped because I have started to look more deeply into my code and I discovered that for some reason passing an array of pscustomobject to some other function is not working without "powershell.exe -File" so making it a global variable solved the problem and script now runs well with just ".\\Filename.psi -param ex -param2 ex2, etc".谢谢@mklement0,你真的有所帮助,因为我已经开始更深入地研究我的代码,我发现由于某种原因,将 pscustomobject 数组传递给其他函数在没有“powershell.exe -File”的情况下无法正常工作,因此使其成为全局变量解决了这个问题,脚本现在运行良好,只需“.\\Filename.psi -param ex -param2 ex2, etc”。 Thanks everyone for the help :D谢谢大家的帮助:D

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

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