:::: MENU ::::

PNP Drivers import using Powershell

$path = $PSScriptRoot

$driversCollection = (Get-ChildItem -Path $path -Filter “*.inf” `
-Recurse -ErrorAction SilentlyContinue |
Select-Object -ExpandProperty Fullname)
foreach($driver In $driversCollection){
# Add and install driver package
pnputil.exe -i -a $driver
}


OS Installed Date > 10 Days


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 inner join SMS_G_System_OPERATING_SYSTEM on SMS_G_System_OPERATING_SYSTEM.ResourceId = SMS_R_System.ResourceId WHERE dateDiff(dd, SMS_G_System_OPERATING_SYSTEM.InstallDate, GetDate()) > 10

event viewer log export by event id


Set-Variable -Name EventAgeDays -Value 1 #we will take events for the latest 7 days
Set-Variable -Name CompArr -Value @("$env:COMPUTERNAME") # replace it with your server names
Set-Variable -Name LogNames -Value @("Application", "System") # Checking app and system logs
Set-Variable -Name ExportFolder -Value "c:\EWLogsbyEventID\" # change this yo your own location
$el_c = @() #consolidated error log
$now=get-date
if($ExportFolder){New-Item -Path $ExportFolder -ItemType Directory -Force}
$startdate=$now.adddays(-$EventAgeDays)
$ExportFile=$ExportFolder + "el" + $now.ToString("yyyy-MM-dd---hh-mm-ss") + ".csv" # we cannot use standard delimiteds like ":"

foreach($comp in $CompArr)
{
foreach($log in $LogNames)
{
Write-Host Processing $comp\$log
$el = get-eventlog -ComputerName $comp -LogName $log -After $startdate | where EventID -EQ 1000
$el_c += $el #consolidating
}
}
$el_sorted = $el_c | Sort-Object TimeGenerated #sort by time
Write-Host Exporting to $ExportFile
$el_sorted|Select EntryType, TimeGenerated, Source, EventID, MachineName, Message | Export-CSV $ExportFile -NoTypeInfo #EXPORT


Powershell Script for Installed driver details to export



param(
    [string]$outputFile
)

$dateTimeString = $(get-date -UFormat '%Y-%m-%d_%T.%S').Replace(':','')
if (! ($outputFile)) {
    $outputFile = "c:\temp\$($env:computername)--PnpSignedDrivers--$($dateTimeString).csv"
}

#$netBackupShare = '\\SHARE\PnPSignedDrivers'

$deviceIdIgnoreList= @(
    'Send To OneNote 2013',
    'WebEx Document Loader',
    'HTREE\ROOT\0',
    'Fax',
    'Microsoft Print to PDF',
    'Microsoft XPS Document Writer'
);

$selectObjectList = @(
    'DeviceClass',
    'Manufacturer',
    'DriverVersion',
#'DriverDate',
    @{Label='DriverDate';Expression={$_.ConvertToDateTime($_.DriverDate)}},
    'Description',
    'DeviceName',
    'FriendlyName',
    'CompatID',
    'DeviceID',
    'HardWareID',
    'DevLoader',
    'DriverName',
    'DriverProviderName',
    'InfName',
    'InstallDate',
    'IsSigned',
    'Signer'
);

Write-Host ''
Write-Host 'Pulling PnP Signed Drivers from WMI ...'
$wmiClass = 'Win32_PnPSignedDriver'
$drivers = Get-WmiObject $wmiClass | 
    Where-Object { !($deviceIdIgnoreList.Contains($_.DeviceID)) } |
    Select-Object $selectObjectList |
        Sort-Object Manufacturer,DeviceClass,Description

if ($drivers) {
    Write-Host ''
    Write-Host 'Exporting results to .CSV ...'
        $drivers | Export-Csv -Path $outputFile

    Write-Host '--> Cleaning up .CSV ...'
    $fileBuffer = Get-Content -Path $outputFile | Select -Skip 1
    Set-Content -Path $outputFile -Value $fileBuffer

    Write-Host ''
    Write-Host "Final output file is located at:`r`n   `"$($outputFile)`""

   # Copy-Item $outputFile "$($netBackupShare)\"
#    Write-Host ''
   # Write-Host "File has also been backed up to:`r`n   $($netBackupShare)\"
} else {
    Write-Host ''
    Write-Host "No driver '$($wmiClass)' was found."
}



Powershell Script for All ADUsers


                PROCESS #This is where the script executes 
{ 
    $path = "C:\temp\" 
    $pathexist = Test-Path -Path $path 
    If ($pathexist -eq $false) 
    {New-Item -type directory -Path $path} 
     
    $reportdate = Get-Date -Format ssddmmyyyy 
 
    $csvreportfile = $path + "\ALLADUsers_$reportdate.csv" 
     
    #import the ActiveDirectory Module 
    Import-Module ActiveDirectory 
     
    #Perform AD search. The quotes "" used in $SearchLoc is essential 
    #Without it, Export-ADUsers returuned error 
                  Get-ADUser -Properties * -Filter * |  
                  Select-Object @{Label = "First Name";Expression = {$_.GivenName}},  
                  @{Label = "Last Name";Expression = {$_.Surname}}, 
                  @{Label = "Display Name";Expression = {$_.DisplayName}}, 
                  @{Label = "Logon Name";Expression = {$_.sAMAccountName}}, 
                  @{Label = "Full address";Expression = {$_.StreetAddress}}, 
                  @{Label = "City";Expression = {$_.City}}, 
                  @{Label = "State";Expression = {$_.st}}, 
                  @{Label = "Post Code";Expression = {$_.PostalCode}}, 
                  @{Label = "Country/Region";Expression = {if (($_.Country -eq 'GB')  ) {'United Kingdom'} Else {''}}}, 
                  @{Label = "Job Title";Expression = {$_.Title}}, 
                  @{Label = "Company";Expression = {$_.Company}}, 
                  @{Label = "Description";Expression = {$_.Description}}, 
                  @{Label = "Department";Expression = {$_.Department}}, 
                  @{Label = "Office";Expression = {$_.OfficeName}}, 
                  @{Label = "Phone";Expression = {$_.telephoneNumber}}, 
                  @{Label = "Email";Expression = {$_.Mail}}, 
                  @{Label = "Manager";Expression = {%{(Get-AdUser $_.Manager -Properties DisplayName).DisplayName}}}, 
                  @{Label = "Account Status";Expression = {if (($_.Enabled -eq 'TRUE')  ) {'Enabled'} Else {'Disabled'}}}, # the 'if statement# replaces $_.Enabled 
                  @{Label = "Last LogOn Date";Expression = {$_.lastlogondate}} |  
                   
                  #Export CSV report 
                  Export-Csv -Path $csvreportfile -NoTypeInformation     
} 





Turn off Bluetooth if no Device is connected actively

Get the Exe file from the following website -

https://www.nirsoft.net/utils/bluetooth_viewer.html

create the following power shell script

$LogFile = "C:\Windows\MSSCCM\Logs\Toggle_Bluetooth_OFF.log"
Function LogWrite
{
Param ([string]$logstring)
$logcontent = "$date $logstring"
Add-Content $Logfile -Value $logcontent
}

New-Item -ItemType Directory -Path "C:\Windows\MSSCCM\Scripts\Bluetooth" -Force -ErrorAction SilentlyContinue

$date=Get-Date -Format g
$PREDirectory = Split-Path -Parent $MyInvocation.MyCommand.Definition
$InstDir = Split-Path -parent $MyInvocation.MyCommand.Definition

LogWrite "START $date*********************************************"

$Parameters = '/sxml "C:\Windows\MSSCCM\Scripts\bluetooth\bluetooth.xml"'
$Commandtorun = "btView.exe"
$process= Start-Process $CommandToRun $Parameters -PassThru -wait -workingdirectory $InstDir
$ExtVal = $process.ExitCode
logwrite "created the XML file with code:$ExtVal"

if(Test-Path "C:\Windows\MSSCCM\Scripts\bluetooth\bluetooth.xml")
{

logwrite "Searching for any connected devices"

$IS_device_connected = Select-String -Path "C:\Windows\MSSCCM\Scripts\bluetooth\bluetooth.xml" -Pattern "<connected>Yes</connected>" -SimpleMatch -Quiet


if(!$IS_device_connected)
{

logwrite "NO Devices are connected, so toggling the bluetooth setting OFF"

################################################################
#*********TURN OFF BLUETOOTH AS NO DEVICE IS CONNECTED*********#
################################################################
 Function Bluetooth {
 [CmdletBinding()] 
 Param (
 [Parameter(Mandatory=$true)][ValidateSet('Off', 'On')][string]$BluetoothStatus
 )
 
 If ((Get-Service bthserv).Status -eq 'Stopped') { Start-Service bthserv }
 Add-Type -AssemblyName System.Runtime.WindowsRuntime
 $asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]
 
 Function Await($WinRtTask, $ResultType) {
 $asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
 $netTask = $asTask.Invoke($null, @($WinRtTask))
 $netTask.Wait(-1) | Out-Null
 $netTask.Result
 }
 
 [Windows.Devices.Radios.Radio,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
 [Windows.Devices.Radios.RadioAccessStatus,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
 Await ([Windows.Devices.Radios.Radio]::RequestAccessAsync()) ([Windows.Devices.Radios.RadioAccessStatus]) | Out-Null
 $radios = Await ([Windows.Devices.Radios.Radio]::GetRadiosAsync()) ([System.Collections.Generic.IReadOnlyList[Windows.Devices.Radios.Radio]])
 $bluetooth = $radios | ? { $_.Kind -eq 'Bluetooth' }
 [Windows.Devices.Radios.RadioState,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
 Await ($bluetooth.SetStateAsync($BluetoothStatus)) ([Windows.Devices.Radios.RadioAccessStatus]) | Out-Null
}
 


Bluetooth -Verbose -BluetoothStatus Off


LogWrite "Turned OFF the bluetooth on the machine"

####################################################################
####################################################################

}
else
{

LogWrite "Exiting the Script as there are Bluetooth devices connected to the machine "

}


}
else
{

LogWrite "There was an error running the tool to create the XML file"

}

LogWrite "END $date******************************************************"
LogWrite ""


CCM repair

#Stop and remove ccm service
& sc stop ccmsetup
& sc delete ccmsetup

#Remove ccm certs
Remove-Item ‘HKLM:\SOFTWARE\Microsoft\SystemCertificates\SMS\Certificates\*’ -Force

#Remove ccm files
Remove-Item -Path $env:windir\ccm -Recurse -Force
Remove-Item -Path $env:windir\system32\ccm -Recurse -Force
Remove-Item -Path $env:windir\ccmcache -Recurse -Force
Remove-Item -Path $env:windir\ccmsetup -Recurse -Force
Remove-Item -Path $env:windir\system32\ccmsetup -Recurse -Force
Remove-Item -Path $env:windir\smscfg.ini -Force
Remove-Item -Path $env:windir\sms*.mif -Force

#Remove ccm registry keys
$RegRoot = “HKLM:\Software\Microsoft”
Remove-Item -Path “$RegRoot\ccm” -Recurse -Force
Remove-Item -Path “$RegRoot\ccmsetup” -Recurse -Force
Remove-Item -Path “$RegRoot\sms” -Recurse -Force

#Remove ccm wmi namespaces
Get-WmiObject -Query “SELECT * FROM __Namespace WHERE Name=’CCM'” -Namespace “root” | Remove-WmiObject
Get-WmiObject -Query “SELECT * FROM __Namespace WHERE Name=’SMS'” -Namespace “root\cimv2” | Remove-WmiObject

#Repair WMI
$Path = ‘C:\Windows\System32\wbem’
Stop-Service -Name Winmgmt -Force
Remove-Item “$Path\repository” -Recurse -Force
& wmiprvse /regserver
Start-Service -Name Winmgmt
Get-ChildItem $Path -Filter *.dll | ForEach-Object { & regsvr32.exe /s $_.FullName } | Out-Null
Get-ChildItem $Path -Filter *.mof | ForEach-Object { & mofcomp.exe $_.FullName } | Out-Null
Get-ChildItem $Path -Filter *.mfl | ForEach-Object { & mofcomp.exe $_.FullName } | Out-Null
& mofcomp.exe ‘C:\Program Files\Microsoft Policy Platform\ExtendedStatus.mof’ | Out-Null


%Microsoft Visio Professional%


&nbsp;

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 inner join SMS_G_System_ADD_REMOVE_PROGRAMS on SMS_G_System_ADD_REMOVE_PROGRAMS.ResourceId = SMS_R_System.ResourceId where SMS_G_System_ADD_REMOVE_PROGRAMS.DisplayName = "%Microsoft Visio Professional%"

&nbsp;