VMware Cloud Community
thewizard954
Contributor
Contributor

Set-VM -Notes field to the current date .

Can someone assist me I'm trying to add the current date to the Notes Field in a vm using power shell script.

Get-VM -Name CHISETMCS64282 | Set-VM -Notes "$($_.Notes )" -Confirm:$false

0 Kudos
22 Replies
kwhornlcs
Enthusiast
Enthusiast

You could try this if you're just looking to add the date to existing notes:

$vm = Get-VM CHISETMCS64282

$newnote = (Get-Date).ToLongDateString() + "`r`n" + $vm.Notes

$vm|Set-VM -Notes $NewNote -Confirm:$false

0 Kudos
LucD
Leadership
Leadership

You would have to use a ForEach construct if you want to do this in a one-liner

Get-VM CHISETMCS64282 | ForEach-Object -Process {

   Set-VM -VM $_ -Notes "$((Get-Date).ToString()) $($_.Notes))" -Confirm:$false

}


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

0 Kudos
thewizard954
Contributor
Contributor

This works great but I need to append to what ever is in the notes field

0 Kudos
LucD
Leadership
Leadership

Then you need to switch the 2 substitutions in the string.

Get-VM CHISETMCS64282 | ForEach-Object -Process {

   Set-VM -VM $_ -Notes "$($_.Notes) $((Get-Date).ToString())" -Confirm:$false

}


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

0 Kudos
DD_DeenDayal
Contributor
Contributor

Hi,

 

Thanks for posting, I have some diffrent issue, I want to fill like below from a text file.
Name: ABC |
F1: XYZ |
F2: QWE |

but is is showing Name: ABC | F1: XYZ | F2: QWE |

Please suggest.
I tried both way in notepad (input file ) with and without new line.

$vm = Get-Content 'C:\Temp\vms.txt'
$notes = Get-Content 'C:\Temp\Notes.txt'


Get-VM $vm | ForEach-Object -Process {
Set-VM -VM $_ -Notes "$notes" -Confirm:$false

}

0 Kudos
LucD
Leadership
Leadership

Did you already try with

$notes = (Get-Content 'C:\Temp\Notes.txt') -join "`n"


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

0 Kudos
DD_DeenDayal
Contributor
Contributor

nopss, Did not worked.

Input file Cintaint : - Name: ABC | Field1 : xyz | Field2 :qwe 

also tried Name: ABC |  'n Field1 : xyz |  "`n" Field2 :qwe


Error :---
}
Get-Content : A parameter cannot be found that matches parameter name 'join'.
At line:2 char:46
+ $notes = Get-Content 'C:\Temp\dd\Notes.txt' -join "`n"
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-Content], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetContentCommand
---------

need output like 

Name: ABC |

Field1 : xyz |

Field2 :qwe 

0 Kudos
LucD
Leadership
Leadership

You didn't use the parenthesis around the Get-Content part


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

0 Kudos
DD_DeenDayal
Contributor
Contributor

thanks but i have to user one line( Name: ABC | Field1 : xyz |Field2 :qwe )and put the in the vm note new line after every pipe 


Name: ABC |

Field1 : xyz |

Field2 :qwe 

0 Kudos
LucD
Leadership
Leadership

From your previous entry I see in the error message that you use "n" instead of "`n".
You didn't include the back-tick


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

0 Kudos
DD_DeenDayal
Contributor
Contributor

i have 100 vms,... I  Create 2 files in one vm list and in 2nd $notes notes per line to the vm.
so 

$vm = Get-Content 'C:\Temp\dd\vms.txt'
$notes = (Get-Content 'C:\Temp\dd\Notes.txt') -join "`n"

Get-VM $vm | ForEach-Object -Process {
Set-VM -VM $_ -Notes "$notes" -Confirm:$false

}

the above code works for one vm one comment but note still in one line ... can we add something ('n or something )so in notes new line will be there.

could you suggest .

0 Kudos
LucD
Leadership
Leadership

You are still leaving out the back-tick


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

0 Kudos
DD_DeenDayal
Contributor
Contributor

1...$vm = Get-Content 'C:\Temp\dd\vms.txt'
2....$notes = (Get-Content 'C:\Temp\dd\Notes.txt') -join "`n"

3...Get-VM $vm | ForEach-Object -Process {
4...Set-VM -VM $_ -Notes "$notes" -Confirm:$false

4...}

in line 2 after -join ... it is "double cots  backtic` n "double cots 

i believe it is not showing correctly on web page.
Suggest if i am doing any mistake .

please suggest for Below issue

 

i have 100 vms,... I  Create 2 files in one vm list and in 2nd $notes notes per line to the vm.
so 

the above code works for one vm one comment but note still in one line ... can we add something ('n or something )so in notes new line will be there.

could you suggest .

 Thanks in advance

0 Kudos
LucD
Leadership
Leadership

What is in those 2 files?
Is the Notes field the same for all VMs?


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

0 Kudos
DD_DeenDayal
Contributor
Contributor

Vm file contains VM names 

vm1

vm2

vm3

and in $notes file . vm notes 
Name: ABC | Field1 : xyz |Field2 :qwe
Name: ABQWEC | Field1 : bnm |Field2 :qwe

different details in same format separated  with ( pipe )|

0 Kudos
LucD
Leadership
Leadership

And where is the link between the two files?
Is the VM name the Name: field in the notes file?


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

0 Kudos
DD_DeenDayal
Contributor
Contributor

the relation is same line no. in different file 

or can we use Excel/CSV

Vm file contains VM names 

vm1 , Name: ABC | Field1 : xyz |Field2 :qwe

vm2, Name: ABQWEC | Field1 : bnm |Field2 :qwe

vm3 ,Name: ABQWEC | Field1 : bnm |Field2 :qwe

 

0 Kudos
LucD
Leadership
Leadership

Can we agree on a single CSV file?
Something like

"VMName","Notes"

"vm1" , "Name: ABC | Field1 : xyz |Field2 :qwe"

"vm2", "Name: ABQWEC | Field1 : bnm |Field2 :qwe"

"vm3" ,"Name: ABQWEC | Field1 : bnm |Field2 :qwe"

 

 


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

0 Kudos
DD_DeenDayal
Contributor
Contributor

yes, 

but VM note on summary page should be 

DD_DeenDayal_0-1661867816940.png

 

0 Kudos