Powershell Test-Connection: different results when run AsJob vs straight-up - powershell

Short version: Why does Test-Connection provide only one result when running -AsJob?
Detailed version:
When the Test-Connection (ping!) command is run in Powershell (running as Administrator) it uses the default value of 4 for the Count parameter. This gives me the expected results (some irrelevant data removed for clarity):
PS C:\Windows\system32> Test-Connection www.stackoverflow.com
Destination IPV4Address IPV6Address Bytes Time(ms)
----------- ----------- ----------- ----- --------
www.stackove... 64.34.119.12 {} 32 117
www.stackove... 64.34.119.12 {} 32 113
www.stackove... 64.34.119.12 {} 32 111
www.stackove... 64.34.119.12 {} 32 113
However, when I run it with the -AsJob flag, it seems (when running Receive-Job, after waiting a few secs) the Count was only 1, but I expected it to be 4 and get similar results as above. Instead I get this:
PS C:\Windows\system32> Test-Connection www.stackoverflow.com -AsJob
Id Name State HasMoreData Location Command
-- ---- ----- ----------- -------- -------
1 Job1 Running False . Test-Connection
PS C:\Windows\system32> Receive-Job 1
Destination IPV4Address IPV6Address Bytes Time(ms)
----------- ----------- ----------- ----- --------
www.stackove... 64.34.119.12 {} 32 113
The AsJob variant always returns one row, even if you provide a specific value for Count.
My Google-fu and Get-Help-fu don't seem to be sufficient. Can someone please explain what's going on here, preferably with some pointers how and where to find this info myself (e.g. which help files or sources explain this behavior)?

Could be a bug. In v3 you get back 4 pings.

Related

Why the select -first 1 doesn't work in this PowerShell command?

I'm testing in a Windows Failover cluster environment. Below is my code.
PS C:\Users\administrator.DEV> Get-ClusterResource *disk*
Name State OwnerGroup ResourceType
---- ----- ---------- ------------
Cluster Disk 1 Online Available Storage Physical Disk
Cluster Disk 2 Online Cluster Group Physical Disk
Cluster Disk 3 Online Available Storage Physical Disk
PS C:\Users\administrator.DEV> (Get-ClusterResource *disk*).GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
PS C:\Users\administrator.DEV> (Get-ClusterResource *disk*)[0].GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False ClusterResource Microsoft.FailoverClusters.PowerShell.ClusterObject
PS C:\Users\administrator.DEV>
As you can see, I have three disk resource. But when I want to get the first one with the select cmdlet, I got empty output.
PS C:\Users\administrator.DEV> Get-ClusterResource *disk* | select -First 1
PS C:\Users\administrator.DEV>
Why this behavior? How can I get the first disk resource in this case?
Apparently, Get-ClusterResource exhibits nonstandard behavior by emitting an array of results as a whole (as a single object) rather than enumerating it, i.e. emitting its elements one by one (the latter is what cmdlets are generally expected to do).
Therefore, either use (Get-ClusterResource *disk*) | select -First 1 (note the (...) to force enumeration of the array), or - as you're already showing - simply index directly into the array: (Get-ClusterResource *disk*)[0]

Improve PS Scripts (jobs, ping, etc)

I've prepared a script in PS, which part looks like this:
"$Octets.11","$Octets.12","$Octets.13","$Octets.14",..., "$Octets.51" - I'd like to 'cut' this part of code, is it possible to write sentence like: "$Octets.11" - "$Octets.51" ?
My script was written to ping some devices in LAN. When some computers are active, the script shows them together at once as ACTIVE, but when it's not, it's pinging for 3 seconds and then shows its inactive, then starts ping another (inactive) and go on. It takes a lot of time to wait dor result. Can I create a function/job to ping every device at once?
Thank you for every hint!
Using test-connection -asjob (unfortunately the headers don't match the properties):
# 1..60 | % tostring 1\0\.\0\.\0\.0
test-connection (1..60 | % tostring comp000) -AsJob -count 1 |
receive-job -wait -auto # 5 seconds
Source Destination IPV4Address IPV6Address Bytes Time(ms)
------ ----------- ----------- ----------- ----- --------
MYCOMP comp001 10.0.0.1 32 3
MYCOMP comp002 10.0.0.2 32
MYCOMP comp003 10.0.0.3 32 2
(null responsetime is down)
(address is the hostname)
This doesn't seem to use any extra processes.

VMWare PowerCLI Get DiskUsage of powered off vm's

I'm creating a script that gets all vm's and shows the DiskSpace. THe Problem is, that if a vm is powered off, it won't show the uesed Space of a disk.
Here are two examples: First one with an VM that is powered on:
PowerCLI C:\> Get-VM sluwv0039
Name PowerState Num CPUs MemoryGB
---- ---------- -------- --------
sluwv0039 PoweredOn 2 4.000
PowerCLI C:\> $VM = Get-VM sluwv0039
PowerCLI C:\> $VM.guest.disks
CapacityGB FreeSpaceGB Path
---------- ----------- ----
49.997 5.417 C:\
Example two where the VM is powered off:
PowerCLI C:\> Get-VM sluwv0012
Name PowerState Num CPUs MemoryGB
---- ---------- -------- --------
sluwv0012 PoweredOff 4 8.000
PowerCLI C:\> $VM = Get-VM sluwv0012
PowerCLI C:\> $VM.guest.disks
PowerCLI C:\>
Note: The Last line is the output. There is no "CapacityGB" etc.
Correct, that property is reading from the guest file system to see how much space is left on the partition. In your case, the C:\ drive. If the VM is off, there's no way for PowerCLI to find that property.
Alternatively, you could look at the $vm.ExtensionData.Summary.Storage properties and do some rough conversions. Note: the output of those are in byte, so you'll want to convert them to GB. Example: $tempVM.ExtensionData.Summary.Storage.Committed / 1GB
It won't be exact, but it will be better than no output at all.
here is example of script to show vm specification:
Get-Vm | Select-Object Name,PowerState,VMHost,NumCPU,MemoryGB,ProvisionedSpaceGB,#{N="HostName";E={#($.guest.HostName)}},#{N="Gateway";E={#($.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute.Gateway.IpAddress[0])}},#{N="DNS";E={$.ExtensionData.Guest.IpStack.DnsConfig.IpAddress}},#{N="IPAddress";E={#($.guest.IPAddress -like "192.168.*")}},#{N="Nics";E={#($.guest.Nics)}},#{N="Datastore";E={#($ | Get-DataStore)}},#{N="Disks";E={#($.guest.Disks)}},Version,#{N="State";E={#($.guest.State)}},#{N="OS";E={#($_.guest.OSFullName)}}
the sample output is like this:
Name State VMHost NumCpu MemoryGB PowerState ProvisionedSpaceGB Version IPAddress HostName OS Nics Disks VMwareTools Gateway DNS
test Running 192.168.32.100 2 1 PoweredOn 43.1085147 v8 192.168.122.1 Elenoon Ubuntu Linux (64-bit) Network adapter 1:VM Network Network adapter 2:local : : Capacity:17167286272, FreeSpace:14212493312, Path:/ Capacity:15188623360, FreeSpace:15154872320, Path:/media/files Capacity:10724835328, FreeSpace:10672824320, Path:/var/log Capacity:973770752, FreeSpace:690139136, Path:/boot guestToolsRunning 127.0.0.1
hope to be useful ;)

What is the -view parameter of Format-List?

Format-List apparently has a string parameter named "view", as can be seen here. What does it do, and how does it work? I cannot find any documentation beyond "The name of an alternate format or 'view.'"
The '-View' parameter on the various Format-* cmdlets allows you to get various different "views" or formattings of the data e.g.:
PS> Get-Process
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
672 56 272684 220692 975 141.45 8480 powershell
692 34 47184 60156 234 23.73 17048 powershell
751 82 217624 162780 1047 157.73 13336 powershell_ise
versus
PS> Get-Process | Format-Table -View StartTime
StartTime.ToShortDateString(): 1/14/2013
ProcessName Id HandleCount WorkingSet
----------- -- ----------- ----------
powershell 8480 672 225988608
StartTime.ToShortDateString(): 2/6/2013
ProcessName Id HandleCount WorkingSet
----------- -- ----------- ----------
powershell 17048 624 92418048
StartTime.ToShortDateString(): 1/17/2013
ProcessName Id HandleCount WorkingSet
----------- -- ----------- ----------
powershell_ise 13336 771 166686720
As for determining which commands support alternate views, you can usually find such info in the docs. Here's an excerpt from the Get-Process help:
You can also use the built-in alternate views of the processes
available with Format-Table, such as "StartTime" and "Priority", and
you can design your own views. For more information, see
T:Microsoft.PowerShell.Commands.Format-Table.
The PowerShell Community Extensions also includes a command called Get-ViewDefinition that can get this info when the docs aren't available (or of much help in this regards.

PowerShell: Getting Help on Get-Process -Property CPU

With PowerShell 3, I tried to get help on what properties are available for CPU; while using Get-Process. I just tried a shot in the dark, as below:
Help Get-Process -Property CPU
But, failed. Any help, please!
What are you looking for? Information about your processor? Get-Process list running processes(e.g. internet explorer) on your computer, not info about your processor-chips(CPU). Ex:
Get-Process
Output:
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
284 25 7128 8748 103 1608 AppleMobileDeviceService
75 7 1136 1528 44 1588 armsvc
703 82 6612 7732 114 1,25 4212 AsusAudioCenter
Information about your processor can be found using:
Get-WmiObject Win32_Processor
Output:
Caption : Intel64 Family 6 Model 42 Stepping 7
DeviceID : CPU0
Manufacturer : GenuineIntel
MaxClockSpeed : 3400
Name : Intel(R) Core(TM) i7-2600 CPU # 3.40GHz
SocketDesignation : LGA1155
To get all properties about your CPU use Get-WmiObject Win32_Processor | fl *. To get a list of avaiable properties, use the Get-Member cmdlet to examine the object that Get-WmiObjectreturns:
Get-WmiObject Win32_Processor | Get-Member
Your shot in the dark missed. Also, since your description of what went wrong is nothing more than "But, failed.", I can only guess at what your problem might be. In order to better help you use help you need to help us by providing pertinent information about your problem such as error messages.
Firstly, Help (or the Get-Help cmdlet) does not have a -Property parameter. -Parameter might be what you looking for, however running Help Get-Process -Parameter CPU will reveal that the Get-Process cmdlet does not have a CPU parameter.
Secondly, Get-Process returns instances of the System.Diagnostics.Process class. The documentation or running Get-Process | Get-Member will show you what properties that class exposes. You can retrieve them by running something like...
Get-Process | Select-Object -Property (
'ProcessName',
'Id',
'ProcessorAffinity',
'UserProcessorTime',
'PrivilegedProcessorTime',
'TotalProcessorTime'
);
Finally, unlike previous versions PowerShell 3.0 does not install local help content. You need to run the Update-Help cmdlet to download and install help content. Alternatively, when running Get-Help you can pass the -Online parameter which will open the help content from MSDN in a web browser.