• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 38
  • Last Modified:

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,
0
ITguy565
Asked:
ITguy565
  • 3
1 Solution
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now