Powershell scripting Assistance *Hyper-V Script for Guest Svc Interface*

PowerShell Scripting Assistance :

$scriptblock1 = { 
    $vMList = Get-VM
    $GuestSvcStatus = ($VMlist | Get-VMIntegrationService -Name "Guest Service Interface").enabled


    Get-VM | Select-Object -Property `
    @{n = 'Computername'; e = {$ENV:ComputerName}},
    VMName,
    @{n = 'Guest Service Interface Status'; e = {$GuestSvcStatus}},
    State
}

$cred = Get-Credential
$Serverlist = @()
$serverlist += Get-ADObject -filter * |? {$_.name -like "O2*"} |select Name -ExpandProperty name
# $serverlist += Get-ADObject -filter * |? {$_.name -like "K3*"} |select Name -ExpandProperty name
# $serverlist += Get-ADObject -filter * |? {$_.name -like "K2*"} |select Name -ExpandProperty name

$VMGuestInfo = invoke-command -Computername $serverlist -Credential $cred -ScriptBlock $scriptblock1 | Select-Object -Property * -ExcludeProperty PSComputerName, RunspaceId |
    Sort-Object -Property ComputerName, VMName
$VMGuestInfo | Out-GridView

Open in new window



This is something I should have been able to figure out but it is escaping me at the moment :

when I run this file I get the following output in the     @{n = 'Guest Service Interface Status'; e = {$GuestSvcStatus}}, section

Guest Service Interface Status : {True, True, True, True}

Can someone please show me where the error is.

Thanks,
LVL 15
ITguy565Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chirag NagrekarSystem AnalystCommented:
What you want as output ?
0
oBdACommented:
Because you're putting the status output of all machines into a single array in line 3, and then assign this array to each VM returned in line 9.
And please, please, do yourself and especially your AD a favor:
- Use the cmdlet matching the object type you want to get; in this case, Get-ADComputer instead of Get-ADObject. Prevents the cmdlet from returning too many objects.
- Filter in AD whenever possible, not in PS. More specifically: do not retrieve each and every AD object you have, only to throw most of them away in PowerShell. Very bad style. Really.
$scriptblock1 = {
	Get-VM | Select-Object -Property `
		@{n='Computername'; e={$ENV:ComputerName}},
		VMName,
		@{n='Guest Service Interface Status'; e={(Get-VMIntegrationService -VM $_ -Name 'Guest Service Interface').Enabled}},
		State
}

$cred = Get-Credential
$serverList = @()
$serverList += Get-ADComputer -Filter "name -like 'O2*'" | Select-Object -ExpandProperty name
# $serverList += Get-ADComputer -Filter "name -like 'K3*'" | Select-Object -ExpandProperty name
# $serverList += Get-ADComputer -Filter "name -like 'K2*'" | Select-Object -ExpandProperty name

$VMGuestInfo = Invoke-Command -Computername $serverList -Credential $cred -ScriptBlock $scriptblock1 |
	Select-Object -Property * -ExcludeProperty PSComputerName, RunspaceId |
	Sort-Object -Property ComputerName, VMName
$VMGuestInfo | Out-GridView

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ITguy565Author Commented:
Because you're putting the status output of all machines into a single array in line 3, and then assign this array to each VM returned in line 9.
And please, please, do yourself and especially your AD a favor:
- Use the cmdlet matching the object type you want to get; in this case, Get-ADComputer instead of Get-ADObject. Prevents the cmdlet from returning too many objects.
- Filter in AD whenever possible, not in PS. More specifically: do not retrieve each and every AD object you have, only to throw most of them away in PowerShell. Very bad style. Really.


oBdA,

Thanks for the assistance with this yet again, you have actually taught me ALOT about scripting since I started to "actively" script at the beginning of this year.

Very good advise about the
get-adobject -filter *


Command, I tend to forget to do that and it causes my scripts to be alot less efficient when running on remote machines than they should.

Thanks again, the output was exactly what I as looking for.
0
ITguy565Author Commented:
oBdA,

Not sure if you would have a use for this, but here is my final script for modifying Hyper-V integration components :

$scriptblock1 = {
    $VMlist = Get-VM
    $VMlist | Select-Object -Property `
    @{n = 'Computername'; e = {$ENV:ComputerName}},
    VMName,
    @{n = 'Guest Service Interface Status'; e = {(Get-VMIntegrationService -VM $_ -Name 'Guest Service Interface').Enabled}},
    State
}

$scriptblock2= {
$VMlist = Get-VM
foreach ($VM in $VMlist.VMName){Enable-VMIntegrationService -Name "Guest Service Interface" -VMName $VM}
$VMlist | Select-Object -Property `
    @{n = 'Computername'; e = {$ENV:ComputerName}},
    VMName,
    @{n = 'Guest Service Interface Status'; e = {(Get-VMIntegrationService -VM $_ -Name 'Guest Service Interface').Enabled}},
    State
}


$cred = Get-Credential
$serverList = @()
$serverList += Get-ADComputer -Filter "name -like 'O1*'" | Select-Object -ExpandProperty name
# $serverList += Get-ADComputer -Filter "name -like 'K3*'" | Select-Object -ExpandProperty name
# $serverList += Get-ADComputer -Filter "name -like 'K2*'" | Select-Object -ExpandProperty name

$VMGuestInfo = Invoke-Command -Computername $serverList -Credential $cred -ScriptBlock $scriptblock1 |
    Select-Object -Property * -ExcludeProperty PSComputerName, RunspaceId |
    Sort-Object -Property ComputerName, VMName

$VMGuestInfo2 = Invoke-Command -Computername $serverList -Credential $cred -ScriptBlock $scriptblock2 |
    Select-Object -Property * -ExcludeProperty PSComputerName, RunspaceId |
    Sort-Object -Property ComputerName, VMName

$GSIOriginalState = $VMGuestInfo |select VMName,"Guest Service Interface Status"
$GSIAlteredState = $VMGuestInfo2 |select VMName,"Guest Service Interface Status"

$StateCheck = $GSIOriginalState.VMName |select -Property `
@{n = 'VM Name'; e = {($_ )}},
@{n = 'Original GSI Status'; e = {($GSIOriginalState."Guest Service Interface Status")}},
@{n = 'Altered GSI Status'; e = {($GSIAlteredState."Guest Service Interface Status")}}

$StateCheck | Out-GridView
#$GSIOriginalState | Out-GridView
#$GSIAlteredState | Out-GridView
#$VMGuestInfo | Out-GridView

Open in new window


The script modifies the status of the Guest Service Interface Hyper-V integration component and then logs the original and altered state of the item.
0
ITguy565Author Commented:
Probably not the most efficient code but it gets the job done.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.