Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 不使用中间变量从函数调用访问字段_Powershell - Fatal编程技术网

Powershell 不使用中间变量从函数调用访问字段

Powershell 不使用中间变量从函数调用访问字段,powershell,Powershell,假设我有以下powershell代码: function GetImageInfo() { [OutputType([ImageInfo])] [ImageInfo] $imageInfo = [ImageInfo]::new() $imageInfo.Owner = "Me" $imageInfo.PrimaryTechnology = "jpeg" $imageInfo.OperatingSystem = "Windows" $imageIn

假设我有以下powershell代码:

function GetImageInfo()
{
    [OutputType([ImageInfo])]

    [ImageInfo] $imageInfo = [ImageInfo]::new()
    $imageInfo.Owner = "Me"
    $imageInfo.PrimaryTechnology = "jpeg"
    $imageInfo.OperatingSystem = "Windows"
    $imageInfo.OperatingSystemVersion = "10"    

    return $imageInfo   
}

class ImageInfo
{
    [string] $Owner
    [string] $PrimaryTechnology
    [string] $OperatingSystem
    [string] $OperatingSystemVersion
    [string[]] $OptionalQualifiers
}
现在我想调用
GetImageInfo
并将
Owner
的值放入一个变量中

我可以这样做:

$info = GetImageInfo
$owner = $imageInfo.Owner
但我感到惊讶的是,这不起作用:

# Throws an error
$owner = GetImageInfo.Owner
对于我现在正在做的事情,较短的选项会很好


有没有办法直接从PowerShell中的方法调用获取字段?

您需要将函数调用包装在括号中。这:

$owner=(GetImageInfo).owner

(GetImageInfo).所有者
@beatcracker-就是这样!作为答案后,我会接受。(谢谢!)再次感谢。这么简单,但这件事让我难堪了一段时间!