Home > Software design >  I'm a novice at Powershell and im trying to complete a task
I'm a novice at Powershell and im trying to complete a task

Time:12-11

I'm starting to work with Powershell and I've been doing some courses online. At this moment I'm stuck in a Challenge, I hope some good soul can help me with the instructions from the course.

Instructions are:

1-Find out what Windows features are installed on the server machine. (I'm remoting command to a computer named "Server")

2-Select only the data stored in the Name and InstallState columns.

3-Sort the data by the Name property in ascending (alphabetical) order.

4-Make sure the output format is set to table

5-Save the final output into a file called C:\features.txt on your desktop machine.

What I have come up with is this:

 Invoke-Command -ComputerName Server -ScriptBlock{
>> Get-WindowsFeature | Select-Object -Property Name, InstallState | Sort-Object -Property Name | Format-Table
>> } | Out-File C:\features.txt

I have tried both with and without the select-object command since I know the format-table command works almost the same in this case. Thank u!

CodePudding user response:

When ever I invoke a scriptblock on a remote computer i assign it to a variable and then handle the results this:

$myResults= Invoke-Command -ComputerName Server -ScriptBlock{…}
Foreach ($item in $myResults){
   Write-host” $item.name / 
   $item.nstallState”
}

You use the pipeline rather excessively which is not wrong but for me personally still a beginner my self it is not easy to totally comprehend what each of them is exactly returning and forwarding to the next. Dont try to write a perfect line with several pips in the first attempt. Start with the smallest fastest fraction of the task. In this case yust get them all out without filter and pipelines. Then work your way from there, make little changes, use write-host to display the results and trail and error yourself to a deeper understanding of each of them. And then in the end u can chain them up.

CodePudding user response:

As per my comment for example.

# 1-Find out what Windows features are installed on the server machine. 
Get-WindowsFeature
# Results
<#

#>


# 2-Select only the data stored in the Name and InstallState columns.
Get-WindowsFeature | 
Select-Object -Property Name, InstallState
# Results
<#

#>

# 3 - Sort the data by the Name property in ascending (alphabetical) order.
Get-WindowsFeature | 
Select-Object -Property Name, InstallState | 
Sort-Object -Property Name

# 4-Make sure the output format is set to table
Get-WindowsFeature | 
Select-Object -Property Name, InstallState | 
Sort-Object -Property Name | 
Format-Table # though this is not needed, since table is the default for less than 5 properties.
# Results
<#

#>

<#
# 5-Save the final output into a file called C:\features.txt on your desktop 
machine.
#>
Get-WindowsFeature | 
Select-Object -Property Name, InstallState | 
Sort-Object -Property Name | 
Export-Csv -Path 'SomePathName' -NoTypeInformation -Append


# (I'm remoting command to a computer named "Server")
$Computers | 
ForEach-Object {
        Invoke-Command -ComputerName $PSItem.ComputerName -ScriptBlock{
        Get-WindowsFeature | 
        Select-Object -Property Name, InstallState | 
        Sort-Object -Property Name | 
        Export-Csv -Path 'SomePathName' -NoTypeInformation  -Append
    } 
}
  • Related