Hi gurus.
I have a few folders within a datastore that I would like to rename to "foldername_Old" , I have quite a few of this folders so I was thinking of reading a text file and then match the name and then rename the folders adding the "_Old", is this possible using PowerShell CLI.
has anyone done this before that can help me
I am very new to this CLI but learning little by little so go easy on me.
The 1st line is indeed the headers line, and needs to be included in the CSV.
This allows the script to refer to fields in the other lines with that name.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Someone may just give you one, but if you want to learn and try to get it to work look at
http://www.beaconitservices.com/blog/2014/04/powershell-bulk-administration-using-csv-imports/
this will show you how to import a csv and do something foreach row. Then look at VMware vSphere 5.1 it says 5.1 but it is still valid. There is an example
Get-Datastore -Name Datastore1 | Set-Datastore -Name Datastore2
Here is a link for installing powercli
https://www.michellelaverick.com/2018/04/setting-up-vmware-powercli-10-x/
SJ.
Thanks a bunch for jumping in.- I have so far the get-datastore down, I will look into the foreach you mention, but I don't want to rename the datastore but a few folders within.
I know there has to be a better way as I am doing this at this time manually and it sucksssssssssss.
Thanks a bunch and hope someone else pitches in with a script also as well.
Thanks a bunch
Just change it from Get-Datastore and Set-Datastore to Get-Folder and Set-Folder. The rename part could be something like
Get-Folder -Name $_.FolderName | Set-Folder -Name "$($_.FolderName)_old"
So the whole thing would be
$folders=Import-CSV C:\folders.csv
foreach($folder as $folders)
{
Get-Folder -Name $_.FolderName | Set-Folder -Name "$($_.FolderName)_old"
}
Then in the csv just put Folder Name as the first row, and every folder underneath it.
Sorry thats wrong the whole things would be
Import-Csv "C:\folders.csv" -UseCulture | %{
Get-Folder -Name $_.FolderName | Set-Folder -Name "$($_.FolderName)_old"
}
SJ.
Thanks again
but how do I go searching folder inside a datastore because when I use the get-folder I get the folder on vsphere infrastructure and not the ones within the datastore when you browse the datastore.
I did this but it did not work
Get-datastore -name "my_datastore_name" | get-folder
but did not do anything but show me the datastore mY_datastore_name" name no folder within
Thanks
What are you trying to do, I was thinking you know the folders you want to rename to _old, thats what I have. You said
" have quite a few of this folders so I was thinking of reading a text file and then match the name"
my method has you place each of those folder names in one column in a csv file and the script I references renames all of those to _old
sj.
Yes and I am sorry for the confusion, I thought I stated that the folders in question are within the datastore.
I have a datastore and within that datastore, I have folders for each VM within that datastore that I would like to rename.
so datastore
folder VM1
files vmdk
vmx
so I am trying to rename VM1 with is a folder within the datastore that contain the VM files
Thanks again and sorry for the confusion and hope I make sense now
Since you want to rename folders on a datastore, you can use the VimDatastore provider.
Try something like this
#
# Datastore,Folder
# DS1,Folder1
# DS1,Folder2
# DS2,Folder77
foreach($line in Import-Csv -Path .\folders.csv -UseCulture){
$ds = Get-Datastore -Name $line.Datastore
New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" > $null
Rename-Item -Path "DS:$($line.Folder)" -NewName "$($line.Folder)_old"
Remove-PSDrive -Name DS -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD
Thanks a bunch for the lines, just one question on the File to be imported, is the first row header "Datastore ,Folder "or that is the description of what DS1,Folder1 is what you are describing.
Thanks again a bunch, testing tomorrow
Thanks again both of you gurus
The 1st line is indeed the headers line, and needs to be included in the CSV.
This allows the script to refer to fields in the other lines with that name.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD
worked !!!!! thanks a bunchhhh you too sj
and I guess when I want to rename them back I just change the CSV file to show all of them "_old" and change the following line
Rename-Item -Path "DS:$($line.Folder)" -NewName "$($line.Folder)_old" TO
Rename-Item -path "DS:$($line.Folder)" -NewName "$($line.Folder)" ???? how do I strip the "_old" that is on the CSV and rename the Folder back to what it was NO "_OLD" at the end??
Thanks again, last question on this regard
Try like this
Rename-Item -path "DS:$($line.Folder)" -NewName "$($line.Folder.Replace('_old',''))"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference