Solved

Enumerate shares on remote server and recreate on local host

Posted on 2011-03-23
6
994 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
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…

773 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