I need to test newly added datastores to hosts to ensure the datastores have read-write permissions. In the past I have been burnt when the storage guys gave me a new LUN to attach to a host so that I could perform a VM migration. When I ran the VM migration, I found the datastore was in READ ONLY mode and I could not continue my migration.
I thus have written a function as shown here, to create a folder on a newly presented datastore:
#
# Function to test read/write properties of a datastore by
# creating a folder on the new datastore then delete the folder.
Function CheckDSReadWriteOK
{
Param(
[STRING]$HostObject = '',
[STRING]$DatastoreName = '',
[STRING]$RemotePath = ''
)
# Select host and datastore
$oDatastore = Get-Datastore -VMHost $HostObject -Name $DatastoreName
$NewPSDrive = New-PSDrive -Location $oDatastore -Name DS -PSProvider VimDatastore -Root "\"
$NewPSFolder = New-Item -Path DS:\ISO -ItemType Directory
$RemovePSDrive = Remove-PSDrive -Name DS -Confirm:$false
}
The function creates the folder "\ISO" on the datastore, I can see it in the vCenter web GUI and on the host via SSH.
My problem is that the Remove-PSDrive is not working. It still appears in the vCenter web GUI and it still shows in the SSH session.
Here's what I've got in the variables:
$oDatastore
Name FreeSpaceGB CapacityGB
---- ----------- ----------
MyDatastore 3,276.799 3,276.800
$NewPSDrive | ft -AutoSize
Name Used (GB) Free (GB) Provider Root CurrentLocation
---- --------- --------- -------- ---- ---------------
DS VimDatastore \MyvCenter@443\DC\MyDatastore
$NewPSFolder
Name Type Id
---- ---- --
ISO DatastoreFolder
When the Remove-PSDrive command runs, the variable $RemovePSDrive is empty.
If I run the command from the command line twice, it seems to be doing something because the 1st time it runs, there's a blank response and 2nd time it runs it errors:
[DBG]: PS C:\Users\jmilano\Documents\PowerShell_Scripts\Datastores>> Remove-PSDrive -Name DS -Confirm:$false
[DBG]: PS C:\Users\jmilano\Documents\PowerShell_Scripts\Datastores>> Remove-PSDrive -Name DS -Confirm:$false
Remove-PSDrive : Cannot find drive. A drive with the name 'DS' does not exist.
At line:1 char:1
+ Remove-PSDrive -Name DS -Confirm:$false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (DS:String) [Remove-PSDrive], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemovePSDriveCommand
[DBG]: PS C:\Users\jmilano\Documents\PowerShell_Scripts\Datastores>>
Remove-PSDrive can only remove a drive create using the New-PSDrive with the "-Persist" switch set.
Without the -Persist the PSDrive is temporary and would be removed automatically when the session ends.
Remove-PSDrive removes all drives connected through New-PSDrive, not only the ones created with the Persist switch.
The Remove-PSDrive cmdlet does not return any output, so it is normal the $RemovePSDrive variable is empty.
When you executed the Remove-PSDrive cmdlet twice from the PS prompt you see the expected output.
The 1st time the PSDrive is removed and no output is produced.
The 2nd time there no PSDrive and you get the error message.
The fact that the variables $NewPSDrive and $NewPSFolder still contain the original content is normal.
The Remove-PSDrive cmdlet does not empty those variables.
Not sure what you mean by
"My problem is that the Remove-PSDrive is not working. It still appears in the vCenter web GUI and it still shows in the SSH session."
Do you mean the ISO folder you created?
That is normal.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sorry for any confusion. Let me explain better.
Further up in my code there is a section which adds the datastore to the host. Sometimes the storage guys forget to make the underlying LUN writable, so it is presented to the host as a read-only datastore. Then when I run my VM storage migration during my change window, I find the new datastores are read-only and I have to scramble to get a storage resource to fix it. We are talking about replacing 50 datastores on many vCenters so it needs to be automated.
The other part of the script will add the new datastores to each respective hosts, and it should test that the datastore is writable, by creating a test folder and then deleting the folder from the datastore.
I found the issue. It's not about removing the PSDrive, it was more about removing the folder from the datastore after it was created, and I found that Remove-Item is what I needed. So here's the resulting, working script, which tests if a datastore is writable:
#
# Function to test read/write properties of a datastore by
# creating a folder on the new datastore then delete the folder.
Function CheckDSReadWriteOK
{
Param(
[STRING]$HostObject = '',
[STRING]$DatastoreName = '',
[STRING]$RemotePath = ''
)
Try
{
$TestDatastoreFolderName = "DS:\NEW_FOLDER"
# Get the new datastore.
$oDatastore = Get-Datastore -VMHost $HostObject -Name $DatastoreName
# Ensure we get a catch-able error if something goes wrong.
$ErrorActionPreference = "Stop"
# Create the drive connection.
$NewPSDrive = New-PSDrive -Location $oDatastore -Name DS -PSProvider VimDatastore -Root "\"
# Create the test folder. If this fails, the datastore is most-likely in read-only mode.
$NewPSFolder = New-Item -Path $TestDatastoreFolderName -ItemType Directory
# If all went well, remove the test folder from the datastore.
$RemovePSFolder = Remove-Item -Path $TestDatastoreFolderName -Confirm:$false
# Remove the drive connection
$RemovePSDrive = Remove-PSDrive -Name DS -Confirm:$false
$ErrorActionPreference = “Continue”
}
Catch
{
Write-Host "$StringText_DS $StringText_Host- The datastore is NOT writeable." -ForegroundColor Red
}
}
Thank you all for your help.
@LucD wrote:Remove-PSDrive removes all drives connected through New-PSDrive, not only the ones created with the Persist switch.
Ah! I see from https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/remove-psdrive?v...
"Beginning in Windows PowerShell 3.0, Remove-PSDrive also disconnects mapped network drives, including, but not limited to, drives created by using the Persist parameter of New-PSDrive."
So its been that way for quite a while. Well, every day is a learning day!