2

次のコードがありますが、「無効な名前空間」エラーが常に発生します。正しい情報を入力していると確信しています。

これを行う別の方法がある場合は、それも受け入れられます。

Param(
    $SiteCode,
    $SourceFile,
    $Destination = "$env:USERPROFILE\DESKTOP\Computers.csv"
)


$Computers = Get-Content $SourceFile
$EmptyArray = @()

foreach($computer in $computers)
{
    $ResourceIDQuery = Get-WmiObject -Namespace "root\sms\site_$SiteCode" -Class SMS_R_SYSTEM -Filter "Name='$computer'"

    $CollectionQuery = Get-WmiObject -Namespace "root\sms\site_$SiteCode" -Class SMS_CollectionMember_a -filter "ResourceID='$($ResourceIDQuery.ResourceId)'"

    foreach($Item in $CollectionQuery)
    {
        $DObject = New-Object PSObject
            $Dobject | Add-Member -MemberType NoteProperty -Name "Computer" -Value $computer 
            $Dobject | Add-Member -MemberType NoteProperty -Name "ResID" -Value $($ResourceIDQuery.ResourceId)
            $Dobject | Add-Member -MemberType NoteProperty -Name "CollID" -Value $($Item.CollectionId)
            $Dobject | Add-Member -MemberType NoteProperty -Name "DirectOrNot" -Value $($Item.IsDirect)
        $EmptyArray += $Dobject
    }
}

$EmptyArray | ConvertTo-Csv -NoTypeInformation | Out-File $Destination
4

2 に答える 2

1

また、 SCCM コンソールのみがインストールされているステーションから SCCM に接続し、他のすべての SCCM コマンドレットが機能する場合は、Get-WmiObject を試してください。

Get-WmiObject -Namespace "root\sms\site_$SiteCode" ... 

エラーが発生します:

“Get-WmiObject : Invalid namespace …”

この状況では、パラメーター ComputerName を指定し、SCCM がインストールされているサーバーを指定する必要があります。

Get-WmiObject -ComputerName "SCCMserver" -Namespace "root\sms\site_$SiteCode" ... 

役に立てば幸いです。これで数分を無駄にします。

于 2014-11-14T13:08:06.833 に答える
1

各コンピューターに接続して情報を抽出する (遅い) のではなく、データベースから直接取得します....

[CmdletBinding()]
param ( 
        [string] $hosts = "",
        [string] $sccmsrv = "",
        [Parameter(Mandatory=$False,Position=3)]
        [string] $path = ""
    )

$usage =  "USAGE: List-AdvertCollMembershipSCCM.ps1 -sccmsrv SCCMSERVER -hosts 'host1 host2 host3' -path 'c:\temp\Outfile.csv'"

if ($host -and $sccmsrv){

Write-Host ""       
Write-Host -ForegroundColor Yellow "SCCM Server: $sccmsrv"
Write-Host -ForegroundColor Yellow "Looking at hosts: $hosts"


  #### Function for executing a SQL query with integrated authentication  
  function execSQLQuery ([string]$fSQLServer, [string]$db, [string]$query){  
    $objConnection = New-Object System.Data.SqlClient.SqlConnection  
    $objConnection.ConnectionString = "Server = $fSQLServer; Database = $db; trusted_connection=true;"  
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand $query, $objConnection  
    trap {Write-Host -ForegroundColor 'red' "($sqlsrv/$db not accessible)";continue}   
    $SqlCmd.Connection.Open()  

    if ($SqlCmd.Connection.State -ine 'Open') {  
       $SqlCmd.Connection.Close()  
       return  
    }  
    $dr = $SqlCmd.ExecuteReader()  

    #get the data  
    $dt = new-object "System.Data.DataTable"  
    $dt.Load($dr)  
    $SqlCmd.Connection.Close()  
    $dr.Close()  
    $dr.Dispose()  
    $objConnection.Close()  
    return $dt  
  }  

  # read the SCCM site name of the SCCM site server  
  $site = (gwmi -ComputerName $sccmsrv -Namespace root\sms -Class SMS_ProviderLocation).sitecode  

  # enumerating SQL server name for the given SCCM site server  
  $sccmCompquery = gwmi -q "Select distinct SiteSystem, Role, SiteCode FROM SMS_SiteSystemSummarizer where role = 'SMS SQL Server' and siteCode = '$site' ORDER BY SiteCode" -namespace "root\sms\site_$site" -ComputerName $sccmsrv  
  [string]$tmpstr = [regex]::Match($sccmCompquery.sitesystem, "\\\\\w+\\$")  
  $sccmSQLServer = $tmpstr.replace("\", "")  
  $objColl = @()  

  #### Collate the host list.  
  $hostlist = @($Input)  
  if ($hosts) {  
    if($hosts -imatch " "){  
       $hostsArr = @($hosts.split(" "))
       $hostlist += $hostsArr  
    }  
    else{  
       $hostlist += $hosts  
    }  
  }  

  # going through the list of hosts  
  foreach($srv in $hostlist){  
    $memberQuery = "SELECT dbo.v_FullCollectionMembership.Name AS 'Hostname', dbo.v_GS_SYSTEM.ResourceID, dbo.v_Collection.Name AS 'Collection Name', dbo.v_Collection.CollectionID, dbo.v_FullCollectionMembership.IsDirect "  
    $memberQuery += "FROM dbo.v_FullCollectionMembership INNER JOIN dbo.v_Collection ON dbo.v_FullCollectionMembership.CollectionID = dbo.v_Collection.CollectionID INNER JOIN dbo.v_GS_SYSTEM ON dbo.v_FullCollectionMembership.ResourceID = dbo.v_GS_SYSTEM.ResourceID "  
    $memberQuery += "WHERE (LOWER(dbo.v_FullCollectionMembership.Name) = LOWER('$srv'))"  

    # running sql query to enumerate list of collections the computer is member of  
    $membership = execSQLQuery $sccmSQLServer "SMS_$site" $memberQuery  

    # if we have a result, go through it and build an object collection with the computer name and the collection(s) it is member of  
    if($membership){  
       foreach($enumColl in $membership){

        $sObject = $enumColl | select Hostname, ResourceID, "Collection Name", CollectionID, IsDirect
        $objColl +=$sObject

       }  
    }  
  }


if ($objColl){
    if ($path){
        $objColl | ft -AutoSize
        Write-Host -ForegroundColor Yellow "Exporting to results to: $path"
        $objColl | Export-Csv $path -NoTypeInformation
    }
    else{
    $objColl | ft -AutoSize
    Write-Host -ForegroundColor Green "Use the -path argument in the command line to export output to csv to display"
    Write-Host -ForegroundColor Green "the 'IsDirect' Information"
    Write-Host ""
    }
}
Else {
    foreach ($Hostname in $hostlist){
    Write-Host ""
    Write-Host -ForegroundColor Yellow "The host $hostname is not a member of any collection"
    }
Write-Host -ForegroundColor Yellow "Check you have entered the correct hostname and try again"
}
}
else {

    Write-Host ""
    Write-Host -ForegroundColor Yellow $usage
}

実行:-

PS C:\> ListSCCMCollections.ps1 -sccmsrv SCCMSERVER -hosts host1,host2,host3 -path "c:\temp\Outfile.csv"

また

PS C:\> Get-Content hostlist.txt | ListSCCMCollections.ps1 -sccmsrv SCCMSERVER -path c:\temp\Outfile.csv

要求された情報を SQL から直接取得する:-

SELECT     dbo.v_FullCollectionMembership.Name AS 'Hostname', dbo.v_GS_SYSTEM.ResourceID, dbo.v_Collection.Name AS 'Collection Name', dbo.v_Collection.CollectionID, 
                      dbo.v_FullCollectionMembership.IsDirect
FROM         dbo.v_FullCollectionMembership INNER JOIN
                      dbo.v_Collection ON dbo.v_FullCollectionMembership.CollectionID = dbo.v_Collection.CollectionID INNER JOIN
                      dbo.v_GS_SYSTEM ON dbo.v_FullCollectionMembership.ResourceID = dbo.v_GS_SYSTEM.ResourceID
WHERE     (LOWER(dbo.v_FullCollectionMembership.Name) = LOWER('Hostname'))

このスクリプトは、SCCM データベースの任意の SQL クエリで使用できます。必要なのは、スクリプト内の SQL クエリを更新することだけです。つまり、$memberQuery 配列 (以下のようにクエリを数行にまたがる場合は、最後の行を除いて、各行の最後に必ずスペースを残してください)。

例えば; ライブ広告が割り当てられたクライアント コレクションをスクリプトで表示する場合は、$memberQuery 配列の SQL クエリを次のように変更します。

$memberQuery = "SELECT dbo.v_FullCollectionMembership.Name AS 'Hostname', dbo.v_GS_SYSTEM.ResourceID, dbo.v_Collection.Name AS 'Collection Name',dbo.v_Collection.CollectionID, dbo.v_FullCollectionMembership.IsDirect, dbo.v_Advertisement.AdvertisementID, dbo.v_Advertisement.AdvertisementName "  
$memberQuery += "FROM dbo.v_FullCollectionMembership INNER JOIN dbo.v_Collection ON dbo.v_FullCollectionMembership.CollectionID = dbo.v_Collection.CollectionID INNER JOIN dbo.v_GS_SYSTEM ON dbo.v_FullCollectionMembership.ResourceID = dbo.v_GS_SYSTEM.ResourceID INNER JOIN dbo.v_Advertisement ON dbo.v_Collection.CollectionID = dbo.v_Advertisement.CollectionID "  
$memberQuery += "WHERE (LOWER(dbo.v_FullCollectionMembership.Name) = LOWER('$srv'))"  

および $sObject 変数を次のように:-

$sObject = $enumColl | select Hostname, ResourceID, "Collection Name", CollectionID, IsDirect, AdvertisementID, AdvertisementName

ライブアドバイスを含むクライアントコレクションを表示するための完全なスクリプト (実行は前と同じ):-

[CmdletBinding()]
param ( 
        [string] $hosts = "",
        [string] $sccmsrv = "",
        [Parameter(Mandatory=$False,Position=3)]
        [string] $path = ""
    )

$usage =  "USAGE: List-AdvertCollMembershipSCCM.ps1 -sccmsrv SCCMSERVER -hosts 'host1 host2 host3' -path 'c:\temp\Outfile.csv'"

if ($host -and $sccmsrv){

Write-Host ""       
Write-Host -ForegroundColor Yellow "SCCM Server: $sccmsrv"
Write-Host -ForegroundColor Yellow "Looking at hosts: $hosts"


  #### Function for executing a SQL query with integrated authentication  
  function execSQLQuery ([string]$fSQLServer, [string]$db, [string]$query){  
    $objConnection = New-Object System.Data.SqlClient.SqlConnection  
    $objConnection.ConnectionString = "Server = $fSQLServer; Database = $db; trusted_connection=true;"  
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand $query, $objConnection  
    trap {Write-Host -ForegroundColor 'red' "($sqlsrv/$db not accessible)";continue}   
    $SqlCmd.Connection.Open()  

    if ($SqlCmd.Connection.State -ine 'Open') {  
       $SqlCmd.Connection.Close()  
       return  
    }  
    $dr = $SqlCmd.ExecuteReader()  

    #get the data  
    $dt = new-object "System.Data.DataTable"  
    $dt.Load($dr)  
    $SqlCmd.Connection.Close()  
    $dr.Close()  
    $dr.Dispose()  
    $objConnection.Close()  
    return $dt  
  }  

  # read the SCCM site name of the SCCM site server  
  $site = (gwmi -ComputerName $sccmsrv -Namespace root\sms -Class SMS_ProviderLocation).sitecode  

  # enumerating SQL server name for the given SCCM site server  
  $sccmCompquery = gwmi -q "Select distinct SiteSystem, Role, SiteCode FROM SMS_SiteSystemSummarizer where role = 'SMS SQL Server' and siteCode = '$site' ORDER BY SiteCode" -namespace "root\sms\site_$site" -ComputerName $sccmsrv  
  [string]$tmpstr = [regex]::Match($sccmCompquery.sitesystem, "\\\\\w+\\$")  
  $sccmSQLServer = $tmpstr.replace("\", "")  
  $objColl = @()  

  #### Collate the host list.  
  $hostlist = @($Input)  
  if ($hosts) {  
    if($hosts -imatch " "){  
       $hostsArr = @($hosts.split(" "))
       $hostlist += $hostsArr  
    }  
    else{  
       $hostlist += $hosts  
    }  
  }  

  # going through the list of hosts  
  foreach($srv in $hostlist){  
    $memberQuery = "SELECT dbo.v_FullCollectionMembership.Name AS 'Hostname', dbo.v_GS_SYSTEM.ResourceID, dbo.v_Collection.Name AS 'Collection Name',dbo.v_Collection.CollectionID, dbo.v_FullCollectionMembership.IsDirect, dbo.v_Advertisement.AdvertisementID, dbo.v_Advertisement.AdvertisementName "  
    $memberQuery += "FROM dbo.v_FullCollectionMembership INNER JOIN dbo.v_Collection ON dbo.v_FullCollectionMembership.CollectionID = dbo.v_Collection.CollectionID INNER JOIN dbo.v_GS_SYSTEM ON dbo.v_FullCollectionMembership.ResourceID = dbo.v_GS_SYSTEM.ResourceID INNER JOIN dbo.v_Advertisement ON dbo.v_Collection.CollectionID = dbo.v_Advertisement.CollectionID "  
    $memberQuery += "WHERE (LOWER(dbo.v_FullCollectionMembership.Name) = LOWER('$srv'))"  

    # running sql query to enumerate list of collections the computer is member of  
    $membership = execSQLQuery $sccmSQLServer "SMS_$site" $memberQuery  

    # if we have a result, go through it and build an object collection with the computer name and the collection(s) it is member of  
    if($membership){  
       foreach($enumColl in $membership){

        $sObject = $enumColl | select Hostname, ResourceID, "Collection Name", CollectionID, IsDirect, AdvertisementID, AdvertisementName
        $objColl +=$sObject

       }  
    }  
  }
if ($objColl){
    if ($path){
        $objColl | ft -AutoSize
        Write-Host -ForegroundColor Yellow "Exporting to results to: $path"
        $objColl | Export-Csv $path -NoTypeInformation
    }
    else{
    $objColl | ft -AutoSize
    Write-Host -ForegroundColor Green "Use the -path argument in the command line to export output to csv to display"
    Write-Host -ForegroundColor Green "the header 'AdvertisementName'"
    Write-Host ""
    }
}
Else {
    foreach ($Hostname in $hostlist){
    Write-Host ""
    Write-Host -ForegroundColor Yellow "The host $hostname is not a member of any collection with live advertisements"
    }
Write-Host -ForegroundColor Yellow "Check you have entered the correct hostname and try again"
}
}
else {

    Write-Host ""
    Write-Host -ForegroundColor Yellow $usage
}
于 2013-07-05T03:13:24.880 に答える