Skip to main content

13 posts tagged with "PowerShell"

View All Tags

Using PowerShell to start the DFS Namespace service

· 3 min read

Distributed File System (DFS) has some service dependencies - so if those don't start the DFS Namespace service will also not start.

DFS Namespace

The dependencies are:

  • Remote Registry
  • Security Accounts Manager
  • Server
  • Workstation

I have seen the Remote Registry service become the culprit of the DFS-N service not starting.

In my experience, this had been caused by antivirus software changing the Remote Registry service to Disabled start-up type so when the DFS-N server restarts, one of the dependency services:

Remote Registry does not start so if you have issues with the DFS-N service not starting – check the Remote Registry Start-up type is configured to Automatic and click Start to confirm there are no errors and try starting the DFS-N service again.

Note: RemoteRegistry – although it is Automatic, will only Start when it is being used so don't be alarmed if it is in a 'Stopped' state.

Remote Registry

I have also created a PowerShell script to do some general checking for the DFS namespace service – which sets the Remote Registry service to Automatic startup then gets the other DFS dependency services and changes the startup type to Automatic and starts them and finally tries to start the DFS Namespace service.

Start-DFS.ps1

#requires -Version 2.0

<#
.SYNOPSIS
Starts the DFS service

.DESCRIPTION
Changes the Remote Registry service to Automatic start-up and Start the DFS NameSpace service dependencies, then start the DFS namespace service.
If the service does not start, it will retrieve the last 10 event log items from the DFS log.

.NOTES
Version: 1.0
Author: Luke Murray (Luke.Geek.NZ)
Creation Date: 20/03/17
Purpose/Change:
20/03/17 - Initial script development
11/06/18 - Updated script formatting

.EXAMPLE
./Start-DFS-Service.ps1

#>

#---------------------------------------------------------[Script Parameters]------------------------------------------------------

$ServiceName = 'DFS'
$ErrorActionPreference = 'Stop'

#-----------------------------------------------------------[Execution]------------------------------------------------------------

Try
{
Get-Service -Name RemoteRegistry | Set-Service -StartupType Automatic
}
Catch
{
Write-Verbose -Message 'There is an issue changing the Remote Registry Service to Automatic Startup Type' -Verbose
}
Try
{
$ServiceDependency = Get-Service -Name $ServiceName -DependentServices
$ServiceDependency | Set-Service -StartupType Automatic | Start-Service
Write-Verbose -Message "$ServiceName dependencies have started. Will now try starting the $ServiceName service.." -Verbose
}
catch [Microsoft.PowerShell.Commands.ServiceCommandException]
{
[Management.Automation.ErrorRecord]$e = $_

$info = New-Object -TypeName PSObject -Property @{
Exception = $e.Exception.Message
Reason = $e.CategoryInfo.Reason
Target = $e.CategoryInfo.TargetName
Line = $e.InvocationInfo.ScriptLineNumber
Column = $e.InvocationInfo.OffsetInLine
}
Write-Verbose -Message 'Opps! There was an error:' -Verbose
$info
}
Catch
{
Write-Verbose -Message "There was an issue starting $ServiceName dependencies" -Verbose
}

try
{
Try
{
Start-Service -Name $ServiceName
Write-Verbose -Message "The $ServiceName service has started." -Verbose
}
Catch
{
Get-WinEvent -LogName Microsoft-Windows-DFSN-Server/Operational | Select-Object -Last 10
}
}

catch
{
[Management.Automation.ErrorRecord]$e = $_

$info = New-Object -TypeName PSObject -Property @{
Exception = $e.Exception.Message
Reason = $e.CategoryInfo.Reason
Target = $e.CategoryInfo.TargetName
Line = $e.InvocationInfo.ScriptLineNumber
Column = $e.InvocationInfo.OffsetInLine
}
Write-Verbose -Message 'Opps! There was an error:' -Verbose
$info
}

Note: Script is also hosted on my Github repository. Feel free to clone/recommend improvements or fork.

Using PowerShell to connect to Microsoft Azure

· 2 min read

Microsoft Azure has a good user portal where you can do most things, however when it comes to automation, gathering a lot of information at once and more in-depth scenarios that the Portal doesn’t quite offer – PowerShell is used.

Before you can use PowerShell to connect to Microsoft Azure, you need to install the Azure Resource Manager modules first – follow the guide below:

Disable SMB1

Once the Az modules has been installed – you can now connect to Azure.

Usually you would have to go through the process of logging in to Azure, finding what subscription you need to connect to and then selecting that manually, however I have created a little function that will connect to Azure and automatically populate a list of the subscriptions that your account has access to in a window which you can then select to connect to which makes the process easier without having to remember different Azure subscription names or ids. This function can easily be used in any environment. I have it loaded as part of my PowerShell profile script so the function can be run from the second I open up a new PowerShell prompt.

Note: Script is also hosted on my Github repository. Feel free to clone/recommend improvements or fork.

How to clear the local workstations Group Policy cache

· One min read

Option 1

  1. Open My Computer/Computer
  2. In the URL or address bar paste: %windir%\system32\GroupPolicy
  3. Right click and delete the: Machine and User folders to clear local group policy cache
  4. Restart the computer to reapply the group policies

Note: You can also run: gpupdate /force on the machine to force the policy to reapply.

You can also run the little PowerShell oneliner as Administrator to remove the Group Policy folder and all files below:

#requires -Version 1.0
#Requires -RunAsAdministrator

Remove-Item "$env:windir\system32\GroupPolicy" -Force -Recurse

Option 2

  1. Delete the “HKLM\Software\Policies\Microsoft Key (looks like a folder).
  2. Delete the “HKCU\Software\Policies\Microsoft Key
  3. Delete the “HKCU\Software\Microsoft\Windows\CurrentVersion\Group Policy Objects Key.
  4. Delete the “HKCU\Software\Microsoft\Windows\CurrentVersion\Policies Key.

Option 3

  1. Remove the computer from the domain (change to a Workgroup)
  2. Restart computer
  3. Run gpupdate /force
  4. Rejoin the domain