VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Unable to validate and create a folder in Datastore

Hi,

I am Unable to validate and create a folder in Datastore for below, as I am getting the below error.

I need to create a folder by using esx host name under "MyDatastore/log"

Example : "MyDatastore/logs/esx20"

Please help!!

$myhost = "esx20.corp.loc"
$mylog = $myhost.Trim(".corp.loc")
$datastore = Get-Datastore -Name "MyDatastore"
$directoryPath = New-PSDrive -Location $datastore -Name DS -PSProvider VimDatastore -Root "\logs"
$directoryPathForLog = $directoryPath+"\"+"$mylog"
if(!(Test-Path -path $directoryPathForLog))
{
New-Item -ItemType directory -Path DS:\$directoryPathForLog
Remove-PSDrive -Name DS -Confirm:$false
Write-Host "Folder path has been created successfully at: " $directoryPathForLog
}
else
{
Write-Host "The given folder path $directoryPathForLog already exists";
}

Error:

Method invocation failed because [VMware.VimAutomation.ViCore.Cmdlets.Provider.DatastoreProvider.DatastoreDriveInfo] does not contain a method named 'op_Addition'.
At D:\Check_Log_Folder_Exisits.ps1:5 char:1
+ $directoryPathForLog = $directoryPath+"\"+"$mylog"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound

Test-Path : Cannot bind argument to parameter 'Path' because it is null.
At D:\Check_Log_Folder_Exisits.ps1:6 char:22
+ if(!(Test-Path -path $directoryPathForLog))
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Test-Path], ParameterBindingValidationException

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Ok, if I understand correctly what you are trying to do, the following should work.

$myhost = "esx20.corp.loc"
$mylog = $myhost.Trim(".corp.loc")
$datastore = Get-Datastore -Name "MyDatastore"

$directoryPath = New-PSDrive -Location $datastore -Name DS -PSProvider VimDatastore -Root '\'
$newFolder = "DS:\logs\$($mylog)"

if (!(Test-Path -Path $newFolder)) {
  New-Item -ItemType directory -Path $newFolder
  Remove-PSDrive -Name DS -Confirm:$false
  Write-Host "Folder path has been created successfully at: " $directoryPathForLog
} else {
  Write-Host "The given folder path $newFolder already exists";
}

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Try with

$directoryPathForLog = "$($directoryPath.Root)\$($mylog)"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

When I execute, it is creating under complete new folder path 

But I would like to create check and create folder called esx20 (based on the VMhost), if that doesn't exist under Mydatastore\logs folder

Current Output

PS D:\> .\Check_Log_Folder_Exisits.ps1

Name Type Id
---- ---- --
esx20 DatastoreFolder
Folder path has been created successfully at: \esx20.corp.loc@443\ha-datacenter\Mydatastore\logs\esx20

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, if I understand correctly what you are trying to do, the following should work.

$myhost = "esx20.corp.loc"
$mylog = $myhost.Trim(".corp.loc")
$datastore = Get-Datastore -Name "MyDatastore"

$directoryPath = New-PSDrive -Location $datastore -Name DS -PSProvider VimDatastore -Root '\'
$newFolder = "DS:\logs\$($mylog)"

if (!(Test-Path -Path $newFolder)) {
  New-Item -ItemType directory -Path $newFolder
  Remove-PSDrive -Name DS -Confirm:$false
  Write-Host "Folder path has been created successfully at: " $directoryPathForLog
} else {
  Write-Host "The given folder path $newFolder already exists";
}

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

It worked....Thank you very much 🙂

0 Kudos