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:
- Shrink an existing partition
- Create and format new partition using the new free space
- Copy files from an existing share/drive to newly created partition
- Remove the share from existing drive(the actual drive is being shared)
- Recreate share on new drive
- Assign new drive the drive letter that was assigned to the old one.
- 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