SCCM 2007 Powershell Scripts

From Slothx.net

Jump to: navigation, search

Contents

Distribution Point Cleanup

# ---------------------------------------------------------------------------------------
# Clean DP - Remove all orphaned packages from distribution points.
# ---------------------------------------------------------------------------------------

function fnClean_DP($site_server,$site_code,$clean)
{
    Write-Host "Reading Resources..."
    write-host ""

    $dp_data_list = @()

    #1 Read in all system resources -------------------------------------------------------------

    $resource_hashtable = @{}

    $query = "select * from sms_sci_sysresuse where SiteCode = '" + $site_code + "' and RoleName = 'SMS Distribution Point'"

    $resource_list = Get-WmiObject -ComputerName $site_server -Namespace ("root\SMS\Site_" + $site_code) -query $query

    foreach ($resource in $resource_list)
    {
        $path = $resource.NetworkOSPath + "\"
        if ($resource_hashtable.ContainsKey($path) -eq $false)
        {
            $resource_hashtable.Add($path,$path)
        }
    }

    $remove_list = @{}

    #2 Read in all distribution points

    $query = "select * from SMS_DistributionPoint where SiteCode = '" + $site_code + "'"
    $dp_list = Get-WmiObject -ComputerName $site_server -Namespace ("root\SMS\Site_" + $site_code) -query $query
 
    $resource_hashtable 


    foreach ($dp in $dp_list) 
    {
        $regex = [regex] '(?sm)Display=(?<path>[^"]*)'
        $path = $regex.Match($dp.ServerNALPath).Groups['path'].Value;

        $regex = [regex] '(?sm)Display=\\\\(?<server>[^\\]*)'
        $server = $regex.Match($dp.ServerNALPath).Groups['server'].Value;

        if ($resource_hashtable.ContainsKey($path) -eq $false)
        {            

            if ($remove_list.ContainsKey($server) -eq $false)
            {
                $remove_list.Add($server,$server)
            }

            if ($clean -eq $false)
            {
                Write-Host "Remove Target: " $dp.ServerNALPath
            }

            if ($clean -eq $true)
            {
                Write-Host "Removed     : " $dp.PackageID $dp.ServerNALPath
                $dp | Remove-WmiObject
            }
        }

    }

    #  Build up SQL String to data validation..

    foreach ($data in $remove_list.GetEnumerator())
    {
        $item = $data.Value
        $sql_string += "select * from v_SystemResourceList where ServerName = '$item'`r`n"        
    }

    return $remove_list,$sql_string
}

clear
$site_code = "XXX"
$site_server = "Server1"

$return_data = fnClean_DP $site_server $site_code $true

$return_data[1] | clip

Advert Info

$site_code = "S01"
$computer = "localhost"
$query = "Select * from SMS_Advertisement"
$data = Get-WmiObject -ComputerName $computer -Namespace ("root\SMS\Site_" + $site_code) -query $query
$data | select-Object AdvertisementID, AdvertisementName, PackageID, PackageName,ProgramName | format-table

PSEXEC Cleanup

# List Service
get-service -computername LEVEL2 | Where-Object {$_.Name -contains "PSEXESVC"}

# Stop Service
$service = Get-WmiObject -ComputerName LEVEL2 -Class Win32_Service `-Filter "Name='PSEXESVC'"
$service.stopservice()

# Stop Service - SC command
sc \\LEVEL2 stop PSEXESVC

# Delete Service
sc \\LEVEL2 delete PSEXESVC

General

SQL query using powershell

$connection_string = "server=.\SQLEXPRESS;database=SMS_P01;Integrated Security=sspi"

$sqlConnection = new-object System.Data.SqlClient.SqlConnection $connection_string
$sqlConnection.Open()

$sql = "select * from v_Package"
$sql = "DBCC SQLPERF(logspace)"

$adapter = new-object data.sqlclient.sqldataadapter($sql, $sqlConnection)

$set = new-object data.dataset
$adapter.fill($set)
$global:table = new-object data.datatable
$global:table = $set.tables[0] 

$global:table

Performance Monitoring

Get all Performance Monitor Categories

get-counter -listset * | Select-Object CounterSetName

Powershell Performance Counters

Get-WmiObject -List | Where-Object {$_.name -match 'perf'}
Get-WmiObject -List | Where-Object {$_.name -match 'perf'} | Select-Object Name
Get-WmiObject -List | Where-Object {$_.name -match 'perf' -AND $_.name -notmatch 'raw'}
Get-WmiObject -List | Where-Object {$_.name -match 'perf' -AND $_.name -notmatch 'raw'} | Select-Object Name

SMS Inboxes File Count

Get-WmiObject -Class Win32_PerfFormattedData_SMSINBOXMONITOR_SMSInbox | Select-Object Name, FileCurrentCount, PSComputerName
Get-WmiObject -Class Win32_PerfFormattedData_SMSINBOXMONITOR_SMSInbox -ComputerName LEVEL2 | Select-Object Name, FileCurrentCount, PSComputerName
$servers = @("LEVEL1","LEVEL2")

foreach ($computer_name in $servers)
{
 write-host ""
 write-host "Server: $computer_name"
 write-host ""
 Get-WmiObject -Class Win32_PerfFormattedData_SMSINBOXMONITOR_SMSInbox -ComputerName $computer_name | Select-Object Name, FileCurrentCount, PSComputerName
}

Site Information

# SMS_Site Server WMI Class
# http://msdn.microsoft.com/en-au/library/cc146620.aspx
#
# Type 1 - Secondary
# Type 2 - Primary
#
$site_code = "S01"
$computer = "localhost"
$query = "Select * from SMS_Site"
$site_list = Get-WmiObject -ComputerName $computer -Namespace ("root\SMS\Site_" + $site_code) -query $query
$site_list | Select-Object SiteCode, Type | Format-Table -AutoSize

UnInstall

Uninstall Key

$list = Get-ChildItem HKLM:\SOFTWARE\Wow6432Node\Microsoft\Updates
foreach ($App in $list)
{
    (Get-ItemProperty registry::$App).DisplayName
}


# select * from Win32_Product where IdentifyingNumber = '{BA0C9AAF-1327-3F06-B49C-349B4BE8F740}'
# HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Updates
$list = Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
foreach ($App in $list)
{
    (Get-ItemProperty registry::$App).DisplayName
}


$list = Get-ChildItem HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion
foreach ($App in $list)
{
    (Get-ItemProperty registry::$App).DisplayName
}

Get-WmiObject win32_quickfixengineering | fl *

Network Test


# --------------------------------------------------------------
# Create Random data file.
# --------------------------------------------------------------

# File.WriteAllBytes Method 
# http://msdn.microsoft.com/en-us/library/system.io.file.writeallbytes.aspx
#
# http://www.matisse.net/bitcalc/
#
# Conversions:
#
# 1 Mbps = 1024 kbps = 131072 bytes 
# 10 Mbps = 10240 kbps = 1310720 bytes
# 100 Mbps = 102400 kbps = 13107200 bytes
# 1 Gbit = 1048576 kbps = 134217728 bytes
#
# ----------------------------------------
#
# Timing Results: 
#
# 1 Mbps   - 131072    bytes - 1 Second (~)
# 10 Mbps  - 1310720   bytes - 1 Second (~)
# 100 Mbps - 13107200  bytes - 1 Second (~)
# 1 Gbit   - 134217728 bytes - 1 Seconds (~)
# 

function fnFile($path,$size)
{ 
	write-host "Creating file: $path - $size bytes"
	$rand = New-Object System.Random 999999999 
	$bytes = New-Object byte[] $size
	$rand.NextBytes($bytes) 
	[io.file]::WriteAllBytes($path,$bytes)
}

cls

$source_file = "test.dat"
$destination = "\\S2\C$"
$file_size = 131072

fnFile $source_file $file_size

write-host "Copy Start"
$time = measure-command {Copy-Item -Path $source_file -Destination $destination}
write-host "Copy End"
$time

List Package Details

Method 1

function fnPackage_Type($package_type)
{
    switch ($package_type)
    {
    	0 {"Software Distribution"}
	3 {"Driver"}
	4 {"Task Sequence"}
	5 {"Software Update"}
	6 {"Device Setting"}
	7 {"Virtual"}
	257 {"Image"}
	258 {"Boot Image"}
	259 {"Operating System Install"}
        default {"Unknown"}
    }
}

$site_code = "XXX"
$computer = "localhost"
$query = "Select * from SMS_PackageBaseclass"
$data = Get-WmiObject -ComputerName $computer -Namespace ("root\SMS\Site_" + $site_code) -query $query
$results = $data | Select-Object PackageID,Name,PackageType,@{Name="Package Type (desc)";Expression={fnPackage_Type($_.PackageType)}},PkgSourcePath | Sort-Object PackageType
$results | Format-Table -AutoSize
$results | Export-Csv package_details.txt -Force

Method 2

function fnPackage_Type(
  [Parameter(ValueFromPipelineByPropertyName = $true)] $PackageType,
  [Parameter(ValueFromPipeline = $true)] $obj
)
{
  $obj | Add-Member NoteProperty "PackageTypeDescription" (
    switch ($PackageType)
      {
        0 {"Software Distribution Package"}
      3 {"Driver Package"}
      4 {"Task Sequence Package"}
      5 {"Software Update Package"}
      6 {"Device Setting Package"}
      7 {"Virtual Package"}
      257 {"Image Package"}
      258 {"Boot Image Package"}
      259 {"Operating System Install Package"}
          default {"Unknown"}
      } 
    )
} 

$site_code = "S01"
$computer = "localhost"
$query = "Select * from SMS_PackageBaseclass"
Get-WmiObject -ComputerName $computer -Namespace ("root\SMS\Site_" + $site_code) -query $query |
  fnPackageType | Select-Object Name,PkgSourcePath,PackageType,PackageTypeDescription

Task Sequences

Task Sequence References



function fnPackage_Type($package_type)
{
    switch ($package_type)
    {
        0 {"Software Distribution"}
	    3 {"Driver"}
	    4 {"Task Sequence"}
	    5 {"Software Update"}
	    6 {"Device Setting"}
	    7 {"Virtual"}
	    257 {"Image"}
	    258 {"Boot Image"}
	    259 {"Operating System Install"}
        default {"Unknown"}
    }
}

clear

$site_code = "XXX"
$computer = "PX1"
$package_id = "YYY"
$query = "select * from SMS_TaskSequenceReferencesInfo where PackageID='$package_id'"
$data = Get-WmiObject -ComputerName $computer -Namespace ("root\SMS\Site_" + $site_code) -query $query
$results = $data | Select-Object ReferencePackageID,ReferenceName,ReferencePackageType,@{Name="Package_Type";Expression={fnPackage_Type($_.ReferencePackageType)}} | Sort-Object Package_Type
$results | Format-Table ReferencePackageID,ReferenceName,Package_Type -auto
$results | Export-Csv task_references.txt -Force -NoTypeInformation

Site Changes

Site Navigation

Personal tools