简体   繁体   English

在 Powershell 中获取特定 SCCM 设备集合的位置

[英]Get location of specific SCCM device collection in Powershell

I am writing a script to export the names of all computer in a device collection to a txt file.我正在编写一个脚本,将设备集合中所有计算机的名称导出到一个 txt 文件。 My script works as expected but I would like to preserve the folder structure in the exported file structure.我的脚本按预期工作,但我想在导出的文件结构中保留文件夹结构。 For this I need to get the location of the Device Collection.为此,我需要获取设备集合的位置。

My Question: Is there a way to get the location of a SCCM Device Collection in PowerShell?我的问题:有没有办法在 PowerShell 中获取 SCCM 设备集合的位置?

I've stumbled across a few posts like this and this that use WMI and WQL for this, but I wasn't able to get those working in my script and I would like to do everything in PowerShell whenever possible.我偶然发现了一些像这样这样使用 WMI 和 WQL 的帖子,但我无法让它们在我的脚本中工作,我想尽可能在 PowerShell 中完成所有工作。

$collections = (Get-CMDeviceCollection | Select -ExpandProperty "Name")

$totalCollections = $collections.length

"Number of Collections: $totalCollections"

$i = 0
foreach($name in $collections){
    ForEach-Object -Process {
        $i++
        "Writing File $i of $totalCollections"
        $SanitizedName = $name -replace '/','(slash)' -replace '\\','(backslash)' -replace ':','(colon)' -replace '\*','(asterisk)' -replace '\?','(questionmark)' -replace '"','(quote)' -replace '<','(less)' -replace '>','(more)' -replace '\|','(pipe)'
        $file = New-Item -Path "C:\Temp\exporte\$SanitizedName.txt"
        Add-Content -Path $file.FullName -Value (Get-CMCollectionMember -CollectionName $name | Select -ExpandProperty "Name")
    }
}

I would like to expand this code so that the txt files are placed in the corresponding subfolder analog to the SCCM file structure.我想扩展此代码,以便将 txt 文件放置在类似于 SCCM 文件结构的相应子文件夹中。 Eg rootFolder/rooms/例如根文件夹/房间/

I was using this module until now but wasn't able to find anything that gives me back the specific location of a collection.直到现在我一直在使用这个模块,但没能找到任何能让我返回集合的具体位置的东西。

Thanks in advance提前致谢

I wasn't able to find a way to do this in plain PowerShell and the SCCM Module.我无法在普通 PowerShell 和 SCCM 模块中找到执行此操作的方法。 In the end I did it like @FoxDeploy suggested.最后我按照@FoxDeploy 的建议做了。 I made a SQL query select for each collection (performance isn't an issue in my case) on our SCCM database to get the folder path.我在我们的 SCCM 数据库上为每个集合(在我的情况下性能不是问题)进行了 SQL 查询 select 以获取文件夹路径。 I then used this to place the export file in the appropriate place.然后我用它把导出文件放在适当的地方。

This is my working example with some confidential lines removed这是我的工作示例,删除了一些机密行

## Parameter ##
$exportLocation = [removed]
$sqlServer = [removed]
$db = [removed]

$query = "SELECT [ObjectPath] FROM [removed].[v_Collections] WHERE CollectionName ="

$SiteCode = [removed] # Site code 
$ProviderMachineName = [removed] # SMS Provider machine name

# Customizations
$initParams = @{}

# Import the ConfigurationManager.psd1 module 
if((Get-Module ConfigurationManager) -eq $null) {
    Import-Module [removed]\..\ConfigurationManager.psd1" @initParams 
}

# Connect to the site's drive if it is not already present
if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {
    New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName @initParams
}

Set-Location "$($SiteCode):\" @initParams

# get all collections and save them to an array
$collections = (Get-CMDeviceCollection | Select -ExpandProperty "Name")

# total number of collections
$totalCollections = $collections.length

# output to console
"Number of Collections: $totalCollections"

# empty output directory
Set-Location [removed]
Remove-Item $exportLocation\* -Recurse -Force
Set-Location [removed]

# loop through all collections
$i = 0
foreach($name in $collections){
    ForEach-Object -Process {
        # print progress
        $i++
        "Writing File $i of $totalCollections"
        # remove all characters, that aren't compatible with the windows file naming scheme (/\:*?"<>|)
        $SanitizedName = $name -replace '/','(slash)' -replace '\\','(backslash)' -replace ':','(colon)' -replace '\*','(asterisk)' -replace '\?','(questionmark)' -replace '"','(quote)' -replace '<','(less)' -replace '>','(more)' -replace '\|','(pipe)'
        # get members of collection
        $collectionMembers = (Get-CMCollectionMember -CollectionName $name | Select -ExpandProperty "Name")
        # write to file
        Set-Location [removed]
        $path = (Invoke-Sqlcmd -ServerInstance $sqlServer -Database $db -Query "$query '$collection'").Item("ObjectPath")
        New-Item -ItemType Directory -Force -Path "$exportLocation$path"
        $file = New-Item -Path "$exportLocation$path\$SanitizedName.txt"
        Add-Content -Path $file.FullName -Value $collectionMembers
        Set-Location [removed]
    }
}

hope this helps someone.希望这对某人有帮助。 Thanks @FoxDeploy谢谢@FoxDeploy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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