Forums on Intune, SCCM, and Windows 11

Welcome to the forums. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your topics and posts, as well as connect with other members through your own private inbox!

NEW Powershell WMI Script and SCCM

1

1bruce

Guest
I have the following powershell script:
#########################
$namespaces = @(gwmi -ns 'root\Microsoft\SqlServer' __NAMESPACE | ? {$_.name -match 'ComputerManagement' } | select name).Name

foreach($space in $namespaces) {
$wmi = $null
$instance = Get-WmiObject -Namespace ("root\Microsoft\SqlServer\" + $space) -Class ServerSettings
$wmi = Get-WmiObject -Namespace ("root\Microsoft\SqlServer\" + $space) -Class SqlService
if ($wmi -eq $null) {
Write-Output "not found"
}
else {

$wmi | Format-Table -Property Hostname, DisplayName, ServiceName, StartName, @{name="InstanceName"; exp={$instance.InstanceName}} -AutoSize | Out-String -Width 4096 | Out-File -FilePath "\\server_name\file_path.csv"

}
}
############################

After running on a single server, the result is correct (table format):

Hostname DisplayName ServiceName StartName InstanceName

When I distribute a script using SCCM "Software Library -> Scripts" for a collection with 16 clients, I only get a result for one of the clients (randomly selected).
I need to create a script so that the result obtained includes all clients in a given collection and is saved to the csv file.
 
Your script is likely overwriting each time it runs. This is really a powershell question and will have nothing to do with CM itself.
 
Overwrite posibilite is most likely there, can you try this one:



$namespaces = @(gwmi -ns 'root\Microsoft\SqlServer' __NAMESPACE | ? {$_.name -match 'ComputerManagement' } | select name).Name

$i=0
foreach($space in $namespaces) {

$wmi = $null
$instance = Get-WmiObject -Namespace ("root\Microsoft\SqlServer\" + $space) -Class ServerSettings
$wmi = Get-WmiObject -Namespace ("root\Microsoft\SqlServer\" + $space) -Class SqlService

if ($wmi -eq $null) {
Write-Output "not found"
}else{

$wmi | Format-Table -Property Hostname, DisplayName, ServiceName, StartName, `
@{name="InstanceName"; exp={$instance.InstanceName}} -AutoSize | `
Out-String -Width 4096 | `
Out-File -FilePath "\\server_name\file_path_$($i).csv"

}

$i++
}
 

Forum statistics

Threads
7,197
Messages
28,083
Members
18,356
Latest member
yemredizdar

Latest posts

Back
Top