简体   繁体   中英

SCCM Powershell SCRIPT to get object location from folder hierarchy

I used below code

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

but got the below error - do am i providing wrong parameter

在此处输入图片说明

Generally , firewall setting may cause this error (0x800706BA) .

If the firewall is enabled on destination computers , you may check the firewall rules in group "windows management instrumentation (WMI)" (The firewall action is 'Allow' ) .

If it is not enabled , you can manually enable them or run following command :

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

In addition , that error implies the error came with command " Get-WmiObject " . To narrow it down you may run command below separately (Just change few variables such as $($SiteCode) , $SiteServerName to the real value of SiteCode and SiteServerName in your environment ) :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM