The script below:
Connect-HVServer -server server.com
$ViewAPI = $global:DefaultHVServers[0].ExtensionData
$desktops = Get-Content -Path "C:\Path\vmlist.txt"
get-hvmachine -MachineName $desktops
foreach ($desktop in $desktops){
If($desktop.base.user) {
$user = ($ViewApi.AdUserOrGroup.AduserOrGroup_Get($desktop.base.user)).base.LoginName
$emailaddress = ''
try{get-aduser $user -erroraction stop | out-null}
catch [Exception] {$emailaddress = 'Account not found'}
if ($emailaddress -ne 'Account not found') {$emailaddress = (get-aduser $user -Properties * | select emailaddress).emailaddress}
}
Else {
$User = ' '
$emailaddress = ''
}
$Assignments += [PSCUSTOMOBJECT] @{
Horizon = $server
Desktop = $desktop.base.name
State = $desktop.base.BasicState
User = $User
EmailAddress = $emailaddress
}
}
Fails with the following error message when run:
Get-HVMachine : Cannot process argument transformation on parameter 'MachineName'. Cannot convert value to type System.String.
At C:\Scripts\WIP-Horizon-GetUsers-Email-Addresses.ps1:5 char:32
+ get-hvmachine -MachineName $desktops
+ ~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-HVMachine], ParameterBindingArgumentTransformationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-HVMachine
Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.
At C:\Scripts\WIP-Horizon-GetUsers-Email-Addresses.ps1:18 char:21
+ $Assignments += [PSCUSTOMOBJECT] @{
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.
At C:\Scripts\WIP-Horizon-GetUsers-Email-Addresses.ps1:18 char:21
+ $Assignments += [PSCUSTOMOBJECT] @{
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.
At C:\Scripts\WIP-Horizon-GetUsers-Email-Addresses.ps1:18 char:21
+ $Assignments += [PSCUSTOMOBJECT] @{
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.
At C:\Scripts\WIP-Horizon-GetUsers-Email-Addresses.ps1:18 char:21
+ $Assignments += [PSCUSTOMOBJECT] @{
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Please, I need help with two things:
1) identify what's causing the error message.
2) Enhance the script so it exports out the results in a CSV or Excel formatted report if possible. Thank you.
You should put the "get-hvmachine -MachineName $desktops" inside foreach loop, get-hvmachine accepts only one machine, and not many, which i gues you have in $desktops variable.
And once in foreach pool, remove that "s" from variable: get-hvmachine -MachineName $desktop
You should put the "get-hvmachine -MachineName $desktops" inside foreach loop, get-hvmachine accepts only one machine, and not many, which i gues you have in $desktops variable.
And once in foreach pool, remove that "s" from variable: get-hvmachine -MachineName $desktop
Also, you didn't declare $Assigments as an array.
$Assignments = @()
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you @LucD . Made the tweaks you suggested and although I'm no longer receiving the error messages, the output result is not getting anything. Something to do with the search parameters. The resulting CSV file has the headings but no desktop info there and this is displayed on the screen:
Get-HVMachine: No Virtual Machine(s) Found with given search parameters
This is the updated script:
Connect-HVServer -server server.com
$ViewAPI = $global:DefaultHVServers[0].ExtensionData
$desktops = Get-Content -Path "C:\Path\vmlist.txt"
$output = foreach ($desktop in $desktops){
get-hvmachine -MachineName $desktop
If($desktop.base.user) {
$user = ($ViewApi.AdUserOrGroup.AduserOrGroup_Get($desktop.base.user)).base.LoginName
$emailaddress = ''
try{get-aduser $user -erroraction stop | out-null}
catch [Exception] {$emailaddress = 'Account not found'}
if ($emailaddress -ne 'Account not found') {$emailaddress = (get-aduser $user -Properties * | select emailaddress).emailaddress}
}
Else {
$User = ' '
$emailaddress = ''
}
New-Object -TypeName PSObject -Property ([ordered]@{
Horizon = $server
Desktop = $desktop.base.name
State = $desktop.base.BasicState
User = $User
EmailAddress = $emailaddress
})
}
$output | Export-Csv -Path "C:\Path\vmlist.csv" -NoTypeInformation -UseCulture
Bonjour, avez-vous réussi à résoudre le problème ?
No not yet. Need to handle other things in the meantime but I'll certainly post when I do. I'll mark Luc's answer as correct though because the initial issue was resolved based on them.
@BBB36can you show us the content of "C:\Path\vmlist.txt", at least one line, and it is also only an Column or more of them? And which version of Horizon Connection Server are you running?
In regard to making my post as Solution, thanks but it is actually from me and LucD together.