Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Tuesday, 27 December 2022

Re-enabling Defender on Windows 2016 servers via command line

I've carried out a number of Microsoft Defender for Endpoint deployments recently. I find that workstations are easy enough to deal with but it can be a pain working with low level servers. Windows Defender shipped natively with Windows 2016 Servers and organizations came up with some pretty creative ways to stop Defender from running. In fairness, at the time, Defender caused some pretty serious CPU spikes when co-existing with a third party antivirus solution.

I've come across three scenarios in particular. They are easy enough to deal with in the UI but I just wanted to show how you can also deal with these using commands. This was essential for me when working at scale and using management tools to automate the solution.

Scenario 1: the Defender feature had been uninstalled.

The Defender features can be added back using the "Add Roles and Features wizard". Don't forget to add the sub-features as well.

Scenario 1 command line:

Dism /Online /Enable-Feature /FeatureName:Windows-Defender-Features

Dism /Online /Enable-Feature /FeatureName:Windows-Defender

Dism /Online /Enable-Feature /FeatureName:Windows-Defender-Gui

Don't forget to add a reboot step afterwards.

Scenario 2: Defender service had been disabled.


You'll be notified that the Defender service is stopped and you won't be able to start it. I found this scenario when Microsoft Defender Antivirus was disabled through Group Policy.


First make sure that you reverse this policy. Just set it to Not Configured, do not choose Disable.

Scenario 2 command line:

Then re-enable Defender using this command:

MpCmdRun.exe -wdenable

It must be executed from "C:\Program Files\Windows Defender" and you must add a step to reboot the system afterwards.


Scenario 3: Defender is turned off.


Essentially this means that Real-time protection is turned off.


It can be turned on in Settings > Windows Defender.

Scenario 3 command line:

You can turn on Real-time protection programatically using PowerShell

Set-MpPreference -DisableRealtimeMonitoring 0


You don't need a reboot for this one.

I hope this information is helpful to automate your Defender deployment. 

Until next time.....


Tuesday, 17 January 2017

PowerShell script - add and configure Intune Subscription

I've been deploying Microsoft Intune a lot recently. Adding and configuring the Intune subscription in the Configuration Manager console is very straightforward but can take be time-consuming. I've created a simple PowerShell script to automate this.

This script adds an Intune subscription to ConfigMgr Current Branch and configures the subscription to enable management of Android, iOS, Windows and Windows Phone devices.

The cmdlets in this script require a valid Intune subscription. They require Configuration Manager 1511 or later, although it is recommended to use 1606 or later. There are published workarounds for using the cmdlets in pre-1606 environments.


Instructions for use
  1. Download an APN certificate request from ConfigMgr and generate the APN certificate directly from Apple in advance of running this script. Save the Apple APN certificate to a local folder eg (E:\Sources\MDM\Apple\AppleCert.pem) https://identity.apple.com
  2. Save script to installation folder
  3. Install the System Center Configuration Manager Cmdlet Library (if you are already using PoSH with ConfigMgr you will have done this already) https://www.microsoft.com/en-us/download/details.aspx?id=46681
  4. Run PowerShell and browse to the installation folder (you may have to run PoSH as administrator as the first step is to set the execution policy to unrestricted)
  5. Run IntuneSubscriptionScript.ps1
  6. The script will prompt you to enter the following information:
  • Enter Site Server name
  • Enter Site Code
  • Enter Intune subscription username
  • Enter Intune subscription password
  • Enter Company Color Scheme (options: Blue, Magenta, Purple, Teal, Lime, Brown, Pink, Orange, Red or Green)
  • Enter your organization name
  • Enter valid contact email address
  • Enter contact name
  • Enter path to Apple APN certificate
  • Enter Apple APN certificate password - leave blank if no password

The script will create the hybrid Intune subscription with your required parameters.



It will then enable management for Android, iOS, Windows and Windows Phone platforms.


Android enabled.



iOS enabled.



Windows enrolled as MDM enabled.

Windows Phone enabled.
Note that the script does not have much error checking for now. I'll get to that when I have a chance.
Download from the TechNet gallery and try it.
Until next time....



Monday, 2 December 2013

Advanced Powershell for ConfigMgr 2012 (SCCM 2012)

Back to ConfigMgr main menu

Back to ConfigMgr & PowerShell menu


This will be the last post in my ConfigMgr 2012 & PoweShell series. The previous posts discussed the following topics:
  • PowerShell Basics
  • Connecting to ConfigMgr site via PowerShell
  • Basic PowerShell Cmdlets for ConfigMgr 2012.

In this section I will provide links to the best online resources I have found for advanced PowerShell for ConfigMgr 2012.

Getting Started with PowerShell: The Pipeline

http://blogs.technet.com/b/heyscriptingguy/archive/2013/12/02/getting-started-with-powershell-the-pipeline.aspx

This blog is by Ed Wilson (The Scripting Guy) and is an extract from his book "Windows PowerShell 3.0 First Steps".

It describes the way that the Windows PowerShell pipeline takes the output from one command, and sends it as input to another command. This is a very powerful scripting feature as it removes the necessity to store the results of the first command as a variable.

eg Get-NetAdapter | Enable-NetAdapter


Configuration Manager PowerShell SDK

http://cm12sdk.net/

This blog by Kaido Järvemets shares PowerShell scripts in the following areas


See sample scripts



Microsoft Configuration Manager 2012 and PowerShell

http://www.sepago.de/e/david/2012/02/25/microsoft-configuration-manager-2012-and-powershell-ae-part-2

This particular blog by David O' Brien describes how he wrote a PowerShell script to "Create Configuration Manager Device Collection".


Use the navigation arrows to find more scripts



Automate Microsoft System Center 2012 Configuration Manager with PowerShell

http://davidobrien.codeplex.com/

This is where David O' Brien has published his PowerShell scripts to Codeplex.

Click on the Source Code tab



ConfigMgr 2012 - PowerShell Right Click Tools

http://psrightclicktools.codeplex.com/

Download the PowerShell Console extensions for ConfigMgr 2012.



Friday, 22 November 2013

Basic PowerShell Cmdlets for ConfigMgr 2012 (SCCM 2012)

Back to ConfigMgr main menu

Back to ConfigMgr & PowerShell menu


Native PowerShell support for ConfigMgr 2012 was introduced with SP1. Microsoft have published a Configuration Manager Cmdlet Reference in the Technet Library. You can find it here


I'd like to highlight some of the more commonly used cmdlets (ones that I find very useful and could use daily). Click on the cmdlet to link to the Technet library page explaining its use and syntax.


Hierarchy

Add-CMBoundaryToGroup                                                         
Assigns boundaries to a boundary group

Add-CmDistributionPoint                                                             
Creates a distribution point

New-CMBoundary                                                                          
Creates a new boundary

New-CMBoundaryGroup                                                             
Creates a new boundary group

New-CMSecondarySite                                                                 
Creates a secondary site in Configuration Manager


Assets

Adds a direct membership rule to one or more Configuration Manager device collection

Approve-CMDevice                                                                        
Approves Configuration Manager device clients

Adds a query membership rule to one or more Configuration Manager device collections.

Install-CMClient                                                                                               
Installs a Configuration Manager client

New-CMDeviceCollection                                                            
Creates a collection for devices and adds the collection to the Configuration Manager hierarchy


Software Distribution

Add-CMDeploymentType                                                           
Adds a deployment type for an application

Approve-CMApprovalRequest                                                 
Approves a request to allow the installation of an application

Get-CMDeployment                                                                      
Gets a summary of Configuration Manager deployments

New-CMApplication                                                                       
Creates an application in Configuration Manager


Operating System Deployment

Add-CMDriverToDriverPackage                                                
Adds a device driver to a driver package

Clear-CMPxeDeployment                                                            
Clears the status of the most recent PXE deployment in Configuration Manager

New-CMDriverPackage                                                                 
Creates a driver package

New-CMOperatingSystemImage                                              
Creates an operating system image

New-CMTaskSequence                                                                
Creates a task sequence

Remove-CMDriverFromDriverPackage                                  
Removes a driver from a driver package

Set-CMDriverBootImage                                                              
Adds a driver to a boot image or removes a driver from a boot image


Software Updates

Add-CMSoftwareUpdateToGroup                                           
Adds a software update to a software update group in Configuration Manager

New-CMSoftwareUpdateGroup                                               
Creates a software update group



Wednesday, 20 November 2013

Connecting to ConfigMgr site via PowerShell

Back to ConfigMgr main menu

Back to ConfigMgr & PowerShell menu


There are two ways to connect to your Configuration Manager site via PowerShell.

Method 1: Using the Configuration Manager console




See the blue tab in the top left corner of the console. Click on the down arrow to reveal the menu. Select "Connect via Windows PowerShell".





It takes a few moments but PowerShell loads. See your Site Code in the command prompt.




Type the cmdlet get-cmsite for information about your site.


Method 2: Manually through PowerShell console

All Windows PowerShell cmdlets are coded and stored in a module.  The modules for Windows are available by default in your Windows PowerShell window and you can add more modules.  You add modules to your Windows PowerShell session by importing them using the Import-Module cmdlet.




This is the ConfigMgr module and can be found C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin



Open PowerShell as Administrator (Note that we need the x86 version - the ConfigMgr Admin Console is a 32-bit application) and navigate to that path.

Enter the cmdlet import-module .\ConfigurationManager.psd1 and press enter



Now all the ConfigMgr cmdlets are available

Connect to your site by typing the command

CD P01: (where P01 is your Site Code)


Try get-cmsite again

PowerShell Basics

Back to ConfigMgr main menu

Back to ConfigMgr & PowerShell menu

Windows PowerShell is Microsoft's task automation and configuration management framework, consisting of a command line shell and associated scripting language built on .NET Framework. It is a valuable administration tool as it combines the speed of a command line with the flexibility of a scripting language.

PowerShell commands are referred to as Cmdlets. All cmdlets are made up of two parts: a verb and a noun.  They are separated by a hyphen ‘-‘ character.

Some cmdlets are integrated with Windows, and others are installed with programs like ConfigMgr.

Launch PowerShell by clicking on this icon on the taskbar. (Open PowerShell as Administrator by right clicking - you should do this as most of the cmdlets you will be using will require this)




You can also search by typing PowerShell.




(Note that we need the x86 version - the ConfigMgr Admin Console is a 32-bit application)

Note the PS telling you that you are running PowerShell




Some useful commands

get-command gives you a list of all available PowerShell commands




See the verbs that can be used in cmdlets. Each verb can be used with in conjunction with various nouns to create powerful cmdlets

Add 
Block
Clear
Close
Connect
Copy
Disable 
Disconnect
Dismount
Enable
End
Export
Format
Get
Grant
Initialize
Import
Install
Invoke
Join
Limit
Measure
Merge
Mount
Move
New
Open
Optimize
Publish
Register
Remove
Rename
Repair
Reset
Resize
Resolve
Restore
Restart
Resume
Revoke
Save
Select
Send
Set
Show
Start
Stop
Suspend
Sync
Test
Unblock
Undo
Uninstall
Unregister
Update
Use
Wait
Write

get-command -verb Get

This will list all the Get cmdlets




Example

Get-service




Try this (use a service that you are allowed to stop - requires PowerShell to be run as administrator)

Get-Service -name WsusService
Stop-Service -name WsusService
Get-Service -name WsusService 



Start the service again

Start-Service -name WsusService

Verify that it is started

Get-Service -name WsusService


ConfigMgr 2012 & PowerShell

Back to main menu

Windows PowerShell is a very powerful way to automate your administrative tasks and native support was introduced with System Center 2012 Configuration Manager SP1.


ConfigMgr requires the 32-bit version of Windows PowerShell 3.0. It can be downloaded here: 

http://www.microsoft.com/en-us/download/details.aspx?id=34595

Powershell is already included as part of Windows Server 2012 Operating System so no further installation or configuration is necessary.

Note: Microsoft have published a "Windows PowerShell User's Guide". It is available on TechNet

http://technet.microsoft.com/en-us/library/cc196356.aspx

Please browse the following sections to learn about ConfigMgr and Powershell.

PowerShell Basics

Connecting to ConfigMgr site via PowerShell

Basic PowerShell Cmdlets for ConfigMgr 2012

Advanced PowerShell for ConfigMgr 2012