[英]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) . 通常,防火墙设置可能会导致此错误(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' ) . 如果在目标计算机上启用了防火墙,则可以检查“ Windows Management Instrumentation(WMI)”组中的防火墙规则(防火墙操作为“允许”)。
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 " . 另外,该错误表示命令“ 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 ) : 要缩小范围,您可以单独运行以下命令(只需将$($ SiteCode) , $ SiteServerName等一些变量更改为您环境中SiteCode和SiteServerName的实际值):
Get-WmiObject -Namespace root/SMS/site_$($SiteCode) -ComputerName $SiteServerName -Query "select * from SMS_ObjectContainerItem"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.