2

I am working to move some files from one physical drive to another in large group of Windows 2012 servers. The drive letters and sizes are all the same on these servers. Basically we have a share on each server and I want to move that data and share over to another drive that has more free space. I'm trying to develop a script to accomplish the following:

  1. Shrink an existing partition
  2. Create and format new partition using the new free space
  3. Copy files from an existing share/drive to newly created partition
  4. Remove the share from existing drive(the actual drive is being shared)
  5. Recreate share on new drive
  6. Assign new drive the drive letter that was assigned to the old one.
  7. Log any failures

This is the code I have put together. As the title states, I can't get the New-SMBShare to run correctly in this script. I'm not getting an errors but the drive is not shared when the script completes. I am attempting to execute the script from within the Powershell ISE and if I run the same command on it's own, the share is created as expected. What would cause the command to fail when run within the script.

I'm also occasionally getting the following: Format-Volume : Cannot perform the requested operation when the drive is read only I get this every once in a while when I run this so I'm not sure why it happens intermittently. It almost seems like sometimes the previous command hasn't completed before the formatting command is run.

Finally I'm also not really sure the best way to accomplish logging any errors or even a success message. I've considered using Robocopy instead of Copy-Item for this reason but if anyone knows a good way to accomplish this with Copy-Item, I'd like to here it.

 #Stopping service to prevent "Format Disk" dialog box from appearing on screen
 Stop-Service ShellHWDetection 
 #Create New 90GB Partition from W Drive
 Resize-Partition -DriveLetter W -Size (1772GB)
 Get-Disk | where size -GT 1.5TB | New-Partition -UseMaximumSize -DriveLetter K
 Get-Partition -DriveLetter K | Format-Volume -FileSystem NTFS  -NewFileSystemLabel "PC_Images" -Confirm:$false
 #Restart ShellHWDetection service
 start-service ShellHWDetection
 #Copy files from "I drive" PC_Images share to new partition
 Copy-Item -Path I:\* -Destination K:\ -Recurse -erroraction silentlycontinue
 #Remove existing PC_Images share and recreate on new partition
 Get-SMBShare -Name PC_Images | Remove-SMBShare -Confirm:$False
 New-SMBShare -Name PC_Images -Path K:\ -ChangeAccess Everyone
 #Change drive letter on new share to I
 Set-Partition -DriveLetter I -NewDriveLetter X
 Set-Partition -DriveLetter K -NewDriveLetter I

Any help would be appreciated

YEMyslf
  • 121
  • 3

2 Answers2

0

I figured out the issue on my primary question. My script changes the drive letter AFTER I created the share which removes the share. If I change the drive letter before creating the share it works as desired. I'm still unsure why I am periodically getting the read only error though.

YEMyslf
  • 121
  • 3
0

I ran into a similar issue with the read only nonsense and resolved it with something like the following (you can pipe New-Partition directly to Format-Volume and omit Get-Partition):

New-Partition … | ForEach-Object { Start-Sleep -s 3; $_ | Format-Volume … }
Bink
  • 193
  • 5