Forum Discussion
Vinodkumarlohar
Dec 04, 2023Copper Contributor
Dfs Namespace folder properties error using powershell
Here are the error while creating the DFS namespace: please help me to get the solution or the PowerShell script get-dfsnroot : Cannot get DFS folder properties on "\\\-DFS" At C:\install\DFS1.p...
Vinodkumarlohar
Dec 04, 2023Copper Contributor
#---------------------------------------------------------------------------
#Name = DFSroot
#Version = 1.00
#Author = Vinod Kumar Lohar
#Purpose = Hosting P drive for Techops sites in IN.YKGW.NET
#---------------------------------------------------------------------------
<#
.DESCRIPTION
The script if for: Creating DFS roots in remote sites, hubs and for INTL roots for nvstop.net domain.
Installation Log will be created in C:\Install folder
Example usage:
.\DFSroot.ps1 - It will create DFS root for site parsed from server hostname- e.g:
Executed on server: POC-WIN2022-SRV will create POC-DFS root
.\DFSroot.ps1 POC - It will create DFS root POC-DFS
.NOTES
Prerequisites:
-Must be executed on elevated window.
-Must be executed on Windows Server 2012 and higher.
What is built in:
-Checking prerequisites
-Installation of DFS components is included.
-Creation of DFS root directory structure.
-Setting up permissions for directories
-Creation of Backup commands and Scheduled Tasks.
-Logging to log file in C:\Install folder
#>
#params
param(
[string]$SiteCode = $null,
[string]$multipleDFS = "single"
)
$sitecode = $SiteCode.ToUpper()
import-module ServerManager
$date=get-date -format "yyyy.MM.dd-hh.mm.ss"
$LogFilePath="C:\install\DFSroot(creation)_logfile_$date.txt"
" " > $LogFilePath
$error = $false
#terminate function
function terminate{
log "Script would terminate now"
pause
log "***********************************Script terminated (DFS creation canceled)*********************************"
log '*************************************************************************************************************'
exit
}
#Function that displays message and stores into the logfile
function log($mesage,$message2){
$time=get-date -Format "hh.mm.ss"
$output=$time+" "+ $mesage+" "+$message2
$output>>$LogFilePath
write-host $output
}
function logNoTimestamp($mesage,$message2){
$time=get-date -Format "hh.mm.ss"
$output=$mesage+" "+$message2
$output>>$LogFilePath
write-host $output
}
#checking if server is configured to use FQDN
function DFSFQDN{
IF((Get-DfsnServerConfiguration localhost).UseFqdn -eq $False)
{
log "Server is being configured for FQDN"
Set-DfsnServerConfiguration -ComputerName localhost -UseFqdn $true
}
Else
{
log "Server is using FQDN"
}
}
#checking if script is runned as elevated rights
function checkRunAsAdmin{
$myIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$wp = New-Object Security.Principal.WindowsPrincipal($myIdentity)
if (-not $wp.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)) {
log "This script requires administrative privileges, please re-launch with elevated credentials"
pause
terminate
}
}
#Function that verify if server have proper OS installed and if it is proper member server
function verifyServer {
$computer=Get-WMIObject win32_computersystem
if(($computer).domainrole -ne 3){
return $false
}
elseif([version](Get-CimInstance Win32_OperatingSystem).version -lt 6.2){
return $false
}
else{
return $true
}
}
#Function that wait for keyboard hit
function pause{
if (((Get-Host).Name) -eq 'ConsoleHost'){
#log -NoNewLine "Press any key to continue"
#$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Start-Sleep -Seconds 1
log ""
} else {
Start-Sleep -Seconds 1
}
}
#Initialization of Site type DFS
function InitSite{
log "DFS Site Init: Start"
log "New-Item(Creating new directory):",(New-Item -ItemType directory -path C:\DFS\$DFSRootName-DFS\DATA)
}
#Function that checks if provided feature is installed
function checkifFeatureInstalled($Featurename){
if((get-windowsfeature $Featurename).installed){
return $true
}
else{ return $false
}
}
#Function that gets domain name
function getDomain{
return (Get-WmiObject Win32_ComputerSystem).domain
}
function success($param){
if($param -eq $true){
log ''
log "At least one link was not created successfully. Please check the log file"
log '*************************************************************************************************************'
log '***********************************DFS created but with some errors!!!***************************************'
log '*************************************************************************************************************'
}
else{
log 'all success'
log '*************************************************************************************************************'
log '***************************************DFS created successfully!!!!!!!!**************************************'
log '*************************************************************************************************************'
}
}
function backup ($DFSRoot){
if(!(test-path C:\Install\DFS-SAVE)){ log "New-Item(Creating Directory):" ,(new-item -ItemType Directory C:\Install\DFS-SAVE)}
if(!(test-path C:\Install\DFS-BACKUP)){ log "New-Item(Creating Directory):" , (new-item -ItemType Directory C:\Install\DFS-BACKUP)}
"dfscmd /view \\$ServerDomain\$DFSRoot-dfs /batch > c:\install\DFS-SAVE\$DFSRoot-DFS-%1.txt" | out-file C:\Install\DFS-BACKUP\Backup-DFS.bat -Encoding ASCII -Append
get-content C:\Install\DFS-BACKUP\Backup-DFS.bat | sort -u | set-content -path C:\Install\DFS-BACKUP\Backup-DFS.bat
$content = [IO.File]::ReadAllText("C:\install\DFS-BACKUP\Backup-DFS.bat")
log "Printing C:\install\DFS-BACKUP\Backup-DFS.bat file content that will be used for creating backup files"
log "*************************Starting file content: C:\install\DFS-BACKUP\Backup-DFS.bat*************************"
logNoTimestamp $content
log '**********************************************End of file content********************************************'
$days= @(("Monday", "MON"), ("Tuesday","TUE"),("Wednesday","WED"),("Thursday","THU"),("Friday","FRI"),("Saturday","SAT"),("Sunday","SUN"))
foreach($day in $days){
$long=$day[0]
$short=$day[1]
$output= schtasks /create /tn DFSDump_$long /tr "C:\INSTALL\DFS-BACKUP\Backup-DFS.bat $long" /SC WEEKLY /D $short /ST 23:00 /ru "SYSTEM" /F
log "SCHTASKS: $output"
}
}
#Main Program
cls
log '*************************************************************************************************************'
log '***************************************DFS root creation script**********************************************'
log '*************************************************************************************************************'
log Activating DFS
log ''
#check if script is runned as elevated user
checkRunAsAdmin
#check if server is a proper member server, proper OS version and member server
if(verifyServer){
log "Server OS version and membership verified successfully, script will continue"
pause
}
else{
log "Server OS version and membership verified uncsuccessfully, script will terminate now "
pause
terminate
}
#Getting Domain information from server properties and hostname
$Computername=($env:COMPUTERNAME).ToUpper()
$ServerDomain=(getDomain).ToLower()
$DomainSplited=$ServerDomain -split "\."
$DomainPrefix= $ServerDomain -split "\." | select -first 1
$DomainSuffix= $ServerDomain -split "\." | select -last 1
if($DomainSplited.Count -eq 2){
$ServerDomainRoot=$false
$DFSRootName=$SiteCode
$DomainxName=$ServerDomain
$DomainShort=$DomainPrefix+ $DomainSuffix
}
else{
}
log ""
log "Server domain is: $ServerDomain"
log "Server computername is: $Computername"
log "Domain prefix is: $DomainPrefix"
log "Domain suffix is: $DomainSuffix"
log "If it is root domain: $ServerDomainRoot"
log "DFSroot name is: $DFSRootName"
log "DomxName is: $DomainxName"
log "NB domain is: $DomainShort"
log "Multiple Site DFS init: $multipleDFS"
log ''
log '*************************************************************************************************************'
log '************************************Starting to Install DFS components***************************************'
pause
$LogFilePathOld=$LogFilePath
$LogFilePath="C:\install\DFSroot_$DFSRootName(creation)_logfile_$date.txt"
rename-item $LogFilePathOld $LogFilePath
#instaling DFS features
if(!(checkifFeatureInstalled("FS-DFS-Namespace"))){
log "Installing FS-DFS-Namespace feature"
Add-WindowsFeature FS-DFS-Namespace
}
else{
log "FS-DFS-Namespace feature already installed"
}
if(!(checkifFeatureInstalled("FS-DFS-Replication"))){
log "Installing FS-DFS-Replication feature"
Add-WindowsFeature FS-DFS-Replication
}
else{
log "FS-DFS-Replication feature already installed"
}
if(!(checkifFeatureInstalled("RSAT-DFS-Mgmt-Con"))){
log "Installing RSAT-DFS-Mgmt-Con feature"
Add-WindowsFeature RSAT-DFS-Mgmt-Con
}
else{
log "RSAT-DFS-Mgmt-Con feature already installed"
}
#check for existing root on the server
if(get-dfsnroot -computerName $ComputerName | where { $_.Path -like "*\\$Domainshort\$DFSRootName-DFS*"}){
log "DFS root exist- script will now terminate"
terminate
}
log "DFS root not found- script will continue"
pause
#check if root exist in domain
$mode=$null
if(get-dfsnroot -path "\\$DomainShort\$DFSRootName-DFS" -erroraction silentlycontinue){
log "DFS root exist in domain- script will create replica"
$mode="Replica"
}
else{
log "DFS share not exist in domain - script will create new root"
$mode="Fresh"
}
#creating folder structure
if(!(test-path c:\dfs)){
log "New-Item(Creating Directory):",( New-Item -ItemType directory -path C:\DFS )
}
else{
if($multipleDFS -eq "multipleDFS"){
log "Folder c:\dfs already exist, script will continue because of multiple site DFS init"
log '*****************************************Multiple site DFS init**********************************************'
}
else{
log "Folder c:\dfs already exist, script will now terminate"
log "If you want to create another Site DFS on this server, please run the script like: .\DFSroot.ps1 SITE multipleDFS"
log "Where SITE is sitecode of DFS root that need to be created e.g: .\DFSroot.ps1 CMM multipleDFS"
terminate
}
}
if(!(test-path c:\dfs\$DFSRootName-DFS)){
log "New-Item(Creating Directory):",(New-Item -ItemType directory -path c:\dfs\$DFSRootName-DFS)
}
#checking dfsconfiguration
DFSFQDN
#creating data folder
initsite
$output = icacls C:\DFS\$DFSRootName-DFS /inheritance:r
log "ICACLS: $output"
$output = icacls C:\DFS\$DFSRootName-DFS --% /grant Administrators:(OI)(CI)F Everyone:(CI)(RX)
log "ICACLS: $output"
$output = net share $DFSRootName-DFS=C:\DFS\$DFSRootName-DFS /GRANT:'Everyone,Full'
log "Net Share: $output"
#Execute initialization if current DFS will be replica of existing one.
if($mode -eq "Replica"){
log '*************************************************************************************************************'
log '**********************************************Replica Mode***************************************************'
log "Linking DFS Root $DFSRootName-DFS"
log (New-DfsnRootTarget -TargetPath "\\$Computername\$DFSRootname-DFS" -ErrorAction SilentlyContinue)
if(get-dfsnroot -path \\$DomainShort\$DFSRootName-DFS | get-dfsnroottarget | where {$_.Targetpath -like "*$Computername*"}) {
log '*************************************************************************************************************'
log "****************************************Creating backup functionalities**************************************"
backup($DFSRootName)
success($error)
}
else{
log "DFS replica creation not successfull"
terminate
}
}
else{
log '*************************************************************************************************************'
log '**********************************************New root mode**************************************************'
log "New root, start initialize it"
log "New-DfsnRoot: " ,(New-DfsnRoot -TargetPath "\\$Computername\$DFSRootname-DFS" -Type DomainV2)
if(get-dfsnroot -path \\$DomainShort\$DFSRootName-DFS | get-dfsnroottarget | where {$_.Targetpath -like "*$Computername*"}) {
log ''
log '*************************************************************************************************************'
log "****************************************Creating backup functionalities**************************************"
log ''
backup($DFSRootName)
success($error)
}
else{
log "DFS root creation not successfull- script will now terminate"
terminate
}
}
#Name = DFSroot
#Version = 1.00
#Author = Vinod Kumar Lohar
#Purpose = Hosting P drive for Techops sites in IN.YKGW.NET
#---------------------------------------------------------------------------
<#
.DESCRIPTION
The script if for: Creating DFS roots in remote sites, hubs and for INTL roots for nvstop.net domain.
Installation Log will be created in C:\Install folder
Example usage:
.\DFSroot.ps1 - It will create DFS root for site parsed from server hostname- e.g:
Executed on server: POC-WIN2022-SRV will create POC-DFS root
.\DFSroot.ps1 POC - It will create DFS root POC-DFS
.NOTES
Prerequisites:
-Must be executed on elevated window.
-Must be executed on Windows Server 2012 and higher.
What is built in:
-Checking prerequisites
-Installation of DFS components is included.
-Creation of DFS root directory structure.
-Setting up permissions for directories
-Creation of Backup commands and Scheduled Tasks.
-Logging to log file in C:\Install folder
#>
#params
param(
[string]$SiteCode = $null,
[string]$multipleDFS = "single"
)
$sitecode = $SiteCode.ToUpper()
import-module ServerManager
$date=get-date -format "yyyy.MM.dd-hh.mm.ss"
$LogFilePath="C:\install\DFSroot(creation)_logfile_$date.txt"
" " > $LogFilePath
$error = $false
#terminate function
function terminate{
log "Script would terminate now"
pause
log "***********************************Script terminated (DFS creation canceled)*********************************"
log '*************************************************************************************************************'
exit
}
#Function that displays message and stores into the logfile
function log($mesage,$message2){
$time=get-date -Format "hh.mm.ss"
$output=$time+" "+ $mesage+" "+$message2
$output>>$LogFilePath
write-host $output
}
function logNoTimestamp($mesage,$message2){
$time=get-date -Format "hh.mm.ss"
$output=$mesage+" "+$message2
$output>>$LogFilePath
write-host $output
}
#checking if server is configured to use FQDN
function DFSFQDN{
IF((Get-DfsnServerConfiguration localhost).UseFqdn -eq $False)
{
log "Server is being configured for FQDN"
Set-DfsnServerConfiguration -ComputerName localhost -UseFqdn $true
}
Else
{
log "Server is using FQDN"
}
}
#checking if script is runned as elevated rights
function checkRunAsAdmin{
$myIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$wp = New-Object Security.Principal.WindowsPrincipal($myIdentity)
if (-not $wp.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)) {
log "This script requires administrative privileges, please re-launch with elevated credentials"
pause
terminate
}
}
#Function that verify if server have proper OS installed and if it is proper member server
function verifyServer {
$computer=Get-WMIObject win32_computersystem
if(($computer).domainrole -ne 3){
return $false
}
elseif([version](Get-CimInstance Win32_OperatingSystem).version -lt 6.2){
return $false
}
else{
return $true
}
}
#Function that wait for keyboard hit
function pause{
if (((Get-Host).Name) -eq 'ConsoleHost'){
#log -NoNewLine "Press any key to continue"
#$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Start-Sleep -Seconds 1
log ""
} else {
Start-Sleep -Seconds 1
}
}
#Initialization of Site type DFS
function InitSite{
log "DFS Site Init: Start"
log "New-Item(Creating new directory):",(New-Item -ItemType directory -path C:\DFS\$DFSRootName-DFS\DATA)
}
#Function that checks if provided feature is installed
function checkifFeatureInstalled($Featurename){
if((get-windowsfeature $Featurename).installed){
return $true
}
else{ return $false
}
}
#Function that gets domain name
function getDomain{
return (Get-WmiObject Win32_ComputerSystem).domain
}
function success($param){
if($param -eq $true){
log ''
log "At least one link was not created successfully. Please check the log file"
log '*************************************************************************************************************'
log '***********************************DFS created but with some errors!!!***************************************'
log '*************************************************************************************************************'
}
else{
log 'all success'
log '*************************************************************************************************************'
log '***************************************DFS created successfully!!!!!!!!**************************************'
log '*************************************************************************************************************'
}
}
function backup ($DFSRoot){
if(!(test-path C:\Install\DFS-SAVE)){ log "New-Item(Creating Directory):" ,(new-item -ItemType Directory C:\Install\DFS-SAVE)}
if(!(test-path C:\Install\DFS-BACKUP)){ log "New-Item(Creating Directory):" , (new-item -ItemType Directory C:\Install\DFS-BACKUP)}
"dfscmd /view \\$ServerDomain\$DFSRoot-dfs /batch > c:\install\DFS-SAVE\$DFSRoot-DFS-%1.txt" | out-file C:\Install\DFS-BACKUP\Backup-DFS.bat -Encoding ASCII -Append
get-content C:\Install\DFS-BACKUP\Backup-DFS.bat | sort -u | set-content -path C:\Install\DFS-BACKUP\Backup-DFS.bat
$content = [IO.File]::ReadAllText("C:\install\DFS-BACKUP\Backup-DFS.bat")
log "Printing C:\install\DFS-BACKUP\Backup-DFS.bat file content that will be used for creating backup files"
log "*************************Starting file content: C:\install\DFS-BACKUP\Backup-DFS.bat*************************"
logNoTimestamp $content
log '**********************************************End of file content********************************************'
$days= @(("Monday", "MON"), ("Tuesday","TUE"),("Wednesday","WED"),("Thursday","THU"),("Friday","FRI"),("Saturday","SAT"),("Sunday","SUN"))
foreach($day in $days){
$long=$day[0]
$short=$day[1]
$output= schtasks /create /tn DFSDump_$long /tr "C:\INSTALL\DFS-BACKUP\Backup-DFS.bat $long" /SC WEEKLY /D $short /ST 23:00 /ru "SYSTEM" /F
log "SCHTASKS: $output"
}
}
#Main Program
cls
log '*************************************************************************************************************'
log '***************************************DFS root creation script**********************************************'
log '*************************************************************************************************************'
log Activating DFS
log ''
#check if script is runned as elevated user
checkRunAsAdmin
#check if server is a proper member server, proper OS version and member server
if(verifyServer){
log "Server OS version and membership verified successfully, script will continue"
pause
}
else{
log "Server OS version and membership verified uncsuccessfully, script will terminate now "
pause
terminate
}
#Getting Domain information from server properties and hostname
$Computername=($env:COMPUTERNAME).ToUpper()
$ServerDomain=(getDomain).ToLower()
$DomainSplited=$ServerDomain -split "\."
$DomainPrefix= $ServerDomain -split "\." | select -first 1
$DomainSuffix= $ServerDomain -split "\." | select -last 1
if($DomainSplited.Count -eq 2){
$ServerDomainRoot=$false
$DFSRootName=$SiteCode
$DomainxName=$ServerDomain
$DomainShort=$DomainPrefix+ $DomainSuffix
}
else{
}
log ""
log "Server domain is: $ServerDomain"
log "Server computername is: $Computername"
log "Domain prefix is: $DomainPrefix"
log "Domain suffix is: $DomainSuffix"
log "If it is root domain: $ServerDomainRoot"
log "DFSroot name is: $DFSRootName"
log "DomxName is: $DomainxName"
log "NB domain is: $DomainShort"
log "Multiple Site DFS init: $multipleDFS"
log ''
log '*************************************************************************************************************'
log '************************************Starting to Install DFS components***************************************'
pause
$LogFilePathOld=$LogFilePath
$LogFilePath="C:\install\DFSroot_$DFSRootName(creation)_logfile_$date.txt"
rename-item $LogFilePathOld $LogFilePath
#instaling DFS features
if(!(checkifFeatureInstalled("FS-DFS-Namespace"))){
log "Installing FS-DFS-Namespace feature"
Add-WindowsFeature FS-DFS-Namespace
}
else{
log "FS-DFS-Namespace feature already installed"
}
if(!(checkifFeatureInstalled("FS-DFS-Replication"))){
log "Installing FS-DFS-Replication feature"
Add-WindowsFeature FS-DFS-Replication
}
else{
log "FS-DFS-Replication feature already installed"
}
if(!(checkifFeatureInstalled("RSAT-DFS-Mgmt-Con"))){
log "Installing RSAT-DFS-Mgmt-Con feature"
Add-WindowsFeature RSAT-DFS-Mgmt-Con
}
else{
log "RSAT-DFS-Mgmt-Con feature already installed"
}
#check for existing root on the server
if(get-dfsnroot -computerName $ComputerName | where { $_.Path -like "*\\$Domainshort\$DFSRootName-DFS*"}){
log "DFS root exist- script will now terminate"
terminate
}
log "DFS root not found- script will continue"
pause
#check if root exist in domain
$mode=$null
if(get-dfsnroot -path "\\$DomainShort\$DFSRootName-DFS" -erroraction silentlycontinue){
log "DFS root exist in domain- script will create replica"
$mode="Replica"
}
else{
log "DFS share not exist in domain - script will create new root"
$mode="Fresh"
}
#creating folder structure
if(!(test-path c:\dfs)){
log "New-Item(Creating Directory):",( New-Item -ItemType directory -path C:\DFS )
}
else{
if($multipleDFS -eq "multipleDFS"){
log "Folder c:\dfs already exist, script will continue because of multiple site DFS init"
log '*****************************************Multiple site DFS init**********************************************'
}
else{
log "Folder c:\dfs already exist, script will now terminate"
log "If you want to create another Site DFS on this server, please run the script like: .\DFSroot.ps1 SITE multipleDFS"
log "Where SITE is sitecode of DFS root that need to be created e.g: .\DFSroot.ps1 CMM multipleDFS"
terminate
}
}
if(!(test-path c:\dfs\$DFSRootName-DFS)){
log "New-Item(Creating Directory):",(New-Item -ItemType directory -path c:\dfs\$DFSRootName-DFS)
}
#checking dfsconfiguration
DFSFQDN
#creating data folder
initsite
$output = icacls C:\DFS\$DFSRootName-DFS /inheritance:r
log "ICACLS: $output"
$output = icacls C:\DFS\$DFSRootName-DFS --% /grant Administrators:(OI)(CI)F Everyone:(CI)(RX)
log "ICACLS: $output"
$output = net share $DFSRootName-DFS=C:\DFS\$DFSRootName-DFS /GRANT:'Everyone,Full'
log "Net Share: $output"
#Execute initialization if current DFS will be replica of existing one.
if($mode -eq "Replica"){
log '*************************************************************************************************************'
log '**********************************************Replica Mode***************************************************'
log "Linking DFS Root $DFSRootName-DFS"
log (New-DfsnRootTarget -TargetPath "\\$Computername\$DFSRootname-DFS" -ErrorAction SilentlyContinue)
if(get-dfsnroot -path \\$DomainShort\$DFSRootName-DFS | get-dfsnroottarget | where {$_.Targetpath -like "*$Computername*"}) {
log '*************************************************************************************************************'
log "****************************************Creating backup functionalities**************************************"
backup($DFSRootName)
success($error)
}
else{
log "DFS replica creation not successfull"
terminate
}
}
else{
log '*************************************************************************************************************'
log '**********************************************New root mode**************************************************'
log "New root, start initialize it"
log "New-DfsnRoot: " ,(New-DfsnRoot -TargetPath "\\$Computername\$DFSRootname-DFS" -Type DomainV2)
if(get-dfsnroot -path \\$DomainShort\$DFSRootName-DFS | get-dfsnroottarget | where {$_.Targetpath -like "*$Computername*"}) {
log ''
log '*************************************************************************************************************'
log "****************************************Creating backup functionalities**************************************"
log ''
backup($DFSRootName)
success($error)
}
else{
log "DFS root creation not successfull- script will now terminate"
terminate
}
}
Vinodkumarlohar
Dec 04, 2023Copper Contributor
above the script, which I have