Hi,
I am not able to get the server and member as it is generating blank
There is no error after the script is run but the output shows blank for Server and Members
Please help
Script
connect-viserver 10.10.10.10
Get-VM | Where {$_.PowerState -eq 'PoweredOn' -and $_.Guest.OSFullName -match 'Windows'} | Select @{N="Folder"; E={$_.Folder.Name}}, @{N="VM"; E={$_.Name}}, @{N="IP_Address";E={@($_.guest.IPAddress[0])}}, @{N="OS"; E={@($_.guest.OSFullName)}} | Export-Csv -Path .\WinOS_Info.csv -NoTypeInformation -UseCulture
disconnect-viserver -server * -force -confirm:$false
$date = Get-Date -format dd-MMM-yyyy
$reportlocation = ".\WinOS_Info_$date.xlsx"
Import-Csv -Path .\WinOS_Info.csv -UseCulture -PipelineVariable row |
ForEach-Object -Process {
try
{
$group =[ADSI]"WinNT://$server.VM/Administrators"
$members = $group.Members() | foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null) }
$result += New-Object PsObject -Property @{
Server = $($server.VM)
Members = $members -join ";"
}
}
catch
{
$result = 'False'
}
$row | Add-Member -MemberType NoteProperty -Name 'Server' -Value $($server.VM) -PassThru | Add-Member -MemberType NoteProperty -Name 'Members' -Value $members -PassThru
} | Export-Excel -Path $reportlocation
Output
That should say $group =[ADSI]"WinNT://$($row.VM)/Administrators"
This is substitution in a string, then you need this notation when referencing a property of an object.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Where is the $server variable coming from and what is supposed to be in there?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
$server variable is nothing but the VM Name, I just wanted to get the same VM name in the sheet again, so that to ensure i am getting the members information from correct server.
But you're not assigning a value to that variable anywhere in the script.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I guess, I have assigned, is this correct ?
Server = $($server.VM)
Members = $members -join ";"
There you are creating the new object, but where do you assign what is in $server?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
I want to get $server and $member details in the output file
$row | Add-Member -MemberType NoteProperty -Name 'Server' -Value $($server.VM) -PassThru | Add-Member -MemberType NoteProperty -Name 'Members' -Value $members -PassThru
There is no problem assigning a value, my question is just, where do you assign something to the variable $server.
If you don't, then this will not work, since $server by default contains $null.
You need to have a $server = ... somewhere in your script, before you actually use the variable
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
I have a .csv file which has below details and $server.VM has get input from that file.
contents of the file as below
"Folder","VM","IP_Address","OS"
"Dev","app09","192.27.3.76","Microsoft Windows Server 2012 (64-bit)"
I understand that, but on the Import-Csv you defined the pipelinevariable as $row.
While the $server variable is nowhere assigned a value in your script.
Change $server to $row.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
As suggested, I have changed $server to $row, still server and members shows blank
$reportlocation = ".\Local_Admin_Accounts_date.xlsx"
Import-Csv -Path .\WinOS_Info.csv -UseCulture -PipelineVariable row |
ForEach-Object -Process {
try
{
$group =[ADSI]"WinNT://$row/Administrators"
$members = $group.Members() | foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null) }
$result += New-Object PsObject -Property @{
Server = $row
Members = $members -join ";"
}
}
catch
{
$result = 'False'
}
$row | Add-Member -MemberType NoteProperty -Name 'Server' -Value $row -PassThru | Add-Member -MemberType NoteProperty -Name 'Members' -Value $members -PassThru
} | Export-Excel -Path $reportlocation -AutoFilter -AutoSize -BoldTopRow -FreezeTopRow -WorksheetName Summary
Now you changed $($server.VM) to $row.
That should be $($row.VM)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
After I made the changes, I am getting error
$reportlocation = ".\Local_Admin_Accounts_$date.xlsx"
Import-Csv -Path .\WinOS_Info.csv -UseCulture -PipelineVariable row |
ForEach-Object -Process {
$group =[ADSI]"WinNT://$($row.VM)/Administrators"
$members = $group.Members() | foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null) }
$result += New-Object PsObject -Property @{
Server = $($row.VM)
Members = $members -join ";"
}
}
$row | Add-Member -MemberType NoteProperty -Name 'Server' -Value $($row.VM) -PassThru | Add-Member -MemberType NoteProperty -Name 'Members' -Value $members -PassThru | Export-Excel -Path $reportlocation
Error :
Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.
At D:\myGet_Local_Administators_3.ps1:22 char:15
+ Server = $($row.VM)
+ ~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Add-Member : Cannot bind argument to parameter 'InputObject' because it is null.
At D:\my\Get_Local_Administators_3.ps1:26 char:8
+ $row | Add-Member -MemberType NoteProperty -Name 'Server' -Value $($r ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-Member], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand
What happens when you change that to Server = $row.VM
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
Now I am able to get the server name after changing as below but member field still shows blank.
Import-Csv -Path .\WinOS_Info.csv -UseCulture -PipelineVariable row |
ForEach-Object -Process {
try
{
$group =[ADSI]"WinNT://$row.VM/Administrators"
$members = $group.Members() | foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null) }
$result += New-Object PsObject -Property @{
Server = $($row.VM)
Members = $members -join ";"
}
}
catch
{
$result = 'False'
}
$row | Add-Member -MemberType NoteProperty -Name 'Server' -Value $($row.VM) -PassThru | Add-Member -MemberType NoteProperty -Name 'Members' -Value $($members -join ";") -PassThru
} | Export-Excel -Path $reportlocation
That should say $group =[ADSI]"WinNT://$($row.VM)/Administrators"
This is substitution in a string, then you need this notation when referencing a property of an object.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you very much LucD.
that worked perfectly