Solved

Enumerate shares on remote server and recreate on local host

Posted on 2011-03-23
6
1,004 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Are your AD admin tools letting you down?

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

Independent Software Vendors: 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

In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

696 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