Monday, April 18, 2016

Set SCCM primary User/Device (Affinity). use as module

When you want to set a device to a primary User.
This script lets you view, add en remove primary users from SCCM without the need of a SCCM management console. Use an account that has a appropriate security level.

Import this script like: import-module 'path to script' -argumentlist 'SITESERVER','SITECODE'
Use Functions like:
Get-PrimaryDevice 'userID'
Get-PrimaryUser 'Computername'
Set-Affinity 'userID' 'ComputerName'
Remove-Affinity 'userID' 'ComputerName'


 param(  
      [string]$Siteserver,  
      [string]$Sitecode  
      )  

 Function Get-PrimaryDevice  
 {  
   PARAM
      (  
           [String]$User       
      )  
   Get-WmiObject -ComputerName $SITESERVER -Namespace "root\SMS\Site_$SITECODE" -Query "select * from SMS_UserMachineRelationship WHERE UniqueUserName like'%$User%' AND IsActive = '1' AND Types = '1'" 
 }  
 Function Get-PrimaryUser  
 {  
   PARAM
      (  
           [String]$Device  
      )    
   Get-WmiObject -ComputerName $SiteServer -Namespace "root\SMS\Site_$SiteCode" -Query "select * from SMS_UserMachineRelationship WHERE ResourceName like '%$Device%' AND IsActive = '1' AND Types = '1'"
 }  
 Function Set-Affinity  
 {  
   PARAM
      (  
     [String]$User,  
           [String]$Device  
      )    
   $CMDeviceResourceID = Get-WmiObject -Namespace "root\SMS\site_$SiteCode" -Class SMS_R_System -ComputerName $SiteServer -Filter "Name like '%$Device%'" | Select-Object -ExpandProperty ResourceID  
      $CMUserName = Get-WmiObject -Namespace "root\SMS\site_$SiteCode" -Class SMS_R_User -ComputerName $SiteServer -Filter "Name like '%$User%'" | Select-Object -ExpandProperty Name  
   $WMIConnection = [WmiClass]"\\$($SiteServer)\root\SMS\site_$($SiteCode):SMS_UserMachineRelationship"  
      $NewRelation = $WMIConnection.psbase.GetMethodParameters("CreateRelationship")  
   $NewRelation.MachineResourceId = $CMDeviceResourceID  
      $NewRelation.SourceId = 2  
      $NewRelation.TypeId = 1  
      $NewRelation.UserAccountName = $CMUserName  
   $WMIConnection.psbase.InvokeMethod("CreateRelationship", $NewRelation, $null)  
 }  
 Function Remove-Affinity  
 {  
   PARAM
      (  
     [String]$User,  
     [String]$Device  
      )  
      $CMDeviceResourceID = Get-WmiObject -Namespace "root\SMS\site_$SiteCode" -Class SMS_R_System -ComputerName $SiteServer -Filter "Name like '%$Device%'" | Select-Object -ExpandProperty ResourceID  
      $CMUserName = Get-WmiObject -Namespace "root\SMS\site_$SiteCode" -Class SMS_R_User -ComputerName $SiteServer -Filter "Name like '%$User%'" | Select-Object -ExpandProperty Name  
      $RelationshipResourceID = Get-WmiObject -ComputerName $SiteServer -Namespace "root\SMS\Site_$SiteCode" -Query "select * from SMS_UserMachineRelationship WHERE UniqueUserName like'%$User%' AND ResourceName like '%$Device' AND IsActive = '1' AND Types = '1'"  
   $RelationshipResourceID.psbase.delete() | New-PSSession -computername $Siteserver  
 }  
 Write-host "Import this script like: import-module 'path to script' -argumentlist 'SITESERVER','SITECODE'`nUse Functions like:`nGet-PrimaryDevice 'userID'`nGet-PrimaryUser 'Computername'`nSet-Affinity 'userID' 'ComputerName'`nRemove-Affinity 'userID ComputerName'" -ForegroundColor Cyan   

Thursday, April 14, 2016

SCCM 2012 Redistribute all packages to distribution points with state INSTALL_RETRYING, INSTALL_FAILED or VALIDATION_FAILED. use as script

In case you sometimes have applications, Packages or Driver packages failed (State 2,3 or 8) to distribute to some Distribution points in SCCM 2012 you can use this script to re-sync them to the appropriate Distribution Points. It's easy to use because you don't need the SCCM management console for it, just make sure you use an account that has a appropriate security level to perform this action.

$SITESERVER = 'Your SiteServer'
$SITECODE = 'Your Sitecode'

$FAILED = Get-WmiObject -computername $SITESERVER -Namespace "root\SMS\Site_$SITECODE" -Query "Select * From SMS_PackageStatusDistPointsSummarizer where (State = 2 OR state = 3 OR State = 8)"

if ($FAILED)
{
    foreach ($i in $FAILED)
    {
        Write-host "Package ID:"$i.PackageID"`nState:"$i.State"`nServerNalPath:"$i.ServerNALPath"`n" -ForegroundColor Red
    }
        Write-Host $FAILED.count "Objects failed to distribution`n" -ForegroundColor Red
        Write-host "State 2 = INSTALL_RETRYING`tState 3 = INSTALL_FAILED`tState 8 = VALIDATION_FAILED" -ForegroundColor Cyan
        
}
else 
{
    Write-Host "No Objects failed to distribute" -ForegroundColor DarkGreen
} 

Write-host "Press a key to start Redistribution" -ForegroundColor Cyan
Pause
$count = $FAILED.count
foreach ($i in $FAILED)
{
   
    Write-host  ($count--)" - Refreshing "$i.PackageID -ForegroundColor Cyan

    $Objectid = Get-WmiObject -computername $SITESERVER -Namespace "root\SMS\Site_$SITECODE" -Query "Select * From SMS_DistributionPoint WHERE PackageID='$($i.PackageID)' AND ServerNALPath like '%$($i.ServerNALPath.Substring(12,10))%'"
    $Objectid.RefreshNow = $True
    [Void]$Objectid.Put()  
}

SCCM 2012 remove a device from SCCM with Powershell. use as module

whenever you need to remove a device based on computer name or mac address you can use this script.No need for SCCM management console. Use an account that has a appropriate security level.
Import this script like: import-module 'path to script' -argumentlist 'SITESERVER','SITECODE'
Use Functions like:
Remove-SCCMDevice 'Device name'
If removing by MACAddress use a colon : or - as seperator

 param(
 [string]$Siteserver,
 [string]$Sitecode
 )

Function Remove-SCCMDevice
{
    PARAM( 
        [string]$INPUT
        )
    
    if ($INPUT -match "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$")
    {
        $RESID = Get-WmiObject -computername $SITESERVER -Namespace "root\sms\site_$SITECODE" -query "select resourceID,MACAddresses from sms_r_system where MACAddresses like `'$INPUT`'"
    }
    elseif ($INPUT)
    {
        $RESID = Get-WmiObject -computername $SITESERVER -Namespace "root\sms\site_$SITECODE" -query "select resourceID,Name from sms_r_system where name like `'$INPUT`'" 
    }

    else
    {
        Write-Host "empty INPUT or Canceled"
        exit 
    }
    if ($resID.ResourceId -eq $null) 
    {
        Write-Host "The Device does not exist in SCCM"
        exit
    }
    else 
    {
    $RESID.psbase.delete() | New-PSSession
    write-host "Deleted device $INPUT from SCCCM"
    }
}

Write-host "Import this script like: import-module 'path to script' -argumentlist 'SITESERVER','SITECODE'`nUse Functions like:`nRemove-SCCMDevice 'Device name'`nIf removing by MACAddress use a colon : as seperator like 00:00:00:00:00:00" -ForegroundColor Cyan