Solved

Enumerate shares on remote server and recreate on local host

Posted on 2011-03-23
6
997 Views
Last Modified: 2012-05-11
For the sake of the scenario I have the following servers:

SERVER2003
SERVER2008

What I am looking for is a script to do the following:

Enumerate shares on remote server
Recreate shares on LOCALHOST
Re-assing Share Permissions and SACL's as per original share
copy data from source shares to new shares with auditing and SACL information

That's the must do's.  The nice to have would be:

Log each activity to a log file
display user friendly output whilst running

Thanks
Glen
0
Comment
Question by:Glen Knight
6 Comments
 
LVL 70

Expert Comment

by:Chris Dent
ID: 35200261
First attempt. This isn't tested :)

Chris
<#
  Copies from source server to destination server. Destination defaults to the local machine.
#>

Param(
  [Parameter( Mandatory = $True )]
  [String]$SourceServer,

  [Parameter( Mandatory = $True )]
  [String]$DestinationServer = $Env:ComputerName,

  [String]$LogFile = "ShareCopy.log"
)

# Get the shares
Get-WmiObject Win32_Share -Filter "Type=0" -ComputerName $SourceServer | ForEach-Object {
  # Create shares on the remote server

  "$(Get-Date): Creating share: $($_.Name)" | Out-File $LogFile

  $ShareClass = [WmiClass]"\\$DestinationServer\root\cimv2:Win32_Share"
  $ShareClass.Create($_.Path, $_.Name, $_.Type)

  # Copy access rights

  "$(Get-Date): Setting security: $($_.Name)" | Out-File $LogFile

  $Security = Get-WmiObject Win32_LogicalShareSecuritySetting -Filter "Name='$($_.Name)'" -Computer $SourceServer
  $Descriptor = $Security.GetSecurityDescriptor()

  $Security = Get-WmiObject Win32_LogicalShareSecuritySetting -Filter "Name='$($_.Name)'" -Computer $DestinationServer
  $Security.SetSecurityDescriptor($Descriptor)

  # Start copying files

  "$(Get-Date): Starting file copy: $($_.Name)" | Out-File $LogFile

  xcopy "\\$SourceServer\$($_.Name)" "\\$DestinationServer\$($_.Name)" /E /C /H /K /O /X /Y
}

Open in new window

0
 
LVL 3

Expert Comment

by:AndrewSkoraro
ID: 35200294
All this is possible using PowerShell.  
0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 35200327
Added creation of the destination folder. Still assuming the drive letters are the same on source and destination.

Chris
<#
  Copies from source server to destination server. Destination defaults to the local machine.
#>

Param(
  [Parameter( Mandatory = $True )]
  [String]$SourceServer,

  [Parameter( Mandatory = $True )]
  [String]$DestinationServer = $Env:ComputerName,

  [String]$LogFile = "ShareCopy.log"
)

# Get the shares
Get-WmiObject Win32_Share -Filter "Type=0" -ComputerName $SourceServer | ForEach-Object {
  # Create the folder

  $RemotePath = "\\$DestinationServer\$($_.Path -Replace ':\\', '$')"
  If (!(Test-Path $RemotePath)) {
    New-Item $RemotePath -ItemType Directory -Force
  }

  # Create shares on the remote server

  "$(Get-Date): Creating share: $($_.Name)" | Out-File $LogFile

  $ShareClass = [WmiClass]"\\$DestinationServer\root\cimv2:Win32_Share"
  $ShareClass.Create($_.Path, $_.Name, $_.Type)

  # Copy access rights

  "$(Get-Date): Setting security: $($_.Name)" | Out-File $LogFile

  $Security = Get-WmiObject Win32_LogicalShareSecuritySetting -Filter "Name='$($_.Name)'" -Computer $SourceServer
  $Descriptor = $Security.GetSecurityDescriptor()

  $Security = Get-WmiObject Win32_LogicalShareSecuritySetting -Filter "Name='$($_.Name)'" -Computer $DestinationServer
  $Security.SetSecurityDescriptor($Descriptor)

  # Start copying files

  "$(Get-Date): Starting file copy: $($_.Name)" | Out-File $LogFile

  xcopy "\\$SourceServer\$($_.Name)" "\\$DestinationServer\$($_.Name)" /E /C /H /K /O /X /Y
}

Open in new window

0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 74

Author Comment

by:Glen Knight
ID: 35200419
Thanks Chris, I will give these a whirl tonight :)
0
 
LVL 13

Expert Comment

by:soostibi
ID: 35202842
This is my try, at the moment without logging and userfriendly output, but with whole NTFS security copying.
$remote = "member1"

$wc = New-Object wmiclass Win32_Share

Get-WmiObject -Class Win32_ShareToDirectory -ComputerName $remote | %{
    $share = if($_.share -match '"(.+)"'){$matches[1] -replace "\\\\","\"}
    $dir = if($_.sharedelement -match '"(.+)"'){$matches[1] -replace "\\\\","\"}
    New-Object -TypeName PSObject -Property @{share = $share; dir = $dir}
} | ?{$_.share -notmatch '\$$'} | %{
    if(!(test-path $_.dir)){
        New-Item -Path (Split-Path $_.dir -Parent) -Name (Split-Path $_.dir -Leaf) -ItemType directory > $null
    }
    $shpath = $_.dir -replace "^(\w):", "\\$remote\`$1`$"
    get-acl -Path $shpath | set-acl $_.dir
    $wc.Create($dir,$share,0) > $null
    $sharesec = ([WMI] "\\$remote\root\cimv2:Win32_LogicalShareSecuritySetting.Name='$share'").GetSecurityDescriptor().descriptor
    ([WMI] "\\localhost\root\cimv2:Win32_LogicalShareSecuritySetting.Name='$share'").setSecurityDescriptor($sharesec) > $null
    Get-ChildItem -Path "\\$remote\$share" | Copy-Item -Destination $dir -Recurse
    Get-ChildItem -Path "\\$remote\$share" -Recurse | get-acl | %{
        set-acl -Path ($_.path -replace "\\\\$remote\\","\\localhost\") -AclObject $_
    }
}

Open in new window

0
 
LVL 74

Author Closing Comment

by:Glen Knight
ID: 35403334
Thanks Chris this worked a treat.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Set OWA language and time zone in Exchange for individuals, all users or per database.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question