Skip to content

Instantly share code, notes, and snippets.

@Spartan-196
Spartan-196 / ManualSCCMusmtRestore.ps1
Last active May 3, 2019 13:54
Manually Restore USMT Capture stored on SCCM migration point from Client PC
##Requirements##
# Disclaimer: Provided as is no support
# Update 1/22/18 After eventual upgrade this still works with SCCM 1710
# Update 4/24/19 Added section for extracting USMT mig file to directory
# DO NOT LOGIN TARGET USER FIRST! RUN AS SEPARATE ADMIN ACCOUNT
# 1. USMT capture must have already been ran on source computer and completed
# 2. Read permissions on SCCM server with USMT Data Store.
# 3. USMT 5 or 10 from Windows ADK https://developer.microsoft.com/en-us/windows/hardware/windows-assessment-deployment-kit
# 4. Know how SCCM is reading your user accounts. My environment is not able to use the new UPN setup for user resolution so I have to use the SAM account names also known as the "pre-windows 2000" name in AD
# 5. This script is written as though it is placed alongside the loadstate.exe file in the USMT required files.
@Spartan-196
Spartan-196 / ClearSCCMCache.ps1
Created February 23, 2018 03:25
Clear SCCM Cache from Powershell
# Disclaimer: Provided as is with no support.
# These items are removed by using a com-object that acts the same as if you cleared it from control panel but with some more control such as only older objects that arent persistant.
# This scirpt was piece mealed together from other places to fit the need of clearing a sccm clients cache from a remote pssesion. Several of our clients got increased caches of nearly 11GB
# Eventually after user data was made over several years disk space was getting tight and a good chunk could be reaclaimed for our end users just by clearing the cache alone.
#WMI query from on http://cm12sdk.net/?p=1517
$UIResourceMgr = New-Object -ComObject UIResource.UIResourceMgr
$Today = Get-Date #For date compare.
@Spartan-196
Spartan-196 / Elevate.ps1
Created February 23, 2018 03:46
Auto Elevate at beginning of powershell script
#Originally gotten from Ben Armstrong MSDN Blog
#https://blogs.msdn.microsoft.com/virtual_pc_guy/2010/09/23/a-self-elevating-powershell-script/
# Get the ID and security principal of the current user account
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
# Get the security principal for the Administrator role
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
# Check to see if we are currently running "as Administrator"
@Spartan-196
Spartan-196 / shortcut.md
Last active February 23, 2018 03:56
Create Shortcut from command line
set TARGET='[Exactpath to folder or exe]' #Must be in quotes if it contains spaces
set SHORTCUT='[path to where you want the shortcut].lnk' #Example: C:\Users\Public\Desktop\New.lnk
powershell.exe -Command "$ws = New-Object -ComObject WScript.Shell; $S = $ws.CreateShortcut(%SHORTCUT%); $S.TargetPath = %TARGET%; $S.Save()"

This is from the days prior to using psremote for most things and was still limping along with telnet, so its writen to work in windows from cmd.exe or from a batch file. Final heavy lifter is a a powershell cal to wscript.shell

@Spartan-196
Spartan-196 / AppFromID.ps1
Created February 23, 2018 04:00
SCCM find application from ContentID
#You can of of course skip defining the variable and just plug and play as needed in the bottom line.
$SiteCode = '[sccmsite]' #example HQ
$Server = '[sccm primary server dnsname]' #example: myccmserver
$ContentID = '[ContentID_From_Report]' #example: HQ00001
Get-WmiObject -Namespace root\sms\site_$SiteCode -ComputerName $Server -Class SMS_Deploymenttype -Filter “ContentID = '$($ContentID.Split(".")[0])'" | Select LocalizedDisplayName -Unique
@Spartan-196
Spartan-196 / FindGUID.md
Last active November 1, 2023 12:59
Find GUID of PC in windows

From Powershell

Get-WmiObject Win32_ComputerSystemProduct | Select UUID

From command prompt

wmic csproduct get uuid

This will out put the PC's UUID which is what SCCM reads as BIOS GUID.

A copy of PSExec is required for this.

From an elevated command prompt or powershell window run psexec as an interactive system instance of cmd.exe

PsExec.exe -i -s -d cmd.exe

In the NEW cmd window that opens bring up stored username and passwords dialog

rundll32 keymgr.dll,KRShowKeyMgr

@Spartan-196
Spartan-196 / CollectionFromAD.md
Last active February 23, 2018 04:29
SCCM Collection Members from AD Groups

Replace [Domain] and [groupname] with the the apropriate values for your domain and the group you want to pull members from.

Select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from SMS_R_System where SMS_R_System.SystemGroupName = "[Domain]\\[groupname]"

Worth noting that this is a single line so if you copy pasteing may be easier to use the code block

Select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from SMS_R_System where SMS_R_System.SystemGroupName = "[Domain]\\[groupname]"
@Spartan-196
Spartan-196 / LastLogonPC.ps1
Last active April 8, 2019 20:58
PC where UserID is last logged on user SCCM
#Used when you have a user id but need to know the name of the computer where they were the last logged on user. If they are using a shared or generic pc where another user has logged in after them since they logged off and the sccm client has reported back already it may not reflect that.
#Wmi Call is rather long as it does a calculated select to convert the datetime from WMI format to a date format easily read, CIM does this conversion automatically for you.
$SiteCode = "" #example: HQ
$SCCMServer = "" #example: MySCCMServer.domain.com
$UserName= "" #This is the SAMAccountName listed in an AD object for a user.
#WMI Version
Get-WmiObject -namespace "root\sms\site_$SiteCode" -ComputerName $SCCMServer -query "select * from sms_r_system where LastLogOnUserName='$UserName'" | Select-Object Name,MACAddresses,IPAddresses,OperatingSystemNameAndVersion,LastLogOnUserName,ResourceNames,SMBIOSGUID,@{Name="LastLogonTimestamp"; Expression = {[DateTime]::ParseExact(($_.LastLogonTimestamp).split('.')[0], 'yyyyMMddHHmmss'
@Spartan-196
Spartan-196 / LastLoggedinUser.ps1
Last active April 8, 2019 20:58
FInd Last Logged in User
#Used when all you have is a computer name no one is logged in currently or is now offline such as laptop or desktop at remote site. As long as their client has reported back who loggedin last you can get a user name for that person.
#Wmi Call is rather long as it does a calculated select to convert the datetime from WMI format to a date format easily read, CIM does this conversion automatically for you.
$SiteCode = "" #example: HQ
$SCCMServer = "" #example: MySCCMServer.domain.com
$PCName= "" #This is the Computer Name listed in SCCM that you want infomraiton from.
#WMI Version
Get-WmiObject -namespace "root\sms\site_$SiteCode" -ComputerName $SCCMServer -query "select * from sms_r_system where Name='$PCName'" | Select-Object Name,MACAddresses,IPAddresses,OperatingSystemNameAndVersion,LastLogOnUserName,ResourceNames,SMBIOSGUID,@{Name="LastLogonTimestamp"; Expression = {[DateTime]::ParseExact(($_.LastLogonTimestamp).split('.')[0], 'yyyyMMddHHmmss', $null)}}
#Cim Instance
Get-CimInstance -namespace "root\sms\si