我正在尝试运行在Microsoft网站https://gallery.technet.microsoft.com/scriptcenter/PowerShell-script-to-find-6fc15ecb/view/Discussions#content
上找到的脚本我不断收到错误:
? : You cannot call a method on a null-valued expression.
At C:\serviceaccounts\report-service-accounts.ps1:57 char:36
+ ... ceList = $data | ? { $_.StartName.toUpper().contains($currentDomain)}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Where-Object], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull,Microsoft.PowerShell.Commands.WhereObjectCommand
这是我认为在尝试了几步解决我的问题后,错误在修改后的版本中引用的部分:(很抱歉,第一次在这里发布,并且无法正确地发布整个脚本)
function processCompletedJobs(){
$jobs = Get-Job -State Completed
foreach( $job in $jobs ) {
$data = Receive-Job $job
Remove-Job $job
if ( $data.GetType() -eq [Object[]] ){
$serviceList = $data | ? { $_.StartName.toUpper().contains($currentDomain)}
foreach( $service in $serviceList ){
$account = $service.StartName
$occurance = "`"$($service.Name)`" service on $($service.SystemName)"
if ( $script:serviceAccounts.Contains( $account ) ){
$script:serviceAccounts.Item($account) += $occurance
}
else {
$script:serviceAccounts.Add( $account, @( $occurance ) )
}
}
}
elseif ( $data.GetType() -eq [String] ) {
$script:warnings += $data
Write-warning $data
}
}