Hi Guys
Im having trouble with the following code, not because it doesnt work, but because regardless of whether different parts of the try statement are successful or not, I cannot get all the columns I want in one go.
So if for example $CurrentPageFile doesnt exist on one server, but when I then try a different WMI query, the table header doesnt display for the relevant select command to pull off the relevant details.
Its a bit annoying because I need all of it for each specific server. I would rather it produces all 5 colums initially, then populates the relevant ones with data if it has it available to display on screen.
if ($VM.Guest.Hostname -ne $VM.Name)
{
$username = Remote-AuthenticationCalculator -VM $VM -AdminAccount $AdminAccount
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $username, $password
try
{
$CurrentPageFile = Get-WmiObject -ComputerName $VM -Query "select * from Win32_PageFileSetting" -Credential $cred -ErrorAction Stop
$CurrentPageFile | Select __Server, Name, InitialSize, MaximumSize
if (!$CurrentPageFile)
{
$CurrentPageFile = Get-WmiObject -ComputerName $VM -Query "select * from Win32_PageFileUsage" -Credential $cred -ErrorAction Stop
$CurrentPageFile | select __Server, Name, AllocatedBaseSize
}
}
catch
{
$message1 = [String]::Format("`nVM: {0}{1}{0} could not be contact remotely using ""Get-WmiObject""`n`nSkipping pagefile interrogation...`n", $quote, $VM.Guest.Hostname)
Write-Host $message1 -BackgroundColor Black -ForegroundColor Red
}
}
Thanks everyone
The properties of the first output record will define what you will see. So, producing all 5 columns initially will work. You can also change the select statement on both places into
$CurrentPageFile | Select __Server, Name, InitialSize, MaximumSize, AllocatedBaseSize
This has to do with the PowerShell output engine.
After the first select, the PS Output engine assumes that the next output will follow the same layout, hence the display issues you are seeing.
There are a couple of options
1) Tell the Output engine to look again.
This can be done by piping the output from each Select to the Out-Default cmdlet
$CurrentPageFile | select __Server, Name, AllocatedBaseSize | Out-Default
2) The solution you already thought about.
Make sure the output is the same in all cases. In other words have all columns present, but just fill the ones that you have data for.
Note, this will also make it a lot easier should you want to export the data to a CSV file
if ($VM.Guest.Hostname -ne $VM.Name)
{
$username = Remote-AuthenticationCalculator -VM $VM -AdminAccount $AdminAccount
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $username, $password
$line = New-Object PSCustomObject -Property @{
Server = ''
Name = ''
InitialSize = ''
MaximumSize = ''
AllocatedBaseSize = ''
}
try
{
$CurrentPageFile = Get-WmiObject -ComputerName $VM -Query "select * from Win32_PageFileSetting" -Credential $cred -ErrorAction Stop
$line.Server = $CurrentPageFile.__Server
$line.Name = $CurrentPageFile.Name
$line.InitialSize = $CurrentPageFile.InitialSize
$line.MaximumSize = $CurrentPageFile.MaximumSize
if (!$CurrentPageFile)
{
$CurrentPageFile = Get-WmiObject -ComputerName $VM -Query "select * from Win32_PageFileUsage" -Credential $cred -ErrorAction Stop
$line.AllocatedBaseSize = $CurrentPageFile.AllocatedBaseSize
}
}
catch
{
$message1 = [String]::Format("`nVM: {0}{1}{0} could not be contact remotely using ""Get-WmiObject""`n`nSkipping pagefile interrogation...`n", $quote, $VM.Guest.Hostname)
Write-Host $message1 -BackgroundColor Black -ForegroundColor Red
}
$line
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for that valuable tidbit :smileygrin:
The problem I have now is that it looks like this:
__SERVER | :Server1 |
Name | : C:\pagefile.sys |
InitialSize | : |
MaximumSize | : |
AllocatedBaseSize : 4096
__SERVER | :Server2 |
Name | : s:\pagefile.sys |
InitialSize | : 4000 |
MaximumSize | : 4084 |
AllocatedBaseSize :
Is it possible to change it to the way it was so that it appears kinda an excel table with the columns at the top and the data populating the cells below? Right now it looks more like a hash table
Thanks LucD :smileygrin:
I will test that now :smileygrin:
I get the following error after I add all that code:
ERROR: Cannot find type [PSCustom]: verify that the assembly containing this type is loaded.
Oops, should be PSCustomObject
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I have had to tweak the script based on what you have provided so it looks kinda like this:
if ($VM.Guest.Hostname -ne $VM.Name)
{
$username = Remote-AuthenticationCalculator -VM $VM -AdminAccount $AdminAccount
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $username, $password
$line = New-Object PSCustomObject -Property @{
Server = ''
Name = ''
InitialSize = ''
MaximumSize = ''
AllocatedBaseSize = ''
}
try
{
$CurrentPageFile = Get-WmiObject -ComputerName $VM -Query "select * from Win32_PageFileSetting" -Credential $cred -ErrorAction Stop
$line.Server = $CurrentPageFile.__Server
$line.Name = $CurrentPageFile.Name
$line.InitialSize = $CurrentPageFile.InitialSize
$line.MaximumSize = $CurrentPageFile.MaximumSize
Write-Output $line
if (!$CurrentPageFile)
{
$CurrentPageFile = Get-WmiObject -ComputerName $VM -Query "select * from Win32_PageFileUsage" -Credential $cred -ErrorAction Stop
$line.Server = $CurrentPageFile.__Server
$line.Name = $CurrentPageFile.Name
$line.AllocatedBaseSize = $CurrentPageFile.AllocatedBaseSize
Write-Output $line
}
}
}
The output looks like this though, is there any way to change it so that all the colums are displayed at the top and then the contents are simply populated as each server fills in the cells?
Also, the output isnt produced in the order that I want to see it, I want it to appear as Server, Name, InitialSize, MaximumSize, AllocatedBaseSize.
InitialSize | : |
MaximumSize | : |
Server | :Server3 |
Name | : C:\pagefile.sys |
AllocatedBaseSize : 4095
InitialSize | : 0 |
MaximumSize | : 0 |
Server | :Server4 |
Name | : c:\pagefile.sys |
AllocatedBaseSize :
InitialSize | : 0 |
MaximumSize | : 0 |
Server | :Server5 |
Name | : c:\pagefile.sys |
AllocatedBaseSize :
InitialSize | : 16024 |
MaximumSize | : 16290 |
Server | :Server6 |
Name | : s:\pagefile.sys |
AllocatedBaseSize :
InitialSize | : 16024 |
MaximumSize | : 16290 |
Server | :Server7 |
Name | : s:\pagefile.sys |
AllocatedBaseSize :
When that same PS output engine decides he can't fit all, or a sufficient part of, the columns on one line, he will switch to the Format-List format.
You can force a Format-Table format, by piping the "$line | Format-Table -AutoSize", but then some columns could be missing or only partially displayed.
It's an optimal use of screen space that the output engine is trying to solve.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD and RvdNieuwendijk - much appreciated :smileygrin:
Adding the "| Format-Table -AutoSize" works perfectly :smileygrin:
Thanks again :smileygrin: