Solved

Enumerate shares on remote server and recreate on local host

Posted on 2011-03-23
6
998 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 71

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 71

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
Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 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