【发布时间】:2017-07-21 17:44:41
【问题描述】:
我在 PowerShell 中编写了一个自动化脚本,它每天早上都会给我一份关于我们的 azure 订阅(虚拟机、自动化作业、警报等)的报告。 现在,我正在添加一个新功能,该功能在我的计算机上运行良好,但在从自动化运行时失败,我不知道如何使它工作。
在我的电脑上运行良好(VS code / PS 5.1 / PS 4.0)
$omsRGname = "xxx"
$omsWorkspaceName = "xxx"
$omsQueryThreat = 'Type=ProtectionStatus ThreatStatusRank!=150 ThreatStatusRank!=470 | select Computer,Threat,ThreatStatus'
$ArrayThreat = New-Object System.Collections.ArrayList
function OmsRequest {
Param(
[parameter(Position = 0, Mandatory = $true)]
$omsRG,
[parameter(Position = 1, Mandatory = $true)]
$omsWorkspace,
[parameter(Position = 2, Mandatory = $true)]
$omsQuery
)
Process {
Import-Module AzureRm.OperationalInsights
$error.clear()
$Result.clear
$script:Result = Get-AzureRmOperationalInsightsSearchResults -ResourceGroupName $omsRG -WorkspaceName $omsWorkspace -Query $omsQuery
$reqIdParts = $Result.Id.Split("/")
$reqId = $reqIdParts[$reqIdParts.Count - 1]
$wait = Get-Date
while ($Result.Metadata.Status -eq "Pending" -and $error.Count -eq 0) {
$Result = Get-AzureRmOperationalInsightsSearchResults -WorkspaceName $omsWorkspace -ResourceGroupName $omsRG -Id $reqId
#debug
$elapsedTime = $(get-date) - $wait
Write-Output "Elapsed: $elapsedTime -- Status: $($Result.Metadata.Status)"
Write-Output "Count: $($Result.Count)"
}
}
}
OmsRequest $omsRGname $omsWorkspaceName $omsQueryThreat
#debug
Write-Output "VALUEOUT:" $Result.Value
#
$OMSComputers = $Result.Value | ConvertFrom-Json
if ($OMSComputers) {
foreach ( $ThreatDetails in $OMSComputers) {
<#write "$(get-date($ThreatDetails.__metadata.TimeGenerated) -format G) - $($ThreatDetails.Computer) - $($ThreatDetails.Threat) - $($ThreatDetails.ThreatStatus)"#>
$ThDetails = [PSCustomObject] @{
Date = (get-date($ThreatDetails.__metadata.TimeGenerated) -format G);
Computer = $ThreatDetails.Computer;
Threat = $ThreatDetails.Threat;
ThreatStatus = $ThreatDetails.ThreatStatus;
}
$ArrayThreat.Add($ThDetails) |Out-Null
}
}
else {Write-Output "OK"}
$ArrayThreat
从我的本地计算机
Elapsed: 00:00:00.4270000 -- Status: Successful
Count: 1
Date Computer Threat ThreatStatus
---- -------- ------ ------------
29/06/2017 12:55:37 xxx.local Virus:ALisp/Bursted.DT Quarantined
29/06/2017 11:48:28 xxx.local Virus:ALisp/Bursted.DT Quarantined
29/06/2017 10:55:37 xxx.local Virus:ALisp/Bursted.DT Quarantined
29/06/2017 09:55:38 xxx.local Virus:ALisp/Bursted.DT Quarantined
29/06/2017 08:48:28 xxx.local Virus:ALisp/Bursted.DT Quarantined
29/06/2017 08:48:28 xxx.local Virus:ALisp/Bursted.DT Quarantined
29/06/2017 07:55:37 xxx.local Virus:ALisp/Bursted.DT Quarantined
29/06/2017 07:55:37 xxx.local Virus:ALisp/Bursted.DT Quarantined
29/06/2017 06:48:28 xxx.local Virus:ALisp/Bursted.DT Quarantined
29/06/2017 06:48:28 xxx.local Virus:ALisp/Bursted.DT Quarantined
当我从自动化运行我的代码时,我得到了这个错误
ConvertFrom-Json : The input object cannot be bound to any parameters for the command either because the command does
not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
At line:164 char:33
+ $OMSComputers = $Result.Value | ConvertFrom-Json
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: ({
"Computer"...ng": {}
}
}:JObject) [ConvertFrom-Json], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
好吧,我的错误就在这里
$OMSComputers = $Result.Value | ConvertFrom-Json
当我查看它的值时,我得到了这个(只是从 $Result.Value 给出的列表中获取了一个属性)
VALUEOUT:
Name : Computer
Type : Property
HasValues : True
First : {}
Last : {}
Count : 1
Parent : {Computer, Threat, ThreatStatus, __metadata}
Root : {Computer, Threat, ThreatStatus, __metadata}
Next : {}
Previous :
Path : Computer
LineNumber : 0
LinePosition : 0
IsReadOnly : False
AllowNew : True
AllowEdit : True
AllowRemove : True
SupportsChangeNotification : True
SupportsSearching : False
SupportsSorting : False
IsSorted : False
SortProperty :
SortDirection : Ascending
IsFixedSize : False
SyncRoot : System.Object
IsSynchronized : False
看到这条线
Keys : {Computer, Threat, ThreatStatus, __metadata}
我尝试将其解析为哈希表,但没有结果。
有没有人有其他创造性的方法来解决这个问题? 谢谢
编辑: 我只是在这里完成了我的职能。但是在完整的脚本中,我连接良好并且 PS 模块已经存在(如果不是功能失败告诉我,则无法找到 RG,但尽管如此,我单击并从您的链接添加模块)。 如果我查看 $result 内容,我会得到查询结果。
{
"Computer": "xxx.local",
"Threat": "Virus:ALisp/Bursted.DT",
"ThreatStatus": "Quarantined",
"__metadata": {
"Type": "ProtectionStatus",
"TimeGenerated": "2017-06-29T10:55:37.77Z",
"highlighting": {}
}
}
当我询问它的类型时
Newtonsoft.Json.Linq.JObject
所以我导入 Newtonsoft.Json 模块并尝试从这种 Json 转换,但它也失败了。
看起来我的 OMS 查询正在运行(我在 $Result 中有一个结果),但是当自动化尝试使用“ConvertFrom-Json”进行转换时,它失败了。
#debug
Write-Output "VALUEOUT:"
Write-Output "Get-variable RESULT"
Get-Variable Result -ValueOnly |format-list
$OMSComputers = $Result.Value | ConvertFrom-Json
输出:
VALUEOUT:
Get-variable RESULT
Id : subscriptions/xxx/providers/Microsoft.Operatio
nalInsights/workspaces/xxx/search/xxx|10.1.0.27|2017-07-05T14-33-52Z
Metadata : Microsoft.Azure.Commands.OperationalInsights.Models.PSSearchMetadata
Error :
Value : {"Computer": "xxx.local" "Threat": "Virus:ALisp/Bursted.DT" "ThreatStatus": "Quarantined"
"__metadata": {
"Type": "ProtectionStatus",
"TimeGenerated": "2017-06-29T10:55:37.77Z",
"highlighting": {}
}, "Computer": "xxx.local" "Threat": "Virus:ALisp/Bursted.DT" "ThreatStatus": "Quarantined"
"__metadata": {
"Type": "ProtectionStatus",
"TimeGenerated": "2017-06-29T09:48:28.42Z",
"highlighting": {}
}, "Computer": "xxx.local" "Threat": "Virus:ALisp/Bursted.DT" "ThreatStatus": "Quarantined"
"__metadata": {
"Type": "ProtectionStatus",
"TimeGenerated": "2017-06-29T08:55:37.757Z",
"highlighting": {}
}, "Computer": "xxx.local" "Threat": "Virus:ALisp/Bursted.DT" "ThreatStatus": "Quarantined"
"__metadata": {
"Type": "ProtectionStatus",
"TimeGenerated": "2017-06-29T07:55:38.327Z",
"highlighting": {}
}...}
ConvertFrom-Json : The input object cannot be bound to any parameters for the command either because the command does
not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
At line:65 char:33
+ $OMSComputers = $Result.Value | ConvertFrom-Json
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: ({
"Computer"...ng": {}
}
}:JObject) [ConvertFrom-Json], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
我不明白为什么 ConvertFrom-Json 失败:/
【问题讨论】:
标签: azure visual-studio-code azure-powershell azure-automation