简体   繁体   English

如何使用参数将Powershell脚本调用到另一个Powershell脚本?

[英]How to call a powershell script to another powershell script with arguments?

I just need to call another script and pass the parameters to it. 我只需要调用另一个脚本并将参数传递给它即可。

I tried doing invoke-expression to access it, i tried using &, and nothing worked 我尝试执行invoke-expression来访问它,我尝试使用&,但没有任何效果

I tried doing the following: 我尝试执行以下操作:

 $hostfile = "C:\Users\username\Desktop\csvfile_test.csv"
 $outFile = ".\testerFile.xlsx"
 &  '.\organizer.ps1' "-csvFile $hostfile -outputPath $outFile "

Invoke-Expression 'C:\Users\username\Desktop\organizer.ps1' "$hostfile $outFile"

I receive the following errors:. 我收到以下错误:

with ampersand (&): 和符号(&):

PS C:\Users\username\Desktop> C:\Users\username\Desktop\scanner.ps1
Exception calling "ReadLines" with "1" argument(s): "The given path's format is not supported."
At C:\Users\username\Desktop\scanner.ps1:48 char:1
+ [System.IO.File]::ReadLines("$csvFile") | ForEach-Object {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : NotSupportedException

with invoke-expression: 使用invoke-expression:

Exception calling "ReadLines" with "1" argument(s): "The given path's format is not supported."
At C:\Users\username\Desktop\scanner.ps1:48 char:1
+ [System.IO.File]::ReadLines("$csvFile") | ForEach-Object {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : NotSupportedException

Invoke-Expression : A positional parameter cannot be found that accepts argument 'C:\Users\username\Desktop\csvfile_test.csv .\testerFile.xlsx'.
At C:\Users\username\Desktop\scanner.ps1:69 char:1
+ Invoke-Expression 'C:\Users\username\Desktop\organizer.ps1' "$host ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Expression], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeExpressionCommand

When you write this: 当您编写此代码时:

& '.\organizer.ps1' "-csvFile $hostfile -outputPath $outFile "

you are passing a single parameter to the script (one quoted string). 您将单个参数传递给脚本(一个带引号的字符串)。 That's not what you want. 那不是你想要的。

This is what you need: 这是您需要的:

$hostfile = "C:\Users\username\Desktop\csvfile_test.csv"
$outFile = ".\testerFile.xlsx"
.\organizer.ps1 -csvFile $hostfile -outputPath $outFile

First, you don't need the & (invocation) operator because your command name (the organizer.ps1 script in the current location, in this example) doesn't contain spaces. 首先,您不需要& (调用)运算符,因为命令名(在此示例中,当前位置的organizer.ps1脚本)不包含空格。 (You can add it if you want, but it's unnecessary in this scenario.) (您可以根据需要添加它,但是在这种情况下是不必要的。)

Second, the -csvFile and -outputPath parameters each require a string. 其次, -csvFile-outputPath参数每个都需要一个字符串。

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

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