简体   繁体   English

将自定义对象传递到另一个ps1脚本

[英]passing custom object to another ps1 script

I'd like to pass a custom made object from script to another. 我想将自定义对象从脚本传递到另一个。

Beginning of subscript.ps1 there are input parameters: subscript.ps1的开头有输入参数:

param(
  [string]$someString,
  [object]$custClassData
 )

In the main.ps1 I'm trying to call the subscript.ps1 after introducing a custom object: 在main.ps1中,我尝试在引入自定义对象后调用subscript.ps1:

class custClass{
   [string]$string1
   [string]$string2
   [string]$string3
}

$customizedObject = [custClass]::new()
$customizedObject.string1 = "smthng1"
$customizedObject.string2 = "smthng2"
$customizedObject.string3 = "smthng3"
$scriptPath = ".\subscript.ps1"
$smString = "somethingsomething"
powershell.exe -file $scriptPath -someString $smString -custClassData $customizedObject

When calling like this if i check in subscript $custClassData.GetType it returns System.String, so i only get the name of the object there. 当像这样调用时,如果我签入下标$ custClassData.GetType,它将返回System.String,所以我只在那里获得对象的名称。 If I generate class and object in the powershell manually and put data there and pass it to the subscript the type is custClass. 如果我在powershell中手动生成类和对象,然后将数据放入那里并将其传递给下标,则类型为custClass。

In subscript.ps1 the $custClassData parameter needs to validate the type [CustClass] not [object] . subscript.ps1中$custClassData参数需要验证类型[CustClass]而不是[object] So something like: 所以像这样:

param(
  [string]$someString,
  [CustClass]$custClassData
 )

This way the data that is passed to that parameter must be of the type [CustClass] . 这样,传递给该参数的数据必须为[CustClass]类型。

Additionally, the way you are calling subscript.ps1 does not look correct. 此外,您调用subscript.ps1的方式看起来不正确。 You do not need to call powershell.exe in order to invoke subscript.ps1 . 您无需调用powershell.exe即可调用subscript.ps1 powershell.exe will always throw an error here. powershell.exe总是会在这里引发错误。

You should change subscript.ps1 to subscript.psm1 , and turn the contents of the script into a function, and use it like this: 您应该将subscript.ps1更改为subscript.psm1 ,并将脚本的内容转换为函数,并按以下方式使用它:

In subscript.psm1 : 在subscript.psm1中

function Do-TheNeedful {
    param(
      [string]$someString,
      [CustClass]$custClassData
    )
    #~
    # do work
    #~
}

In main.ps1 在main.ps1中

class custClass{
   [string]$string1
   [string]$string2
   [string]$string3
}

Import-Module subscript.psm1

$customizedObject = [custClass]::new()
$customizedObject.string1 = "smthng1"
$customizedObject.string2 = "smthng2"
$customizedObject.string3 = "smthng3"
Do-TheNeedful -someString "a_string" -custClassData $customizedObject

Calling powershell.exe casts everything to strings. 调用powershell.exe会将所有内容转换为字符串。 Launch the script file directly instead: 而是直接启动脚本文件:

File: sub.ps1 文件: sub.ps1

param(
  [object]$foo
)

$foo

File: main.ps1 文件: main.ps1

class myClass{
    [string]$A
}

$myObject = [myClass]::new()
$myObject.A = "BAR"

.\sub.ps1 $myObject

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

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