Powershell:如何创建自定义对象并将其传递给 powershell 中的函数? [英] Powershell: How to create custom object and pass it to function in powershell?

查看:111
本文介绍了Powershell:如何创建自定义对象并将其传递给 powershell 中的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 PowerShell 中创建一个具有属性的自定义对象,然后将该对象传递给一个函数.我找到了创建自定义对象的在线示例,但它使用的是 HashTable.但是,我有一个带有属性的对象,而不是一个对象数组.

I want to create a custom object with properties in PowerShell and then pass that object to a function. I found the online example to create custom object, but its using HashTable. However, I have single object with properties, not an array of objects.

  1. 如果可能,我想创建单个对象而不是哈希表.
  2. 如果使用 HashTables,我该如何检索对象并将其传递给函数?

下面是我的代码示例:

function CreateObject()
{       
  $properties = @{      
    'TargetServer' = "ServerName";
    'ScriptPath' = "SomePath";
    'ServiceName' = "ServiceName"
 }

   $obj = New-Object -TypeName PSObject -Property $properties    

   Write-Output $obj.TargetServer
   Write-Output $obj.ScriptPath
   Write-Output $obj.ServiceName

   return $obj
}

 function Function2([PSObject] $obj)
{   
   Do something here with $obj
}


$myObj = CreateObject

Function2 $myObj

编辑 1
感谢@Frode 和@Matt.我不知道 'return' 语句也会返回其他结果.以下是否有效?

EDIT 1
Thanks @Frode and @Matt. I didn't know that 'return' statement would return other results also. Will the following work?

function CreateObject()
{      
    return New-Object -TypeName PSObject -Property @{     
       'TargetServer' = "ServerName"
       'ScriptPath' = "SomePath"
       'ServiceName' = "ServiceName"
      }
}

function Init()
{
   // Do something here

   $myObject = CreateObject()

  // Do something here with $myObject

  return $myObject
}

function Funcntion2([PSObject] $obj)
{
  //Do somthing with $obj
}

$obj = Init

Function2 $obj

推荐答案

来自 about_return 重要的是要知道

From about_return Its important to know that

在 Windows PowerShell 中,即使没有包含 Return 关键字的语句,每个语句的结果也会作为输出返回.

In Windows PowerShell, the results of each statement are returned as output, even without a statement that contains the Return keyword.

因此,正如 Frode 所说,您将获得一个字符串数组.您希望将您的对象作为一个整体而不是它的部分返回.如果您的函数的目的只是返回该自定义对象,那么您可以将该语句缩减为一行.

So as Frode said you are going to be getting a string array. You want to be returning your object as a whole and not its parts. If the purpose of your function is just to return that custom object then you can reduce that statement to a single line.

function CreateObject()
{      
    return New-Object -TypeName PSObject -Property @{     
        'TargetServer' = "ServerName"
        'ScriptPath' = "SomePath"
        'ServiceName' = "ServiceName"
    }
}

如果您至少有 PowerShell 3.0,那么您可以使用 [pscustomobject] 类型转换来完成同样的事情.

If you have at least PowerShell 3.0 then you can use the [pscustomobject] type cast to accomplish the same thing.

function CreateObject()
{      
    return [pscustomobject] @{     
        'TargetServer' = "ServerName"
        'ScriptPath' = "SomePath"
        'ServiceName' = "ServiceName"
    }
}

请注意,在这两种情况下,return 关键字都是可选的,但要知道它仍然用作函数的逻辑出口(直到该点的所有输出仍然返回).

Note that in both cases the return keyword is optional but know that it does still serve a purpose as a logical exit of a function (all output until that point is still returned).

如果您不需要将函数的结果保存在变量中,您也可以将其链接到下一个函数中.

If you don't need to save the results of the function in a variable you can also just chain that into your next function.

Function2 (CreateObject)

这篇关于Powershell:如何创建自定义对象并将其传递给 powershell 中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆