SCCM Powershell SCRIPT从文件夹层次结构中获取对象位置

时间:2016-10-10 12:16:44

标签: powershell configurationmanager sccm

我使用下面的代码

Function Get-SCCMObjectLocation {

    param(
        [Parameter(Mandatory=$true,
                ValueFromPipeline=$true,
                ValueFromPipelineByPropertyName=$true, 
                Position=0)][string]$SMSId,
        [string]$SiteCode = (Get-CMSite).SiteCode,
        [string]$SiteServerName = (Get-CMSite).ServerName)

    #Find the container directly containing the item
    $ContainerItem = Get-WmiObject -Namespace root/SMS/site_$($SiteCode) -ComputerName $SiteServerName -Query "select * from SMS_ObjectContainerItem where InstanceKey = '$($SMSId)'"
    If (!$ContainerItem) {
        $ObjectName = Get-WmiObject -Namespace root/SMS/site_$($SiteCode) -ComputerName $SiteServerName -Query "select * from SMS_ObjectName where ObjectKey = '$($SMSId)'"
        If (!$ObjectName) {
            Write-Warning "No object or containers found for $SMSId"
            break;
        }
        Else
        {
            Return "root\$(($ObjectName).Name)"
            break;
        }
    }

    If ($ContainerItem -is [array]) {
        Write-Warning "Multiple objects with ID: $SMSId"
        Foreach ($Item In $ContainerItem) {
            $tempOutputString = Get-SCCMContainerHierarchy -ContainerNodeId $Item.ContainerNodeID -ObjectType $Item.ObjectType -ObjectTypename $Item.ObjectTypeName -SiteCode $SiteCode -SiteServerName $SiteServerName
            $OutputString = "$OutputString`nroot\$tempOutputString"
        }
        Return "$OutputString"
    }
    Else {
        #One object found
        $OutputString = Get-SCCMContainerHierarchy -ContainerNodeId ($ContainerItem).ContainerNodeID -SiteCode $SiteCode -SiteServerName $SiteServerName
        Return "root\$OutputString"
    }


}

Function Get-SCCMContainerHierarchy {
    param(
        [Parameter(Mandatory=$true,
                Position=0)][string]$ContainerNodeId,
        [Parameter(Mandatory=$true,
                Position=1)][string]$SiteCode = (Get-CMSite).SiteCode,
        [Parameter(Mandatory=$true,
                Position=2)][string]$SiteServerName = (Get-CMSite).ServerName,
        [Parameter(Mandatory=$false)]$ObjectType = ($ContainerItem).ObjectType,
        [Parameter(Mandatory=$false)]$ObjectTypeName = ($ContainerItem).ObjectTypeName)

    Switch ($ObjectType) {
        2       {$ObjectTypeText = $ObjectTypeName; $ObjectName = (Get-CMPackage -ID $SMSId).Name} # Package
        14      {$ObjectTypeText = $ObjectTypeName; $ObjectName = (Get-CMOperatingSystemInstaller -ID $SMSId).Name} # OS Install Package
        18      {$ObjectTypeText = $ObjectTypeName; $ObjectName = (Get-CMOperatingSystemImage -ID $SMSId).Name} # OS Image
        20      {$ObjectTypeText = $ObjectTypeName; $ObjectName = (Get-CMTaskSequence -ID $SMSId).Name} # Task Sequence
        23      {$ObjectTypeText = $ObjectTypeName; $ObjectName = (Get-CMDriverPackage -ID $SMSId).Name} # Driver Package
        19      {$ObjectTypeText = $ObjectTypeName; $ObjectName = (Get-CMBootImage -ID $SMSId).Name} # Boot Image
        5000    {$ObjectTypeText = $ObjectTypeName; $ObjectName = (Get-CMDeviceCollection -Id $SMSId).Name} # Device Collection
        5001    {$ObjectTypeText = $ObjectTypeName; $ObjectName = (Get-CMUserCollection -Id $SMSId).Name} # User Collection
        default {$ObjectTypeText = "unknown object type: '$($ObjectTypeName)' = $($ObjectType)"; $ObjectName = "unknown object name ($SMSId)"}
    }

    $OutputString = "$ObjectName `t[$ObjectTypeText]"
    #ContainerNodeID of 0 is the root
    While ($ContainerNodeId -ne 0) {
        #Find details of that container
        $ContainerNode = Get-WmiObject -Namespace root/SMS/site_$($SiteCode) -ComputerName $SiteServerName -Query "select * from SMS_ObjectContainerNode where ContainerNodeID = '$($ContainerNodeId)'"
        $ContainerName = ($ContainerNode).Name
        $ContainerNodeId = ($ContainerNode).ParentContainerNodeID
        $OutputString = "$ContainerName\$OutputString"
    }
    Return $OutputString
}
Get-SCCMObjectLocation -SMSId "PS1022EB" -SiteCode PS1 -SiteServerName xxyyzz.com

但得到以下错误 - 我是否提供了错误的参数

enter image description here

1 个答案:

答案 0 :(得分:1)

通常,防火墙设置可能会导致此错误(0x800706BA)。

如果在目标计算机上启用了防火墙,则可以在“Windows管理工具(WMI)”组中检查防火墙规则(防火墙操作为“允许”)。

如果未启用,您可以手动启用它们或运行以下命令:

netsh advfirewall firewall set rule group="Windows Management Instrumentation (WMI)" new enable=yes

此外,该错误意味着命令“ Get-WmiObject ”出现错误。要缩小范围,您可以单独运行以下命令(只需将 $($ SiteCode) $ SiteServerName 等少数变量更改为 SiteCode的实际值 SiteServerName

Get-WmiObject -Namespace root/SMS/site_$($SiteCode) -ComputerName $SiteServerName -Query "select * from SMS_ObjectContainerItem"