Editing Annotations In VSphere Using PowerShell - powershell

I need to be able to edit the Annotations of VMs from powershell(ps) as will be editing many at once Image. I found "AvailibleField" after getting VM view in ps Image but not sure how to edit these. Can anyone help? Thanks :)

I think what you're looking for is the Get/Set-Annotation cmdlets: http://www.vmware.com/support/developer/PowerCLI/PowerCLI651/html/Set-Annotation.html
Example:
PS C:\Users\kruddy> Get-VM file02 | select CustomFields
CustomFields
------------
{[CompanyName, ]}
PS C:\Users\kruddy> Get-VM file02 | Get-Annotation
AnnotatedEntity Name Value
--------------- ---- -----
file02 CompanyName
PS C:\Users\kruddy> Set-Annotation -Entity (Get-VM file02) -CustomAttribute CompanyName -Value TempCorp -Confirm:$false
AnnotatedEntity Name Value
--------------- ---- -----
file02 CompanyName TempCorp
PS C:\Users\kruddy> Get-VM file02 | Get-Annotation
AnnotatedEntity Name Value
--------------- ---- -----
file02 CompanyName TempCorp
PS C:\Users\kruddy> Get-VM file02 | select CustomFields
CustomFields
------------
{[CompanyName, TempCorp]}
PS C:\Users\kruddy>

Related

Show output in two columns Powershell

I have 2 execution commands and we need to have it as a otput file (csv). The results should be shown in two columns next to each other. I am not sure how to use "format-table" or hashtables?
It gets attributes from Vmware
get-vm | get-annotation -customattribute "GrupaAktualizacji" | select value
get-vm | select name, #{N="DnsName"; E={$_.ExtensionData.Guest.Hostname}}
Below the example output
Name DnsName
---- -------
examplename example.com
and
PS C:\Windows\system32> get-vm | get-annotation -customattribute "GrupaAktualizacji" | select value
Value
-----
GWT
GW3
GW2
GW3
GW3
GW2

Repeating the server name in the Win32_Service StartName report

I am trying to figure out in Powershell how to format this input example.
Input
SERVER0040 name startname
---------- ---- ---------
AGS Cash Forecasting DOMAIN\serviceaccount
AGS CutOffTime DOMAIN\serviceaccount
AGS DL DOMAIN\serviceaccount
SERVER0042 name startname
---------- ---- ---------
AGBankImportService DOMAIN\serviceaccount
AGConfirmationDeliveryService DOMAIN\serviceaccount
AGConfirmationMatchService DOMAIN\serviceaccount
To get the output as shown below.
Server name startname
SERVER0040 AGS Cash Forecasting DOMAIN\serviceaccount
SERVER0040 AGS CutOffTime DOMAIN\serviceaccount
SERVER0040 AGS DL DOMAIN\serviceaccount
SERVER0042 AGBankImportService DOMAIN\serviceaccount
SERVER0042 AGConfirmationDeliveryService DOMAIN\serviceaccount
SERVER0042 AGConfirmationMatchService DOMAIN\serviceaccount
Thanks for any input.
Edilberto
You are not showing your code, forcing folks to guess at this. There are several ways to format output, depending on how you are getting it in the first place. A simple idea of this is shown is below.
Example:
Clear-Host
$env:COMPUTERNAME,'nwtex01','nwtiis01' |
ForEach {
Get-WmiObject -ComputerName $PSItem -Class Win32_Service |
Select-Object -Property SystemName, Name, StartName -First 3 |
Format-Table -AutoSize
}
# Results
<#
SystemName Name StartName
---------- ---- ---------
NWTDC01 adfssrv NORTHWINDTRADERS\adfsadmin
NWTDC01 AdobeARMservice LocalSystem
NWTDC01 ADSync NORTHWINDTRADERS\sqladmin
SystemName Name StartName
---------- ---- ---------
NWTEX01 AdobeARMservice LocalSystem
NWTEX01 AeLookupSvc localSystem
NWTEX01 ALG NT AUTHORITY\LocalService
SystemName Name StartName
---------- ---- ---------
NWTIIS01 AdobeARMservice LocalSystem
NWTIIS01 AJRouter NT AUTHORITY\LocalService
NWTIIS01 ALG NT AUTHORITY\LocalService
#>
This is a very common PowerShell 101 kind of thing, with many examples in the built-in help files, and blogs all over the web.

Get-ADComputer from Multiple Computers from a CSVFile

I have a csv file full of computer information formatted:
Name OS Site Code AD_Status Region Tech
computerone Windows 10 Enterprise **** Exists Chicago T T
computertwo Windows 10 Enterprise **** Exists Chicago T T
computerthree Windows 10 Enterprise **** Exists Chicago T T
I'm running a Powershell script that grabs the computer name from the csv file and checks its 'modifyTimeStamp' field.
$csvfile = Import-CSV -Path 'C:\Users\****\testexcel.csv'-Delimiter ","
$numofcompsincsv = $csvfile.psobject.properties.value[0] - 1
for ($i = 0; $i -le $numofcompsincsv; $i++) {
Get-ADComputer -identity $csvfile[$i].psobject.properties.value[0] -Properties * | FT Name, modifyTimeStamp
}
The problem with this is that it prints the computer information one by one, for example:
Name modifyTimeStamp
---- ---------------
computerone 7/19/2019 11:06:22 AM
Name modifyTimeStamp
---- ---------------
computertwo 7/24/2019 6:02:14 AM
Name modifyTimeStamp
---- ---------------
computerthree 7/24/2019 2:02:14 AM
How can I modify this so that it prints all in one like:
Name modifyTimeStamp
---- ---------------
computerone 7/19/2019 11:06:22 AM
computertwo 7/24/2019 6:02:14 AM
computerthree 7/24/2019 2:02:14 AM
Don't do this with a for loop; use the pipe instead:
Import-CSV -Path 'C:\Users\****\testexcel.csv' | Select -expand Name | Get-ADComputer -prop modifyTimeStamp | Select Name,ModifyTimeStamp
ETA: I apparently didn't create the CSV (hand-coded) I used for testing quite properly, and had originally piped the CSV to | Select Name | instead of expanding the property (which worked in my test). When I re-did it using Excel, I verified that | Select -expand Name | was required, per the comment by #js2010.

Searching for several Operating Systems with Get-ADComputer using LDAP Filter

I am using the below code to get Active Directory information with Get-ADComputer and it works fine
$computersFilter= "(&(operatingSystem=*Windows 7*)(name=*-*)(!name=V7-*)(!name=*-none)(!name=*-oncall)(!name=*-blackbaud)(!name=sc-win7-1)(!name=ut-swclient-01))"
$computers= Get-ADComputer -LDAPFilter $computersFilter -Property LastLogonDate | Select-Object Name, OperatingSystem,LastLogonDate
$computers | Select Name, LastlogonDate, OperatingSystem | Export-Csv $ServiceTagsPath -NoTypeInformation
I'd like to also retrieve computers with operating systems above Windows 7 (Windows 8, 8.1, and Windows 10) but when I change the filter like this:
(&(operatingSystem=*Windows 7*)(operatingSystem=*Windows 8*)(operatingSystem=*Windows 10*) ...
nothing is returned into the $computers variable
so what's the right way to do this?
The way you have defined $computersFilter, if you look at it's type you will notice that it is of type string. And hence it doesn't return anything.
PS C> $computersFilter = "(&(operatingSystem=*Windows7*))(name=*-*)(!name=V7-*)(!name=*-none)(!name=*-oncall)(!name=*-blackbaud)(!name=sc-win7-1)(!name=ut-swclient-01))"
PS C> $computersFilter.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
$computersFilter should be declared as an array, so that the -LDAPFilter parameter is evaluated against the collection of object, like this -
PS C>$computersFilter = #("operatingSystem=*Windows7*", "name=*-*", "!name=V7-*", "!name=*-none", "!name=*-oncall", "!name=*-blackbaud", "!name=sc-win7-1", "!name=ut-swclient-01")
PS C> $computersFilter.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array

Create Union of Multiple Tables in Powershell

I'm trying to create a union of multiple tables in Powershell to output in a user-friendly format as a report, similar to a UNION query in SQL.
I have the following code:
$ft = #{auto=$true; Property=#("MachineName", "Status", "Name", "DisplayName")}
$hosts = #("svr001", "svr002")
$report = #()
ForEach ($h in $hosts) {
$results = Get-Service -CN $h -Name MyService
$report += $results | Format-Table #ft
# Other things occur here, which is why I'm creating $report for output later.
}
Write-Output $report
The output of this code is as follows:
MachineName Status Name DisplayName
----------- ------ ---- -----------
svr001 Running MyService MyServiceDisplayName
MachineName Status Name DisplayName
----------- ------ ---- -----------
svr002 Running MyService MyServiceDisplayName
Since you simply add arrays to do a union in Powershell (i.e.,
$union = #(0, 1, 2) + #(3, 4, 5)), my initial thought was that I should
get the following output:
MachineName Status Name DisplayName
----------- ------ ---- -----------
svr001 Running MyService MyServiceDisplayName
svr002 Running MyService MyServiceDisplayName
In retrospect, I think I understand why I do not get this output, but I'm
unclear how to create a union of the two tables from the first output example into a single table as in the second, and I haven't been able to locate anything in the docs or examples online that would send me in the right direction.
Move the Format-Table to the last command. Format-* cmdlets create special format-objects that you can't work with manually so theres no point in saving them. When you save the result of Format-* to an array, you're saving the "report" which is why you get two tables in the output (array consists of two reports).
Collect the data first, then use Format-Table when you want to display the results.
$ft = #{auto=$true; Property=#("MachineName", "Status", "Name", "DisplayName")}
$hosts = #("svr001", "svr002")
$report = #()
ForEach ($h in $hosts) {
$results = Get-Service -ComputerName $h -Name MyService
$report += $results
# Other things occur here, which is why I'm creating $report for output later.
}
#Write-Output is not necessary as it is default behaviour
$report | Format-Table #ft
Sample output (used wuauserv as servicename):
MachineName Status Name DisplayName
----------- ------ ---- -----------
localhost Stopped wuauserv Windows Update
frode-pc Stopped wuauserv Windows Update
The Get-Service Cmdlet also supports an array of strings for the -ComputerName parameter. This works for me:
Get-Service -CN $hosts -Name MyService | Format-Table #ft
Sample Output using wuauserv:
MachineName Status Name DisplayName
----------- ------ ---- -----------
Tim-SRV1 Running wuauserv Windows Update
Tim-SRV2 Stopped wuauserv Windows Update