Return PowerShell 3.0 中函数的数组
Return an array from a function in PowerShell 3.0
我正在尝试让 PowerShell 3.0 return 来自函数的数组。不幸的是,这没有很好的记录,因为我在过去两天里一直在搜索 Google 来寻找这方面的例子。我差点用 C# 重写整个脚本并收工了。
该脚本检查包含在变量中的一组 URLS。有问题的函数从一个数组中获取 URL 的列表,并循环遍历该数组,将 HTTP 状态代码添加到一个新数组中。该函数执行上述所有操作,但它不 return 数组。这是有问题的功能:
function URLCheck ($URLStatusCode)
{
foreach($uri in $URLStatusCode )
{
$result = @()
$time = try
{
$request = $null
## Check response time of requested URI.
$result1 = Measure-Command { $request = Invoke-WebRequest -Uri $uri}
$result1.TotalMilliseconds
}
catch
{
<# If request generates exception such as 401, 404 302 etc,
pull status code and add to array that will be emailed to users #>
$request = $_.exception.response
$time = -1
}
$result += [PSCustomObject] @{
Time = Get-Date;
Uri = $uri;
StatusCode = [int] $request.StatusCode;
StatusDescription = $request.StatusDescription;
ResponseLength = $request.RawContentLength;
TimeTaken = $time;
}
}
return $result
}
我这样称呼:
URLCheck $LinuxNonProdURLList
$result
执行后我还打印了 $result 的内容,我注意到它是空的。但是,如果我将 return 语句放在 foreach 循环中,它会将信息发送到控制台。
如有任何帮助,我们将不胜感激。
经过更多的故障排除后,我发现数组 $result 只是该函数的本地数组。我在 foreach 循环之外声明了数组并修复了错误。这是更新后的代码:
function URLCheck ($URLStatusCode)
{
$result = @()
foreach($uri in $URLStatusCode )
{
$time = try
{
$request = $null
## Check response time of requested URI.
$result1 = Measure-Command { $request = Invoke-WebRequest -Uri $uri}
$result1.TotalMilliseconds
}
catch
{
<# If request generates exception such as 401, 404 302 etc,
pull status code and add to array that will be emailed to users #>
$request = $_.exception.response
$time = -1
}
$result += [PSCustomObject] @{
Time = Get-Date;
Uri = $uri;
StatusCode = [int] $request.StatusCode;
StatusDescription = $request.StatusDescription;
ResponseLength = $request.RawContentLength;
TimeTaken = $time;
}
}
return $result
}
我正在尝试让 PowerShell 3.0 return 来自函数的数组。不幸的是,这没有很好的记录,因为我在过去两天里一直在搜索 Google 来寻找这方面的例子。我差点用 C# 重写整个脚本并收工了。
该脚本检查包含在变量中的一组 URLS。有问题的函数从一个数组中获取 URL 的列表,并循环遍历该数组,将 HTTP 状态代码添加到一个新数组中。该函数执行上述所有操作,但它不 return 数组。这是有问题的功能:
function URLCheck ($URLStatusCode)
{
foreach($uri in $URLStatusCode )
{
$result = @()
$time = try
{
$request = $null
## Check response time of requested URI.
$result1 = Measure-Command { $request = Invoke-WebRequest -Uri $uri}
$result1.TotalMilliseconds
}
catch
{
<# If request generates exception such as 401, 404 302 etc,
pull status code and add to array that will be emailed to users #>
$request = $_.exception.response
$time = -1
}
$result += [PSCustomObject] @{
Time = Get-Date;
Uri = $uri;
StatusCode = [int] $request.StatusCode;
StatusDescription = $request.StatusDescription;
ResponseLength = $request.RawContentLength;
TimeTaken = $time;
}
}
return $result
}
我这样称呼:
URLCheck $LinuxNonProdURLList
$result
执行后我还打印了 $result 的内容,我注意到它是空的。但是,如果我将 return 语句放在 foreach 循环中,它会将信息发送到控制台。
如有任何帮助,我们将不胜感激。
经过更多的故障排除后,我发现数组 $result 只是该函数的本地数组。我在 foreach 循环之外声明了数组并修复了错误。这是更新后的代码:
function URLCheck ($URLStatusCode)
{
$result = @()
foreach($uri in $URLStatusCode )
{
$time = try
{
$request = $null
## Check response time of requested URI.
$result1 = Measure-Command { $request = Invoke-WebRequest -Uri $uri}
$result1.TotalMilliseconds
}
catch
{
<# If request generates exception such as 401, 404 302 etc,
pull status code and add to array that will be emailed to users #>
$request = $_.exception.response
$time = -1
}
$result += [PSCustomObject] @{
Time = Get-Date;
Uri = $uri;
StatusCode = [int] $request.StatusCode;
StatusDescription = $request.StatusDescription;
ResponseLength = $request.RawContentLength;
TimeTaken = $time;
}
}
return $result
}