Powershell Script to join two function results based on common key columns - powershell

Could someone provide a PowerShell script to join two function results based on common key columns.
Example:
Result1 and Result2 has common field 'Name'. I want to join both the results and fetch the below informations.
$Result1 = get-wmiobject -ComputerName localhost -Class win32_service
$Result2 = get-service
Result
Name : wuauserv
DisplayName : Windows Update
Status : Running
StartMode : Manual
ProcessId : 400

Use below PowerShell script to join both the results based on common key column (Name).
$Result1=get-wmiobject -ComputerName localhost -Class win32_service
$Result2=get-service
$Result=#()
for($i=0;$i -lt $Result2.count;$i++)
{
$startmode=($Result1 | where{$_.Name -eq $Result2[$i].Name})|Select StartMode,ProcessId
$tempObj=new-object PSObject
$tempObj | Add-member noteproperty Name $Result2[$i].Name
$tempObj | Add-member noteproperty DisplayName $Result2[$i].DisplayName
$tempObj | Add-member noteproperty Status $Result2[$i].Status
$tempObj | Add-member noteproperty StartMode $startmode.StartMode
$tempObj | Add-member noteproperty ProcessId $startmode.ProcessId
$Result += $tempObj
}
$Result

No need to combine the output of the two commands. All of the properties are already out from the Get-WMIObject. (Personally I like using the CIM cmdlets instead of WMI wherever possible too)
Get-CimInstance Win32_Service | select Name, DisplayName, State, StartMode, ProcessId
Edit: The output of State from gcim is the Status property of Get-Service (Here's a calculated property to fix that, if that's an issue)
Get-CimInstance Win32_Service | select Name, DisplayName, #{Name="Status";Expression={$_.State}}, StartMode, ProcessId

try this
$result=get-wmiobject -ComputerName localhost -Class win32_service | %{ New-Object psobject -Property #{ objectwmi=$_; objectgetservice=(get-service | where name -eq $_.Name | select -first 1)} }

Related

WMI - Getting DNS Config via Powershell

when attempting to run my script then I got the following output. So Computer Name is duplicating.
I am getting {192.168.1.11, 192.168.1.12, $null} in DNS.
Script :
$Result=Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled = 'True'" -Property DNSServerSearchOrder,DNSHostname
$output = new-object PSObject
$output | add-member NoteProperty "ComputerName" $Result.DNSHostname
$output | add-member NoteProperty "DNSServerSearchOrder" $Result.DNSServerSearchOrder
$output
Output :
ComputerName DNSServerSearchOrder
------------ --------------------
{srv01, srv01} {192.168.1.11, 192.168.1.12, $null}
Use Select-Object when piping multiple results, like:
$Result | Select-Object -Property DNSHostname,DNSServerSearchOrder
DNSHostName DNSServerSearchOrder
----------- --------------------
srv01 {192.168.1.11, 192.168.1.12}
srv01 {$null}
The reason there are two results is you have two enabled adapters. The second has no DNS servers set

Search Print queues using powershell

I'm working on a quick string of code that allows me to search a specific print server and also its contents such as printers. What I want for it to do is once getting the printer information to be able to find a specific printer using the port name which in this case is an IP address not quite sure if is possible but I haven't found a command that lets me define a search using those values.
Get-Printer -computerName "server01"|select-object -Property Name, PortName
Name PortName
Printer01 X.XX.XXX.X
Prnter02 X.XX.XX.XX
is there a way to be able to find a printer using the Get-Printer commandlet and utilizing the port name to find the specific printer?
I'm just explaining in more detail #JeffZeitlin very correct answer.
Get-Printer - The Get-Printer cmdlet retrieves a list of printers installed on a computer. You can also use Get-Printer to retrieve the properties of a single printer, and then use that information as input into other cmdlets.
Get-Printer | Get-Member - The Get-Member cmdlet gets the members, the properties and methods, of objects.
Get-Printer | get-member
Get-Printer | Select-Object - The Select-Object cmdlet selects specified properties of an object or set of objects. It can also select unique objects, a specified number of objects, or objects in a specified position in an array.
Get-Printer | select-object -Property Name, PortName
Get-Printer | Select-Object | Where-Object - The Where-Object cmdlet selects objects that have particular property values from the collection of objects that are passed to it. For example, you can use the Where-Object cmdlet to select files that were created after a certain date, events with a particular ID, or computers that use a particular version of Windows.
Get-Printer | select-object -Property Name, PortName | where PortName -eq "PORTPROMPT:"
Get-Printer -ComputerName
Get-Printer -ComputerName $env:Computername | select-object -Property Name, PortName | where PortName -eq "PORTPROMPT:"
Scripting Example Get-Printer
$server = "$env:Computername"
$printersportnames = Get-Printer -ComputerName $server | select-object -ExpandProperty PortName
#Write-Host $printersportnames
$results = New-Object System.Collections.ArrayList # Empty Array
ForEach ($printerportname in $printersportnames) {
$printerportname = $printerportname.ToString()
#Write-Host $printerportname
$output = Get-Printer -ComputerName $server | where-object {$_.PortName -eq $printerportname}
$x = $output.Name
$y = $output.PortName
$z = $output.CommunicationStatus
$a = $output.OperatingStatus
$Object = New-Object PSObject
$Object | Add-Member -Name 'Name' -MemberType Noteproperty -Value $x
$Object | Add-Member -Name 'PortName' -MemberType Noteproperty -Value $y
$Object | Add-Member -Name 'CommStatus' -MemberType Noteproperty -Value $z
$Object | Add-Member -Name 'OperStatus' -MemberType Noteproperty -Value $a
$results += $object
}
$results

How can you add text to a PowerShell table result?

I'm trying to figure out how I can add some text such as MHz or GB to the results in the PowerShell table. Is it possible with a foreach loop?
Get-WmiObject Win32_PhysicalMmory |
Format-Table Banklabel, Manufacturer,
#{Label="Speed"; Expression={$_.ConfiguredClockSpeed}},
#{Label="Capacity"; Expression={[int64]($_.Capacity/1GB)}} -AutoSize
I have another way that comes close, but will not work with multiple results as it doesnt add the informtion into new rows.
$Disk = Get-WmiObject -Class Win32_LogicalDisk
$DiskSpace = [int64]($Disk.Size/1GB)
$FreeSpace = [INT64]($Disk.FreeSpace/1GB)
$UsedSpace = (($DiskSpace-$FreeSpace)/$DiskSpace).ToString("P0")
$OBJ = New-Object PSObject
$OBJ | Add-Member NoteProperty "ID" ($Disk.DeviceID)
$OBJ | Add-Member NoteProperty "Name" ($Disk.VolumeName)
$OBJ | Add-Member NoteProperty "Format" ($Disk.FileSystem)
$OBJ | Add-Member NoteProperty "Capacity" ("$DiskSpace-GB")
$OBJ | Add-Member NoteProperty "Free Space" ("$FreeSpace-GB")
$OBJ | Add-Member NoteProperty "Used" ($UsedSpace)
Write-Output $OBJ | Format-Table
You can use much more complex expression, see following example:
Get-WmiObject win32_physicalmemory | Format-Table Banklabel, Manufacturer, #{Label="Speed";`
Expression={$_.ConfiguredClockSpeed}}, #{Label="Capacity";`
Expression={([INT64]($_.Capacity/1GB)).ToString()+" GB"}} -autosize
use the concatenation for your requirement.
replace
Get-WmiObject win32_physicalmemory | Format-Table Banklabel, Manufacturer, #{Label="Speed";` Expression={$_.ConfiguredClockSpeed}}, #{Label="Capacity";` Expression={[INT64]($_.Capacity/1GB)}} -autosize
with this:
Get-WmiObject win32_physicalmemory | Format-Table Banklabel, Manufacturer, #{Label="Speed";` Expression={"$($_.ConfiguredClockSpeed) MHz"}}, #{Label="Capacity";` Expression={"$([INT64]($_.Capacity/1GB)) GB"}} -autosize
Hope it helps.
Because you're already using calculated properties it's easy to extend them. It depends what you try to achieve. I would recommend to place the unit into table header like this:
Get-WmiObject win32_physicalmemory |
Select-Object -Property Banklabel, Manufacturer, #{Name='Speed in MHz';Expression={$_.ConfiguredClockSpeed}}, #{Name='Capacity in GB'; Expression={[INT64]($_.Capacity/1GB)}}
If you really need it in the table you could do it like this:
Get-WmiObject win32_physicalmemory |
Select-Object -Property Banklabel, Manufacturer, #{Name='Speed';Expression={"{0} MHz" -f $_.ConfiguredClockSpeed}}, #{Name='Capacity in GB'; Expression={"{0} GB" -f [INT64]($_.Capacity/1GB)}}
But this way you loose the possibility to calculate with your results. It's just strings now.

Powershell - Select-Objects from previous commands

I can't seem to understand how to get a previous-command-object displayed on the final table in powershell. I understand I'm not explaining myself that well, so I'll go with the examples :)
get-dhcpserverv4scope -computername server01
This will return me something like this:
ScopeID,SubnetMask,Name
10.0.10.0,255.255.255.0,Scope1
10.5.0.0,255.255.248.0,Scope2
Now, when I run this instead I can't get ScopeID to be displayed with the second command results:
get-dhcpserverv4scope -computername server01 | ForEach-Object {get-dhcpserverv4optionvalue -computername server01 -scopeid $_.scopeid} | where {$_.OptionID -like "6"} | Select-Object Name, OptionID, Value
I obviously get to select the Objects from the get-dhcpserverv4optionvalue. How can I get also the ScopeID included in the latest Select-Object?
Is it actually possible?
Thanks
You can use Add-Member to add a property to the each object returned by dhcpserverv4optionvalue:
(get-dhcpserverv4optionvalue -computername server01 -scopeid $_.scopeid) |
Add-Member -MemberType NoteProperty -Name "ScopeID " -Value $_.ScopeID -PassThru
The full oneliner will be:
get-dhcpserverv4scope -computername server01 |
ForEach-Object
{(get-dhcpserverv4optionvalue -computername server01 -scopeid $_.scopeid) | Add-Member -MemberType NoteProperty -Name "ScopeID " -Value $_.ScopeID -PassThru} |
where {$_.OptionID -like "6"} | Select-Object Name, OptionID, Value
Not tested, but this should work:
get-dhcpserverv4scope -computername server01 | ForEach-Object {
$ScopeID = $_.ScopeID
get-dhcpserverv4optionvalue -computername server01 -scopeid $_.scopeid
} | where {
$_.OptionID -like "6"
} | Select-Object Name, OptionID, Value, #{L='ScopeID';E={$ScopeID}}
I am basically saving the ScopeID value within the ForEach statement and then using at the end by creating a custom property at the end.

Select-Object with output from 2 cmdlets

Suppose I have the following PowerShell script:
Get-WmiObject -Class Win32_Service |
Select DisplayName,#{Name="PID";Expression={$_.ProcessID}} |
Get-Process |
Select Name,CPU
This will:
Line 1: Get all services on the local machine
Line 2: Create a new object with the DisplayName and PID.
Line 3: Call Get-Process for information about each of the services.
Line 4: Create a new object with the Process Name and CPU usage.
However, in Line 4 I want to also have the DisplayName that I obtained in Line 2 - is this possible?
One way to do this is to output a custom object after collecting the properties you want. Example:
Get-WmiObject -Class Win32_Service | foreach-object {
$displayName = $_.DisplayName
$processID = $_.ProcessID
$process = Get-Process -Id $processID
new-object PSObject -property #{
"DisplayName" = $displayName
"Name" = $process.Name
"CPU" = $process.CPU
}
}
A couple of other ways to achieve this:
Add a note property to the object returned by Get-Process:
Get-WmiObject -Class Win32_Service |
Select DisplayName,#{Name="PID";Expression={$_.ProcessID}} |
% {
$displayName = $_.DisplayName;
$gp = Get-Process;
$gp | Add-Member -type NoteProperty -name DisplayName -value $displayName;
Write-Output $gp
} |
Select DisplayName, Name,CPU
Set a script scoped variable at one point in the pipeline, and use it at a later point in the pipeline:
Get-WmiObject -Class Win32_Service |
Select #{n='DisplayName';e={($script:displayName = $_.DisplayName)}},
#{Name="PID";Expression={$_.ProcessID}} |
Get-Process |
Select #{n='DisplayName';e={$script:displayName}}, Name,CPU
Using a pipelinevariable:
Get-CimInstance -ClassName Win32_Service -PipelineVariable service |
Select #{Name="PID";Expression={$_.ProcessID}} |
Get-Process |
Select Name,CPU,#{Name='DisplayName';Expression={$service.DisplayName}}