Solved

Enumerate shares on remote server and recreate on local host

Posted on 2011-03-23
6
986 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now