ITguy565
asked on
Powershell scripting Assistance *Hyper-V Script for Guest Svc Interface*
PowerShell Scripting Assistance :
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,
$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
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,
What you want as output ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
ASKER
oBdA,
Not sure if you would have a use for this, but here is my final script for modifying Hyper-V integration components :
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.
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
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.
ASKER
Probably not the most efficient code but it gets the job done.